Variables and Data type in Java

Variables in java are the graceful representation of memory address of the value.

Ex: int x =3;

Suppose below figure shows the memory address representation of the value stored as per the above statement.

Value ‘3’ is stored at the address 1000, this 1000 can be represented in the program as some variable.
In the above case it is x, so ‘x’ is a variable.

There are different types of variables in Java.

Local Variable : A variable that is declared inside any method.
It is called local because, it is accessible only within that method.

Instance Variable : A variable declared inside the class but not inside any method and it is not declared as static.

Static variable : A variable declared as a ‘static’ .

Let’s see an example to understand all the variables.

1
2
3
4
5
6
7
8
9
10
11
12
public class VariableTypes {
 
    int x=3;//instance variable
    
    static int y=4;//static variable
    
    public void method() {
         int z=4;//local variable
 
    }
 
}
public class VariableTypes {

	int x=3;//instance variable
	
	static int y=4;//static variable
	
	public void method() {
		 int z=4;//local variable

	}

}

Datatypes in Java

There are mainly 2 types of data types in Java
1)Primitive Datatype
2)Non Primitive Datatype

Primitive Datatype: Are those which are predefined by the Java language and are reserved by a keyword and they will not have any class for that, they are just reserved keywords.
There are 8 different primitive types supported in Java

Non Primitive Datatype : Are those which are defined with its class.
Ex:String

Note: Custom data types like Person,Employee are also non primitive datatype.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on