Java 15-121 Fall 2018 Margaret Reid-Miller
Reminders How many late days can you use all semester? 3 How many late days can you use for a single assignment? 1 What is the penalty for turning an assignment late when you run out of late days? No credit Fall 2018 15-121 (Reid-Miller) 2
Java vs Python Program Translation: Java is compiled, Python is interpreted Java syntax: Uses braces {} to group statements (indentation is recommended, but not semantic) Most statements end in semi-colons. Java programs: Often have several classes, each in a file with same name. Java programs start with the method main. Fall 2018 15-121 (Reid-Miller) 3
Today Working with Eclipse Basic Types Types (primitive/reference) (operators/precedence) Variable declaration Expressions Naming conventions Fall 2018 15-121 (Reid-Miller) 4
Declared Types Java requires that you declare the type of each variable (when it first appears in your program) and the return type of each method. Types: boolean (true or false) int (integer) double (floating point decimal) String (sequence of characters) void - return type of a method that returns no value Java checks that the types match when you compile. Compiler error messages often indicate a type mismatch. Fall 2018 15-121 (Reid-Miller) 5
Primitive Data Types Java has two categories of data: primitive data (e.g., number, character) object data (programmer created types) There are 8 primitive data types: byte, short, int, long, float, double, char, boolean Primitive data are only single values; they have no special capabilities. Fall 2018 15-121 (Reid-Miller) 6
Common Basic Types Type Description Example of Literals int integers (whole numbers) 42, 60634, -8, 0 double real numbers 0.039, -10.2, 4.2E+72 char single characters 'a', 'B', '&', '6' boolean logical values true, false String List of characters "", "a", "ab6&" Fall 2018 15-121 (Reid-Miller) 7
Numbers Type Storage Range of Values byte 8 bits -128 to 127 short 16 bits -32,768 to 32,727 int 32 bits -2,147,483,648 to 2,147,483,647 long 64 bits -9x10 18 to 9x10 18 float 32 bits ±10-45 to ±10 38, 7 significant digits double 64 bits ±10-324 to ±10 308, 15 significant digits Fall 2018 15-121 (Reid-Miller) 8
Basic Type Operators Type Description Operators int integers +, -, *, /, %, ++, -- double real numbers +, -, *, / char boolean single characters relational logical ==,!=, <, >, <=, >=,!, &&, String Strings + (concatenation) Fall 2018 15-121 (Reid-Miller) 9
Strings String is not a primitive type, but a class/type String variables store references to memory addresses Strings are immutable! You can assign a new string to a variable, though. A string cannot span more than one line: "Not a valid String literal Useful operations(methods): WRONG! charat, compareto, contains, equals (don t use ==), length, substring, trim (see String API) Fall 2018 15-121 (Reid-Miller) 10
Variable Declaration Before you can use a variable, you must declare its type and name. You can declare a variable only once in a method. Examples: int numdimes; double length; char coursesection; boolean done; String lastname; camelcase is the Java convention Fall 2018 15-121 (Reid-Miller) 11
Declaring Variables Declaring a variable instructs the compiler to set aside a portion of memory large enough to hold data of that type. int count; double length; count length No value has be put in memory yet. That is, the variable is undefined. Fall 2018 15-121 (Reid-Miller) 12
Assignment Statements An assignment statement stores a value into a variable's memory location: <variable> = <expression>; The type of the variable and expression must match. The first assignment to a variable initializes it. count = 3; count 3 length = 72.3 + 2.0; length 74.3 Fall 2018 15-121 (Reid-Miller) 13
Assignment statements shortcuts int n; n = 0; int n = 0; n = n + 1; n += 1; n++; n = n - 1; n -= 1; n--; Other assignment operators: *=, /=, %= Fall 2018 15-121 (Reid-Miller) 14
Data Conversion Widening conversions convert data to another type that has the same or more bits of storage. E.g., short to int, long (safe) int to long (safe) int to float, double (magnitude the same but can lose precision) Narrowing conversions convert data to another type that has the fewer bits of storage and can lose information. E.g., double or float to any integer type int, long to short Fall 2018 15-121 (Reid-Miller) 15
Mixing Types When a Java operator is applied to operands of different types, Java does a widening conversion automatically, known as a promotion. Example: 2.2 * 2 evaluates to 4.4 1.0 / 2 evaluates to 0.5 double x = 2; assigns 2.0 to x "count = " + 4 evaluates to "count = 4" string concatenation Fall 2018 15-121 (Reid-Miller) 16
Mixing Types Conversions are done on one operator at a time in the order the operators are evaluated. 3 / 2 * 3.0 + 8 / 3 2.0 * 4 / 5 + 6 / 4.0 5.0 3.1 Fall 2018 15-121 (Reid-Miller) 17
Mixing Types String concatenation has the same precedence as + - and is evaluated left to right. 1 + "x" + 4 "1x4" "2+3=" + 2 + 3 1 + 2 + "3" "2*3=" + 2 * 3 4-1 + "x" "x" + 4-1 "2+3=23" "33" "2*3=6" "3x" syntax error Fall 2018 15-121 (Reid-Miller) 18
Type Casting Type casting tells Java to convert one type to another. Uses: Convert an int to a double to force floating-point division. Truncate a double to an int. Examples: double average = (double) 12 / 5 int feet = (int) (28.3 / 12.0) Fall 2018 15-121 (Reid-Miller) 19
Type casting Because type casting has high precedence, it casts the operand immediately to its right only. Example: double s = (double) 2 + 3 / 2; double s2 = (double) (2 + 3) / 2; double average = (double) 22 / 4; double average2 = 22 / (double) 4; double wrong = (double) (22 / 4); 3.0 2.5 5.5 5.5 5.0 Fall 2018 15-121 (Reid-Miller) 20
char data type A variable of type char holds exactly one (Unicode) character/symbol. Every character has a corresponding integer value. The digit characters '0' '9' have consecutive integer values, as do the letters 'A' 'Z' and 'a' 'z'. We can use this ordering to sort alphabetically. Conversions: String letter = "" + 'M'; // evaluates to "M" int aasint = 'a'; // evaluates to 97 'a' + 2; // evaluates to 99 char c = (char)('a' + 2); // evaluates to 'c' Fall 2018 15-121 (Reid-Miller) 21
Operator Precedence Operator type Operator Associates grouping (), [],. Left to right postfix <expr>++, <expr>-- Right to left unary/prefix ++, --, +, -,! Right to left cast (type) Right to left multiplicative *, /, % Left to right additive +, - Left to right relational <, <=v >, >= Left to right equality ==,!= Left to right AND && Left to right OR Left to right assignment =, +=, -=, *=, /=, %= Right to left Fall 2018 15-121 (Reid-Miller) 22
Identifiers and Keywords Identifiers are names that specify different elements of a program such as class, method, or variable can be any combination of letters, digits, _ or $ the first character must NOT be a digit case-sensitive (total is different from Total) Examples: main method1 maxcount TUESDAY $amount Puzzle Keywords are a set of predefined identifiers that are reserved for special uses. Examples: public static void class Fall 2018 15-121 (Reid-Miller) 23
Naming Conventions Java naming conventions help readers readily distinguish various Java elements: Class: Starts with a capital letter A class names should be a noun that describe an object type. e.g., String, Radio Method: Starts with a lower case letter A method name should start with a verb and describe what the method does. e.g., displayquestion, getname, computetax Variable: Starts with a lower case letter A variable name should be a noun that describes what data it holds. e.g., favoritefood, name, taxrate Fall 2018 15-121 (Reid-Miller) 24