Once execution goes out of scope the variable is destroyed
They cannot be static variable
Local Variables are not initialized with default value as described in Datatypes
On the other hand, fields are initialized with the default values
class HelloWorld { public static void main(String[] args) { int x; // error: variable x might not have been initialized System.out.println(x); // compilation error fun(x); // compilation error } static void fun(int x) { // do nothing }}
Limits
Integer.MAX_VALUE
Testing datatype
Using reflection
a.getClass().getName()
Using instanceof
Can be used with class/sub class/parent class/interface
There is compile time type checking if the object and class are in the same hierarchy, else compilation fails
Remember: a Child cast to be a Parent, is still a Child.
In simple words, it checks if left object can be cast to right type successfully
// Syntaxobj instanceof Class// Example classesclass Shape {}class Circle extends Shape {}class Rectangle extends Shape {}// Testingvar circle = new Circle();var rectangle = new Rectangle();var shape = new Shape();Shape circleShape = (Shape) new Circle();System.out.println(circle instanceof Circle); // trueSystem.out.println(circle instanceof Shape); // true, Shape is super classSystem.out.println(shape instanceof Circle); // falseSystem.out.println(circleShape instanceof Circle); // trueSystem.out.println(circleShape instanceof Rectangle); // falseSystem.out.println(circle instanceof Rectangle); // compilation error
// up casting, child to parentShape circleShape = new Circle();// down casting, parent to child// must be done explicitlyCircle circle = (Circle) circleShape;
Memory considerations
All values of primitive types are stored in stack memory
Address of reference type is stored in stack memory itself but the address points to the address in heap memory
Wrapper Classes
Every primitive datatype has a corresponding wrapper class
Wrapper classes are final and immutable
Wrapper class objects can be null while primitives cannot be null
Collections require objects and can not work with primitives, hence wrapper classes help primitives to be used in collections
Never use == with wrappers since they are reference types. Always use equals() to check the equality of values. Check Integer Interning
Boxing: process of converting primitive to wrapper class
Unboxing: process of converting wrapper class to primitive
Autoboxing: automatically convert primitive to wrapper class or vice versa
int primitiveInt = 42;// BoxingInteger boxedInt = Integer.valueOf(primitiveInt);// Unboxing int primitiveInt = boxedInt.intValue(); // if boxedInt is null, then NPE// Autoboxing: Primitive -> WrapperInteger autoBoxedInt = primitiveInt;// Autoboxing: Wrapper -> Primitive// if boxedInt is null, then NPE// internally invokes `boxedInt.intValue()`int autoUnboxedInt = boxedInt;
Arithmetic Operations
Integer n1 = 50;Integer n2 = 10;// causes internal auto-boxing into primitive and then boxing// n1.intValue() and n2.intValue() is used internallyInteger result = n1 / n2; // autoboxing, if n2 == null, then NPE