Know Your Variables — Chapter 3
2 min readApr 20, 2022
There are 2 types of variabels.
- Primitive — Holds fundamental values including integers, booleans, and floating point numbers.
- Object References — Holds references to objects.
Note:
Using of ‘f’ at the end of the value is needed for a variable to be explicitly declared as a float, since Java assumes every decimal value to be a double by default.
float f=32.5f;
Naming conventions for a class,method, variable
- It must start with a letter or an underscore (_), or dollar algn ($). You can’t start a name with a number.
- After the first character, you can use numbers as well.
- It shouldn’t be one of Java’s reserved words.
Object References
- There is no such thing as an object variable. (Only an object reference variable)
- It holds something like a pointer.
- Objects live on the garbage collectible heap.
- Performing arithmetic operations in Java(increments etc.) on a reference variable, is not possible unlike C.
- Unless an object reference is final, one Dog object could refer to another Dog object later.
- Objects can also be assigned as null.
A bit about being null
- null is a value.
- If there is only one reference to a particular object, then once its set to null (deprogrammed), the object is lost.
- Unlike primitives, every lost object goes into garbage collection(GC).
Arrays
- An array could also be considered an object reference as it holds the references to the primitives/object references inside of it.
- An array itself is never a primitive (regardless of what the array holds), it is always an object.
- Accessing an unassigned index on an array of objects will give null.
- Accessing a value within an unassigned index on an array of objects will give a runtime error.
Vocabulary
Implicit widening — Although you can’t put a bigger size variable’s value inside a smaller, you can put do other way around. This is done using implicit widening.