Two Types of Types } Java is object-oriented. Classes define objects such as the Windows we saw in previous video : Specified via a class definition 1. Instantiated using: new ConstructorName() 2. Written by a programmer (you or someone else) using Java 3. Known as reference types (because the variable identifier has 4. a value that is a reference address to an object in memory) Class #03: } The language has some other things as well: Java Primitives Built into Java without definition as a class 1. No constructors used for instantiation Software Design I (CS 120): D. Mathias 2. Known as primitive types (because the variable identifier 3. stores the basic value we want directly) Software Design I (CS 120) 2 Primitive Types in Java Using Primitive Variables Integer types } Declaring an int (or any other primitive type), uses the public class Program byte -128...127 { same syntax as before: public static void main( String[] args ) short (2 bytes) -32_678...32_677 type name; { int (4 bytes) -2_147_483_648...2_147_483_647 Simple, given values int num1; num1 = 10; } When we want to assign a long (8 bytes) -9_223_372_036_854_775_808... 9_223_372_036_854_775_807 value to the variable identifier, int num2 = -77; Value computed, we can simply assign a given Real number types based on math int num3 = 8 / 4; or computed value } expression } float (4 bytes) 7 decimal digits of accuracy double (8 bytes) 15 decimal digits of accuracy Other types (to be covered later) In memory, the JVM stores the assigned value along with the variable identifier. boolean (1 byte) true / false char (2 bytes) single character of text Software Design I (CS 120) 3 Software Design I (CS 120) 4 1
Using Primitive Variables Using Primitive Variables } Once you create a primitive } Once you create a primitive public class Quadratic public class Program variable, it can be used variable, it can be used { { anywhere that values of the anywhere that values of the public static void main( String[] args ) public static void main( String[] args ) same type can be used { same type can be used { int x = 14; int size = 1112; } For example, we can use an } Another example: int halfSize = size / 2; int y; int variable wherever an int Window firstWin = new Window(); y = 3 * x * x – 2 * x + 5; firstWin.setSize(600); itself can be used, such as in arithmetic, or as input to a System.out.println(“y = ” + y); Window bigWin = new Window(); method that requires an int } bigWin.setSize( size ); } Window smallWin = new Window(); smallWin.setSize( halfSize ); } } Software Design I (CS 120) 5 Software Design I (CS 120) 6 The Java int and Its Relatives The Java int and Its Relatives Syntax Syntax • 1 or more consecutive decimal digits (0-9) • 1 or more consecutive decimal digits (0-9) • optional: preceded by + or - • optional: preceded by + or - • optional: can separate long numbers using underscore _ • optional: can separate long numbers using underscore _ • examples: 215 -17 +0 7_234_562 • examples: 215 -17 +0 7_234_562 Unary operator Unary operator - unary negation (e.g., -33) - unary negation (e.g., -33) Binary infix operators Binary infix operators Postfix operators Integer division leads to integer results, % gives the integer with no decimal place . remainder for division: + addition + addition ++ increment by 1 These results are simply truncated, not rounded . 10 % 1 is 0 - subtraction - subtraction -- decrement by 1 10 / 1 is 10 10 % 2 is 0 * multiplication * multiplication 10 / 2 is 5 10 % 3 is 1 10 / 3 is 3 / division / division Increase/decrease 10 % 4 is 2 10 / 4 is 2 the value by 1 % remainder etc. % remainder etc. Software Design I (CS 120) 7 Software Design I (CS 120) 8 2
The Java float or double The Java float or double Syntax Syntax • a number with a single decimal point • a number with a single decimal point • optional: preceded by + or - • optional: preceded by + or - • optional: can separate long numbers using underscore _ • optional: can separate long numbers using underscore _ • optional: can use scientific (E) notation • optional: can use scientific (E) notation • examples: 21.5 -1.7 123_456.789 5.23E7 • examples: 21.5 -1.7 123_456.789 5.23E7 Unary operator Unary operator - unary negation (e.g., -3.3) - unary negation (e.g., -3.3) Postfix operators Postfix operators Infix operators Infix operators % gives same remainder as Dividing doubles does ++ increment by 1 ++ increment by 1 the int version, but in + addition produce decimal-places. + addition double form: -- decrement by 1 -- decrement by 1 - subtraction - subtraction 10.0 / 1.0 is 10.0 10.0 % 1.0 is 0.0 10.0 / 2.0 is 5.0 * multiplication * multiplication 10.0 % 2.0 is 0.0 10.0 / 3.0 is 3.333… 10.0 % 3.0 is 1.0 Increase/decrease Increase/decrease / real division 10.0 / 4.0 is 2.5 / real division 10.0 % 4.0 is 2.0 the value by 1 the value by 1 etc. etc. % remainder % remainder Software Design I (CS 120) 9 Software Design I (CS 120) 10 Basic Java Division Precedence for Mathematical Operators } Consider an expression like: 7 * 3 + 2 } Evaluate the following expressions: Is this: } (7 * 3) + 2 = 23? Or maybe: int x = 5 / 2; 1. 2 } 7 * (3 + 2) = 35? } We can decide using following evaluation rules: int x = 5 / 8; 2. 0 Anything grouped in parentheses goes first. 1. double x = 5.0 / 2.0; 3. 2.5 Within a group, use following precedence (top down order), doing the 2. various operations in sequence: double x = 5.0 / 8.0; 4. 0.625 Precedence Table for Arithmetic ++ -- (decrement/increment) - (unary negation) * / % (multiplicative) + - (additive) With equal precedence, go left-to-right 3. Software Design I (CS 120) 11 Software Design I (CS 120) 12 3
An Example Mixing Types in Java 1 + 2 * 3 – 4 – 5 / 6 + 9 % 2 } We can combine types in an expression like: 10 * 3.6 First precedence: * / % } Computation coerces “narrow” types into “wider” ones 1 + [2 * 3] – 4 – [5 / 6] + [9 % 2] 1 + 6 – 4 – 0 + 1 Remember: int division has no decimal precision! } The result will be of the widest type contained in the basic binary expression we are calculating Next precedence: left-to-right } Examples: [1 + 6] – 4 – 0 + 1 } 7 / 2 == 3 (int) [7 – 4] – 0 + 1 } 7.0 / 2.0 == 3.5 (double) [3 – 0] + 1 } 7 / 2.0 == 3.5 (double) 3 + 1 } 7.0 / 2 == 3.5 (double) 4 Software Design I (CS 120) 13 Software Design I (CS 120) 14 Safe & Unsafe Casting Explicit Casting } When we turn a narrow type into a wider one, this is } Sometimes, we want to force a loss of precision, e.g.: considered “safe” in Java } Rounding down numbers in calculations } Safety here comes from precision } Rounding up to ensure we have enough room for something } For example, since floating-point numbers have decimal places } To do so, we can cast one expression type to another and integers don’t, we don’t lose any information by turning an int into a double during execution of our code one, using the following syntax: } Going the other way can lead to problems, however (primitiveType) mathExpression } Possible loss of precision } Information “thrown away” } An example would be: } Compiler won’t allow this sort of thing: double dub = 76 / 8.0; // == 9.5 double num1 = 6.0; int num = (int) dub * 2; // == 18 int num2 = num1; // error! Software Design I (CS 120) 15 Software Design I (CS 120) 16 4
Precedence of Casting For This Week } Meetings this week: } In our example: } Monday, Wednesday: regular classroom double dub = 76 / 8.0; // == 9.5 } Tuesday, Friday: virtual labs int num = (int) dub * 2; // == 18 We get 18 because casting has higher precedence than any of } First quiz: by 5:00 PM, Friday 18 September (Canvas) the arithmetic operations } Thus, 9.5 is turned into an integer, 9 , before multiplication } Program 01 : available now } Must be careful to control casting properly, using parentheses } Due: Thursday 17 September, by 11:59 PM as needed to get the right results: } Readings : Chapters 1 & 2 int num = (int)(4.8 * 2); == 9 } Office Hours: online int num = (int) 4.8 * 2; == 8 } Monday/Wednesday/Friday: 11:00 AM–12:00 PM } Tuesday: 3:15 PM–4:15 PM int num = (int) 2 * 4.8; error! Software Design I (CS 120) 17 Software Design I (CS 120) 18 5
Recommend
More recommend