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 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


myObject2 = myObject1;

myObject2.val = 10;


Output will be:


  • 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:





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





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


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!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)