Variables and Data types

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

Ex: int x =3;

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. public class VariableTypes {
  2.  
  3.     int x=3;//instance variable
  4.    
  5.     static int y=4;//static variable
  6.    
  7.     public void method() {
  8.          int z=4;//local variable
  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

	}

}


Datatypes in Java

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

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