| Primitive Data Types | At the data member level, all primitive data are initialized if no data is defined as follows:zero (0) for numbers.false for boolean.String to null. | ||||
| Unicode | char | null (‘u0000’) – Unicode for charchar = c =’W’char c3 = ‘u0057’ //Hexadecimal of W | |||
| char are unsigned data types | |||||
| Exam Tip | Character in unicode use the “” backslash | ||||
| Data type bit size | |||||
| booleanbyte (8 bits) | char (16 bits)short (16 bits)int (32 bits)long (64 bits) | float (32 bits)double (64 bits) | |||
| Range of Integer Values | byteshortintlong | -2×7 to 2×7-1-2×15 to 2×15-1-2×3 to 2×31-1-2×63 to 2×63-1 | (-128 to 127)-32768 to 32767(-2147483648 to 2147483647) | ||
| Formula for Integer range: -2(bits-1) to 2(bits-1) – 1 | |||||
| Octal, decimal or hexadecimal can be used to create integers.010 = 8 in octal – Octal starts with 0.0×100 = 8 in hexadecimal – Hexadecimal starts with 0x. | |||||
| Number Promotion | Promotion of numeric data types (top is highest order):doublefloatlongintcharshortbyteboolean | ||||
| Narrow(casting is needed, target is lesser value and data is lost)Widening(No casting, target is larger and no data lost) | |||||
| byte -> short -> char-> int -> long -> float -> double (from less to more data) | |||||
| Exam Tip | Promotion can occur in assignment, arguments and return types. | ||||
| Casting | Casting can be used to cast a general or higher type to a lesser or more specific type. | ||||
| widening (promotion) | Converting from a type of fewer bits to one with more bits is widening. Java implicitly widens primitive data type. No cast is necessary. | ||||
| Widening of numbers do not loose information. | |||||
| narrowing | Converting from a type of greater bits to one with less bits is narrowing. Casting is required. | ||||
| Narrowing loose information. | |||||
| Although char and short have the same size (16 bits).We can not do this:char ch = ‘u0054’;short s = ch;Char is an unsigned integer. The compiler will complain.
We need to cast: short s = (short) ch; |
|||||
| int x = 394.232 Casting is required to convert double to int.This code will not compile. | |||||
| Exam tip | Be careful, with the numeric ranges:Ex: int is (-2147483648 to 2147483647).The exam might give you (-2147483648 to 2147483648). | ||||
| Exam tip | By default floating numbers are double.Example: 0.0 is a double number.For a float use 0.0F suffix. | ||||
| Exam tip | Float f = 40.0;This is a compiler error.A double is being assigned to float.Casting is needed, or the F literal suffix must be used – 40.0F. | ||||
| Reference Data Types (Objects) | Reference variables are handles to objects in memory. Reference variables are initialized to null if and object is not initialized. | ||||
| Instance variables | Once a class is instantiated to an object the reference data members are instant variables. | ||||
| Class variables | If a reference variable is modified as static, the data members are static or class variables. The variable belongs to the class. | ||||
| No instance of the class is necessary for the class to be used. For instance variables, a class instance is necessary. | |||||
| Casting | Based on an object hierarchy, we can implicitly assign a subclass variable to a super variable. | ||||
| Widening | An object is automatically cast up the hierarchy. The casting from subclass to super (widening) is done automatically. | ||||
| Narrowing | We need casting to assign a super instance to a subclass variable in the same hierarchy. This is narrowing.We are going down the hierarchy. | ||||
| Base(Simple Idea) | We can assign a specific to a general when it come to object. We can not assign a general to a specific object. | ||||
| Subclass(More Specific Idea) | Component c;//Abstract can not be instantiatedButton b = new Button();c = bThis is legal, we are assigning a subclass to a super. This is a polymorphic behavior.b = c; //Error
To do this we need casting. b = (Button) c. |
||||
| Exam Tip | Assigning a subclass to the base is allowed and done without casting | ||||
| Exam Tip | Assigning a base class to the subclass needs casting. | ||||
| Exam Tip | Narrowing conversion of objects may produce runtime errors and not compile time errors.Component c;c = new Label(“Asds”); //Implicit cast at compile time ok//c has reference to Label//assign label to Button, wrong hierarchy// Component – Button, Label
Button b = (Button) c; //Explicit cast at runtime not ok, c refences a label This will compile fine. But at runtime an error will occur. Explicit cast are determined at runtime. |
||||
| Exam Tip | Object casting can not be done across an hierarchy | ||||
| Exam Tip | Methods may return the class or subclasses of the declared type. | ||||
| Arrays | Array’s are objects that can contain both primitive or object data elements. Their initialized data value are based on the data type describe above. | ||||
| Size of an array can not be changed. | |||||
| indices are used to access the elements in an array. Indices begin with 0. | |||||
| Initialize array | Use new operator: char a[] = new a[23]; | ||||
| One line initialization:int[] years = { 1993, 1994, 1995 };//The length of the array is three. | |||||
| Primitive variables are initialized to their default value for arrays. | |||||
| Objects are initialized to null for arrays. | |||||
| char a[]; or char[] a;a[] = new a[23];int n[] = {1, 2, 3, 4};n.length – no of elements //length is 4 | |||||
| The length of a 2 dimensional array is retrieved by: a[x][y];a.length – provides the length for the x array.a[0].length – provides the length for the y array. | |||||
| Exam Tip | Know about 2 dimensional arrays. | ||||
| Exam Tip | For local variables, their value must be initialized before usage. Including arrays. If part of the array has been initialized, then only that part can be used. | ||||
| Exam Tip | int [] seeds = new int[i];For a local variable, seeds get’s initialize to 0 since it is an array. | ||||
| String | String are immutable objects. They can not be changed. | ||||
| String can be assigned a value of null. | |||||
| Exam Tip | Primitive values can not be assigned a null value | ||||
| Wrapper | Each primitive data type as corresponding wrapper class in the java.lang package.Around the primitive, they wrap an object implementation | ||||
| These classes allow the creation of object types for the primitives. | |||||
| ByteShortCharacterInteger | LongFloatDoubleBoolean | ||||
| Exam Tip | The values in the wrapper class can not be changed. They are immutable. | ||||
| Exam Tip | Each of the integer wrapper classes have a respective parseByte, parseShort, parseInt, parseLong method.They take a respective String datatype and convert it to an byte, short or integer value.They can also take a respective String datatype with a radix and convert it to an byte, short or integer value. | ||||
| Exam Tip | The floating and double data type do not have a parse value. They do have a valueOf(String) method which returns the primitive datatype value for the string value. Note these integer wrapper class also have the valueOf(String) operator. | ||||