Variables in Java

Gawesh Prabhashwara
4 min readMay 1, 2022

--

Head Fisrt Java Edition ii Chapter iii,

There are two types of variables.

  • Primitive
  • reference

*Variables are utilized in two different ways. They exist in two forms: as an object state (instance variable) and as a local variable.

*When we declare a variable, we must consider the kind of variable. Because Java takes into account the variable’s type. If you put a floating point number in an integer variable, for example, the precision is lost unless you tell the compiler otherwise ( everything after decimal point). Variables must also have a name.

Structure of Variable

Primitive Variables,

  • Integers, Booleans, and floating point numbers are among the primary values it keeps (simple bit pattern). Primitive variables come in a variety of sizes, each with its own name.
  • The following table shows the sizes of the primitive data types in Java.
Primitive data types and sizes

*The rules for naming variables, classes, and methods are as follows:

  • Number can be used after the first character. However, do not begin with a number.
  • A number cannot be used to begin a name.
  • A letter, underscore, or dollar sign must be the first character.
  • It can be anything you want without using the reserved java terms (reserved words are like public, static ,int ,void).
  • Check to see if the value will fit into the variable.

Value can be stored in a variable.

  • You can’t put a great amount of money into a small amount of money. Because if you choose to do so, you will lose some. There will be a lot of spillage. If the compiler detects that anything won’t fit in the variable from your code. It will make every effort to assist you in avoiding it.

What about a reference to an object?

  • variable variables An object reference is a collection of bits that represent a method of accessing an item. Rather of the object itself, the object reference holds something akin to a pointer. An address is also an option. Except we have no idea what’s inside a reference variable in Java. Whatever it is, we know it represents just one thing. And the JVM understands how to use the reference to access the object.
  • The object reference variable is similar to a remote control for an object.

*The 3 steps of object declaration, creation and assignment.

  1. Creating a variable to be used as a reference.
  2. Object creation.
  3. Creating a link between the item and the reference.

Garbage collection life on Heap,

*Two variables for book references should be declared. In the scene, add two new Book objects. Assign the Book objects to the reference variables. The two Book items have now taken up residence on the heap.

2.Book d = c;

*The things that c and d relate to are the same. The c and d variables store two separate copies of the same value.

3.c = b

*Both b and c are referring to the same thing.

4. c = null;

*The value null is assigned to variable c. As a result, c is a null reference. It’s an idiom that means “doesn’t refer to anything.”

What is the best way to express arrays in Java?

*Declare an array variable with the value of an int. An array variable is a remote control for an array object.

ex: int [] numbers;

2.Create a new int array with a length of 7 and assign it to the previously specified int[] variable “numbers.”

numbers = new int [7] ;

3. Assign an int value to each entry of the array.

numbers[0] =2;

numbers[1] =4; … like this pattern.

*Arrays are always objects, whether they’re designated to carry primitives or object references. To hold primitive values, though, you can declare an array object. To put it another way, a primitive element can exist in an array object, but an array is never a primitive.

Make a reference type array.

*To begin, make a variable with the array type you want. Second, allocate memory for the array with new and assign it to the array variable.

*We don’t have a variable name for the Dog while it’s in an array.

*Rather, we use array notation and press the remote control button (a dot on an item at a specific Index (position) in the array:

References,

[1] Sierra, Kathy, and Bert Bates. “Head First Java™ Second Edition.” (2021).

--

--