Variables and Data Types in Java

A variable in Java hold some data.

As Java is a strongly typed language, we need to declare the type of a variable and the variable can only hold a compatible type of data.

Declaring the type of a variable is like introducing the variable to your program, and you should only introduce once.

 

Declaration of a variable

The declaration of a variable begins with optional access modifier, the data type and is followed by the variable name and then a semicolon.

public int myVar;

  • Here, public is the access modifier and int is the data type of the variable named myVar.

  • Access modifiers tell which all code can access this member and access modifier public tell that this variable can be accessed from any other class. 

  • The data type is the type of value that a variable can hold. The data type may be a primitive data type or a reference data type.

 

Initialization

​Initialization along with declaration can be done as 

int myVariable = 5;

You can initialize variables from initialization blocks or through constructors.

 

Primitive type

  1. The data type may be a primitive data type or a reference data type. 

  2. A primitive data type is a basic type like int, float, double etc. and they hold a literal directly. 

  3. There are eight primitive data types defined in Java: boolean, byte, char, short, int, long, float, double. 

 

Example: Primitive type

int i=5;

  • The primitive type int (primitive type for integer) tells that this variable can hold only integers. 

 

Casting of types

  1. Compatible types can be implicitely or explicitely casted to each other.

  2. Except boolean, all other types are compatible with each other. A boolean can be assigned only a true or false or an expression that evaluate to true or false.

 

Example: Valid boolean assignments

boolean b1 = true;

boolean b2 = false;

boolean b3 = (3 < 5) ;

Example: Invalid boolean assignments

boolean b4 = 0;

boolean b2 = 1;

 

Reference type

  1. When an object is created using new keyword, enough memory is allocated in memory to hold all its members. A reference type variable holds the address of an object. 

Example: Reference type

Consider a class MyClass:

We can create two objects of the class MyClass and assign it to the reference type as:

public MyClass myObject1 = new MyClass();

public MyClass myObject2 = new MyClass();

Here MyClass is the reference type and myObject1 and myObject2 are reference type variables that refer to an object of type MyClass, returned by "new MyClass()".

 

Pass By value vs. Pass By Reference

  1. When you copy variables or when you pass a variable to a method in Java, always, the value is passed: Java will pass whatever the variable holds. 

  2. In the case of a primitive, Java passes the value of the variable and in the case of a reference type variable, it passes the address (reference) that is holds.

Example: Pass by value demonstration

myObject1.val=5;

myObject2 = myObject1;

myObject2.val = 10;

System.out.println(myObject1.val);

Output will be:

10

  • This is because, after the statement 'myObject2 = myObject1;' both reference type variables point to the same object in memory.

  • The object previously pointed by myObject2 doesn't have any reference to it and hence it will be eligible for garbage collection. 

 

Null Literal

Any reference type can be assigned with a special literal called null.

This means that the reference is not pointing to an actual object of its type.

If you try to invoke a method on a null object, you will get NullPointerException.

 

Exercise to increase your understanding

1. Create a class MyClass with one instance variable myInsVar and one static variable myStaVar and initialize both to 0.

class MyClass{

int myInsVar=0;

static int myStaVar=0;

}

2. In the main method of the same class, create two objects of MyClass.

MyClass obj1 = new MyClass();

MyClass obj2= new MyClass();

3. Increment both the variables on the objects as below:

obj1.myInsVar++;

obj1.myStaVar++;

obj2.myInsVar++;

obj2.myStaVar++;

3. First calculate the output looking at the below code (without actually executing) and then execute to see if your findings were correct:

System.out.println(obj1.myInsVar);

System.out.println(obj1.myStaVar);

System.out.println(obj2.myInsVar);

System.out.println(obj2.myStaVar);

Did you get the output as you expected? If yes, you have understood. Else remember that myStaVar is static and is common for all objects and there is only one copy per class, but there is a copy of myInsVar per object. Now try to understand it again. Still not able to understand, please feel free to ask me.

References and notes: 

You can read more about access modifiers at access-modifiers-and-inheritance-in-java-with-examples

Comments

need more exercise 

Was it useful?

do methods paramters come under local variables

Was it useful?

yes method parameters are local to the method in which they are declared.

Was it useful?

yes method parameters are local to the method in which they are declared.

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!