Variables
-
defaults for the variable types
- byte, short, int, long =
0 - float, double =
0.0 - char =
\u0000 - boolean =
false - reference =
null
- byte, short, int, long =
-
All
primitive local variables(stack allocated memory space) need to be initialized explicitly, during declaring or seperately before use, otherwise they will raise compile-time error when used. -
All
primitive array of local variables(heap allocated memory space) need to be allocated memory space usingnewoperator, otherwise the data elements will not be default to the default values and no allocation of the same will raise to compile-time error.
| Variable Type | Category | Example | Remarks |
|---|---|---|---|
| Local Variable | Primitive | int i; | Not initialized, throws compile-time error |
| Local Variable | Array of Primitives | int[] i; | Not initialized, throws compile-time error(no memory allocated) |
| Local Variable | Array of Primitives | int[] i = new int[1]; | defaulted elements to 0 |
| Local Variable | Wrapper | Integer i; | Not initialized, throws compile-time error |
| Local Variable | Array of Wrapper | Integer[] i; | Not initialized, throws compile-time error(no memory allocated) |
| Local Variable | Array of Wrapper | Integer[] i = new Integer[1]; | No Error, defaulted elements to null |
| Local Variable | objects | CustomObject i; | Not initialized, throws compile-time error |
| Local Variable | Array of objects | CustomObject[] i; | Not initialized, throws compile-time error(no memory allocated) |
| Local Variable | Array of objects | CustomObject[] i = new CustomObject[1]; | No Error, defaulted elements to null |
| Variable Type | Category | Example | Remarks |
|---|---|---|---|
| Instance/Static Variable | Primitive | int i; | Not initialized, no Error, Default value to 0 |
| Instance/Static Variable | Array of Primitives | int[] i; | Not initialized, no memory allocated, default value to null |
| Instance/Static Variable | Array of Primitives | int[] i = new int[1]; | No Error, defaulted elements to 0 |
| Instance/Static Variable | Wrapper | Integer i; | Not initialized, no memory allocated, default value to null |
| Instance/Static Variable | Array of Wrapper | Integer[] i; | Not initialized, no memory allocated, default value to null |
| Instance/Static Variable | Array of Wrapper | Integer[] i = new Integer[1]; | memory allocated, default elements value to null |
| Instance/Static Variable | objects | CustomObject i; | Not initialized, no memory allocated, default value to null |
| Instance/Static Variable | Array of objects | CustomObject[] i; | Not initialized, no memory allocated, default value to null |
| Instance/Static Variable | Array of objects | CustomObject[] i = new CustomObject[1]; | memory allocated, default elements value to null |
- Local Variables
- MUST be initialized before use, otherwise compile-time error
- created in the stack memory area, for references objects, the object lives in heap but the type lives in stack
- final is the ONLY modifier which can be used with local variables
- Instance Variables
- eligible for GC, once they go out of scope.
- scoped to object
- ONLY accessed/modified within a non-static block
- automatically initialized to default values
- if declared as final, MUST be initialized in decleration or constructor or instance initializer block
- Static Variables
- Global var, available to all programs running in the JVM instance
- loaded into memory, when the class loads, even before the object creation
- NOT participate or eligible for GC
- automatically initialized to default values
- if declared as final, MUST be initialized in decleration or static initializer block
- Order of Initialization : Static variable > Static Initializer block > Instance Variable > Instance Initializer block > Instance Constructor