Know Your Variables
Variables temporarily store the data in the computer’s memory and behave as containers. Variables must have a type and identifier(name). Variables are stored in the computer memory, and the variable name is assigned to its memory address.
Variable names must be compatible with the following rules.
- It must start with a letter, underscore (_), or a dollar sign ($). You can’t start a name with a number.
- After the first character, you can use numbers as well. Just don’t start it with a number.
- It can be anything you like, subject to those two rules, just so long as it isn’t one of Java’s reserved words
- A reserved keyword can not use as the variable name.
Variables are used as,
- instance variables
- local variables
- method arguments
- method return types
There are two types of variables in Java.
- Primitive types variables.
- Reference type variables.
Primitive types are used to store simple values, and reference types are used to store complex objects.
Java support type-safe variables. Integer-type variables can take decimal points and characters.
Types of primitive variables in Java.
- “boolean” type variables can have “true” and “false” values only.
- “char” type variables can have characters.
- Numeric variables can have different sizes of numeric values.
- The byte variables can not take integer variables. The compiler checks those incompatibilities in the compile time and gives errors because Java variables are type-safe variables.
- The int variable can hold integer values. Following the example, create a new variable and store 25 in a memory location.
Creating a new variable.
25 will be stored in the memory and the “age” reference is assigned with its memory address.
Copying primitive variables.
Copying a primitive variable will create a new variable and store that variable in another memory address.
A suffix must be added after the variable’s value when defining long values. Suffixes can be ‘l’ or ‘L’. Otherwise, the compiler thinks it is an integer value.
Also, when wringing floating-point variables, there should be a suffix as ‘for ‘F’. Otherwise, the compiler thinks it is a double value.
Reference type variables
Defining reference type variables must need to allocate memory. JRE allocate and release memory.
- First, declare a variable (“Dog” is the type of the variable and “myDog” is the name of the variable)
- Second, Create an object (allocating memory on the JVM heap.)
- Third, linking the variable and Object using the assignment operator (“=”).
When creating objects, JVM assigns the references to that objects. In this procedure, all references crated from one JVM have the same size. But each JVM might have a different way of representing references, so references on one JVM may be smaller or larger than references on another JVM.
Arithmetic operations can not be done with reference variables.
Examples
Creating two Book objects.
Book b = new Book();
Book c = new Book();
Creating a new reference as d and assigning it with c.
Book d = c;
Both b and c refer to the same Object.
Book c = b;
Assign the value null to variable c. Then c does not refer to anything.
c = null;
D is referring to b. Then there is no reference to object 2. The garbage collector removes that boject2 from the heap.
d = b;
Java Arrays
Arrays are used to store the same type of different variables together. Arrays are used because array values can be quickly accessed (fast random access for array elements).
Arry declaring can be done as follows.
int[] nums;
Arrays are reference type variables which means arrays are objects so we need to initialize them with a new keyword.
When initializing an array we need to define the size of the array. In the following example, the array size is 7.
nums = new int[5];
Arrays start with the 0th index.
Values of the array can be accessed and assigned as follows.
nums[0] = 10;
nums[1] = 20;
nums[2] = 30;
nums[3] = 40;
nums[4] = 50;
The “num” is an Object, and all assigned values are primitive (int) variables.
Once you’ve declared an array, you can’t put anything in it except things of the declared array type. (but integer array can store byte values as earlier mentioned.)