Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday, 13 December 2016

Variables and Data Types in java

Variable is name of memory location. In java there are three types of variables instance variables, local variables, static variables.

There are two types of data types in java

1) Primitive data types.
2) Non-Primitive data types.


Variable is name of reserved area allocated in memory. In other words, is name of memory location.

For example:

(1) int v = 20;   

Here v is a variable, Which holds the value 20..

(2) double d = 30;

Here d is a variable, Which holds the value 30.

Variable declaration

  • int a;
  • int a, b, c;

Variable initialization

  • int a = 10;
  • int a = 10, b = 20, c = 30.

Types of Variable

There are three types of variables in java:
  1. Instance Variable.
  2. Local Variable.
  3. Static Variable.

Instance Variable

Instance variable is a variable , which is declared inside a class but outside of method is called instance variable. Instance variable gets memory at run time when object is created.

Local Variable

Local variable is a variable , which is declared inside of method is called local variable.

Static Variable

Static variable is a variable , which is defined by the static keyword is known as static variable. static variable cannot be local variable. Static variable belongs to class level variable.

Let's see the example in program

class TypesOfVariableExample
int a=10; //instance variable
static int b=20; //static variable
void show()//method declaration
int c=30; //local variable

We can define a variable in two location in a class. For example

class Demo
int a;//class level variables
int b;//class level variables
public static void main(String args[])
int c;//Local variables
int d;//Local variables

Data Types In Java

Data Types: data types represent the different  values to be stored in the variable. In java there are two types of data types.

(1) Primitive data type.

(2) Non-Primitive data type.

Variables And Data Types In Java

Primitive Data Type

(1) byte

Default  Size - 1 Byte

Default  Value - 0
Range : -128 to 127

For Example:

byte b=10;

(2) short -

Default  Size - 2 Byte

Default  Value- 0
Range : -32768 to 32767 

For Example:

short s=10;

(3) int

Default  Size - 4 Byte

Default  Value- 0
Range : -2147483648 to 2147483647

For Example:

int i=10;

(4) long

Default  Size - 8 Byte

Default  Value- 0L
Range : -9223372036854775808 to 9223372036854775807

For Example:

long l=10;

(5) char -

Default  Size - 2 Byte
Default  Value- '\u0000'
Range : 0 to 65536 (Unsigned) 

For Example:

char c='a';

(6) boolean -

Default  Size - 1 bit

Default  Value- false
Range : true/false

For Example:

boolean b=true or false;

(7) float -

Default Size - 4 Byte

Default Value - 0.0f
Range :  approximately (+-) 3.40282347E+38F(6-7 significant decimal digits) Java implements IEEE 754 standard

For example :

float f=2.3f;

(8) double -

Default Size - 8 Byte

Default Value - 0.0d
Range :  approximately (+-) 1.79769313486231570E+308(15 significant decimal digits)

For example :

double d=8.5d;

Note:There is 1 byte=8 bit;

Non-Primitive or reference or object Data types

What is signed and unsigned in java 

Signed(+ or -): signed data types can hold both positive and negative values.

Unsigned(+): unsigned data types can hold large positive values but can't hold negative values.

For example:
A signed byte can represent values -128 to 127, while an unsigned byte can represent values from  0 to 255.

👉: In java all the data types or data types values are signed(except char)



  1. Please continue this great work and I look forward to more of your awesome blog posts

    Android Training
    PHP Programming Training

  2. Nice article keep this great work up. There is good java variable tutorials Click Here

  3. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Core Java Online Training Hyderabad

  4. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. Keep up the good work.
    Visit us: Java Online Training Hyderabad
    Visit us: Core Java Online Course
    Visit us: java course
    Visit us: Best Online Java Course


  5. This post is so interactive and informative.keep updating more information...
    Career In Java
    Purpose Of Java

  6. Nice blog! Your blog unique and very informative for everyone.
    Visit us: Java Online Training Hyderabad
    Visit us: Core Java Online Course

  7. Hi, I read your whole blog. This is very nice. Good to know about the career in qa automation is broad in future. We are also providing various Java Training, anyone interested can Java Training Online for making their career in this field .


Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts