cs 211 java basics
DESCRIPTION
CS 211 Java Basics. Today’s lecture. Review of Chapter 1 Go over homework exercises for chapter 1. Simple Java Program. public class HelloWorld { public static void main (String[] args ) { //our code will all go here… System.out.println ("Hello, World!"); } }. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/1.jpg)
CS 211Java Basics
![Page 2: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/2.jpg)
Today’s lectureReview of Chapter 1Go over homework exercises for chapter
1
![Page 3: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/3.jpg)
Simple Java Program
public class HelloWorld {public static void main (String[] args) {
//our code will all go here…System.out.println("Hello, World!");
}}Each word of this should make sense by the semester's end! For now it is boilerplate code—just the template we'll use to write code.
file: HelloWorld.java
![Page 4: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/4.jpg)
WhitespaceWhitespace means the 'blank' characters:
space, tab, newline characters.White Space is (almost) irrelevant in Java.
Java does not care about indentationHowever, indentation is highly recommended!
Java uses curly braces, { and } to replace indentationFollow the indentation pattern you had for Python, for
readabilityThe computer will only care about { }, however
![Page 5: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/5.jpg)
Good Whitespace Examplepublic class GoodSpacing {
public static void main (String[] args) {int x = 5;int y = 12;System.out.println("x+y = "+
(x+y));}
}indentation levels for each block: class, method definitions, control structures…
![Page 6: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/6.jpg)
Types!Java is strongly typed: every
expression has a specific type that is known at compile time.
Java has two kinds of types:primitive types (containing literal
values)reference types (containing objects of
some class). These are complex types.Variable types must be declared before
use
![Page 7: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/7.jpg)
Type declarationpublic class GoodSpacing {
public static void main (String[] args) {int x = 5;int y = 12;System.out.println("x+y = "+
(x+y));}
}x is declared to be of type integerargs is declared to be a list of stringsmain is declared to return void (nothing)the println method returns void
![Page 8: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/8.jpg)
Primitive Typesboolean: truth values: true, falsechar: a character in single-quotes: 'a' 'H' '\n' '5’integers: byte, short, int, longfloating point: float, double
void is technically not a type, but is used to represent when a method doesn’t return a value
![Page 9: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/9.jpg)
Specific Integer TypesJava provides integral numbers of different 'widths' (different numbers of bits), and different ranges of values:
byte (8 bits) -128 to 127short (16 bits) -32768to 32767int (32 bits) -2147483648 to 2147483647long (64 bits) (-263) to (263-1)
char (16 bits) 0 to 65535 (all positive)(a character is actually stored in memory as an integer)
You don’t have to memorize any of these ranges, just be aware they exist
![Page 10: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/10.jpg)
Floating Point NumbersApproximating the real #s: called floating point numbers. internal binary representation: like scientific notation.
S: sign bit. E: bias-adjusted exponent.M: adjusted fractional value.
value = (-1)S * 2E * MAlso representable: infinity (+/−), NaN ("not a number")float: 32-bit representation. (1 sign, 8 exp, 23 frac)double: 64-bit representation. (1 sign, 11 exp, 52 frac)
sign exponent fraction
![Page 11: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/11.jpg)
Reference Typesstrings: Stringlanguage-defined: System, Mathuser-defined: Person, Address, Animalarrays: int[], String[], Person[], double[][]
int[] myArray = {1, 5, 7};
int[] otherArray = new int[];
Person jill = new Person();new is a keyword that we can use to create all reference typesarrays and strings are special in Java; other ways to create without new
![Page 12: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/12.jpg)
Creating VariablesVariables must be declared and initialized before use.Declaration: creates the variable. It includes a type and a
name. The variable can only hold values of that type.int x; char c; double[][] grid; Person p;
Initialization: assign an expr. of the variable's type to it.x=7+8; c='M';
grid={{0,1},{2,3}}; p= new Person();
Both: we can declare and instantiate all at once:int x = 5;
char c = 'S';
Person p = new Person();
![Page 13: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/13.jpg)
Castinga cast is a conversion from one type to another.
We cast things by placing the type in parentheses in front of it:
(int) 3.14
One use: forcing floating-point division.int x=3, y=4;double z = ((double)x)/y;System.out.println(z);
![Page 14: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/14.jpg)
StringsCan be declared as
String cat = “Fluffy”;String cat = new String(“Fluffy”);
Can be added with + operator (concatenation)“Hello” + “ “ + “world”
Can mix typesString cat = “Hi” + 3 + ‘ ‘ + false;int number = 5 + 3;
![Page 15: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/15.jpg)
Java CommentsThere are two main styles of comments in Java:Single-Line: from // to the end of the line.
Multi-Line: all content between /* and */, whether it spans multiple lines or part of one line.
JavaDoc: convention of commenting style that helps generate code documentation/API. More on this later.
![Page 16: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/16.jpg)
Expressions, Statements
Expression: a representation of a calculation that can be evaluated to result in a single value. There is no indication what to do with the value.
Statement: a command, or instruction, for the computer to perform some action. Statements often contain expressions.
![Page 17: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/17.jpg)
Basic Expressionsliterals (all our primitive types)operation expressions:
< <= > >= == != (relational ops)
+ - * / % (math ops)& | && || !(boolean ops)
parenthesized expressions ( expr )variables (can store primitive types or reference
types)method calls that return a value (are not void)
![Page 18: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/18.jpg)
Programming TRAPOnly use == and != for primitive typesWe will learn how to compare reference types
later
![Page 19: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/19.jpg)
Expression ExamplesLegal:x++ (3>x) && (! true)x%2==1 (x<y)&&(y<z)numPeople drawCard()(2+3)*4 y!=z
Illegal:x>y>z 4 && false x=3 7(x+y)
![Page 20: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/20.jpg)
Basic StatementsDeclaration: announce that a variable exists.Assignment: store an expression's result into a
variable.method invocations (may be stand-alone)blocks: multiple statements in { }'scontrol-flow: if-else, for, while, … (next
lecture)
![Page 21: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/21.jpg)
Statement Examplesint x; // declare xx = 15; // assignment to xint y = 7;// decl./assign. of yx = y+((3*x)−5); // assign. with operatorsx++; // increment (x = x+1)System.out.println(x);// method invocation
if (true):{ //if statementint x = 50;int y = 3;x = x + y;
}
![Page 22: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/22.jpg)
Print Statementprintln is a method of the PrintStream class,
that can print anything on a single line:System.out.println(“Hello World!);
System.out is the standard buffer for printing to the screen
![Page 23: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/23.jpg)
User InputCreate a Scanner object and get the next value: Scanner scan = new Scanner(System.in); int num = scan.nextInt();
String string = scan.nextLine();
System.in is the standard buffer for keyboard input
![Page 24: CS 211 Java Basics](https://reader033.vdocument.in/reader033/viewer/2022051518/56816021550346895dcf235e/html5/thumbnails/24.jpg)
Questions?