CPSC 2150 - DAY 2 AUGUST 23, 2016 ================================================================================ Primitive/Reference Types and Value Semantics --------------------------------------------- Java contains 8 primitive types .boolean .byte .short .int .long .float .double .char Variable Declaration (Not automatically initialized if local variables) short index; boolean isDone = true; int counter = 3; float tip = cost * 0.15; Char vs. Short (char is signed, short is unsigned) When writing a long constant, use an upper case L long x = 13L; lowercase syntactically correct, but confusing For consistency prefer F to f. Every int is long, so long is a bigger type Heirarchy Double | Float | long | int | \ short char | byte Widening is automatic when needed (i.e. implicit) int i = 13; //no type conversion long x = 12; //int to long (widening) long y = i; //int to long (widening) Widening can be forced by an explicit cast int sum = 76; int count = 10; float average = sum/count; // no widening average = sum/(float)count; //int to float widening, result is 7.6 Narrowing requires EXPLICIT CAST Cast is a promise by program that the narrowing type conversion is ok May result in loss of information .casting float to int truncates decimals .casting long to int discards top bytes Assignment is a copy Parameters are copied Class types provided by: . Java standard libraries .string, integer, date, system .Programmer .Person, animal, savings, helloworldapp .Arrays .can contain primitive or reference types .int[], float[], String[] .indexed starting from 0 .just one literal null For reference types, the "value" in the memory location is a pointer to the actual object. Declaration binds the variable to a memory location which contains a pointer Explicit object creation with new() java.util.Date d = new java.util.Date(); Using Arrays ------------ An array type does not include the length int[] ids = new int[rostersize]; int searchRoster(int[] students) {...} Array Length Set at run time, cannot change after initialization. Avaliable as a property with .length Iteration: "foreach" loop (keyword is for) int sum = 0; for(int a: ids) sum+=a; float average = sum/(float)ids.length Set in Stone - can't change the size after initialized Assignment creates and Alias ---------------------------- Assignment copies the pointer accountB - $300 -| Points to same pointer, if you change one, it changes accountA - $300 -| the other accountB.deposit(150); accountB - $450 -| Same account balance because same accountA - $450 -| pointer, when deposit is made. For references p,q consider p == q .compares pointers for equality! .do they refer to the same object??? p = 0xFF (contains 4) q = 0xFF (contains 4) THEY ARE SAME POINTER SO THEY ARE EQUAL To test for equalty P = 0xAF (contains 4) Q = 0xFF (contains 4) .equals will return value, comparison can be made there Immutable Wrapper Classes ------------------------- Every primitive type has a corresponding wrapper class .int, long, float, double The classes are immutable .so no aliasing worries Do not provide a zero-argument constructor Do provide useful static constants Integer.MAX_VALUE, Integer.MIN_VALUE Do provide useful static methods Converting from string to primitive