Variable and Data Types in Java

Variable and Data Types in Java

In this tutorial we will learn about Variable and Data Types in Java.

Variables are the named memory location in java, used to Store Data. And in general, Data type is the type of the variable.

There are three types variable in java according to scope:

  1. Local Variable
  2. Instance Variable
  3. Static Variable

And there are two Types of Data Type in Java:

  1. Primitive data type.
  2. Non primitive data types

Variable in Java

A variable is reserved memory location to store some value and keep it for later use. It is having a particular name and type.

Variable and Data Types in Java

Types of Variable in Java

There are three types of variable in Java:

  1. Local Variable
  2. Instance Variable
  3. Static Variable

Local Variable

A variable declared within a method definition, constructor, or block is called local variable.

Scope: Local variables are created when we enter into method, constructor or block and destroyed when we exit the method, constructor or block.

[pastacode lang=”java” manual=”public%20class%20variable%20%7B%0Apublic%20void%20show()%0A%20%7B%0A%20int%20a%3D3%3B%20%20%20%20%20%20%20%20%20%2F%2F%20local%20variable%0A%20System.out.println(a)%3B%0A%20%7D%0A%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20variable%20obj%3Dnew%20variable()%3B%20%20%20%20%20%2F%2F%20making%20object%20of%20the%20class%0A%20obj.show()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20using%20dot%20operator%20with%20obj%20to%20access%20show%20method%0A%20%20%0A%7D%20%0A%7D%20″ message=”A Java program containing Local Variable” highlight=”” provider=”manual”/]


Instance Variable

Variables declared inside a class and outside all the methods are called Instance variables. Memory of the instance variables are allocated during object creation and the memory is allocated on heap. Access modifier can be assigned to instance variable.

Scope : When object created with the use of new keyword then the instance variables are created and destroyed when object is destroyed.

[pastacode lang=”java” manual=”public%20class%20variable%20%7B%0A%09public%20int%20varInst%20%3D%203%3B%20%20%2F%2F%20instance%20variable%20with%20public%20access%20modifier%0A%0A%20%20%20%20public%20void%20evenodd()%20%7B%0A%09%09if%20(varInst%20%25%202%20%3D%3D%200)%0A%09%09%09System.out.println(%22Even%20number%22)%3B%0A%09%09else%0A%09%09%09System.out.println(%22Odd%20number%22)%3B%0A%09%7D%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09variable%20obj%20%3D%20new%20variable()%3B%0A%09%09obj.evenodd()%3B%0A%09%7D%0A%7D” message=”A java program using Instance Variable” highlight=”” provider=”manual”/]


Static Variable 

A variable declared with static keyword is known as static variable, it can be declared inside a class but outside all methods and constructors. It is also known as class variable.

Static variables are stored in static memory. Hence a single copy will be shared to all the objects of the class.

It can be used to refer the similar properties of the objects.

Scope: Static variables are created when the program starts and destroyed when the program stops.

[pastacode lang=”java” manual=”public%20class%20variable%20%7B%0A%09static%20int%20varStc%20%3D%202%3B%20%20%20%20%20%20%20%2F%2F%20static%20variable%0A%20%20%20%20public%20void%20show()%20%7B%0A%09%09var%20%3D%20varStc%20*%203%3B%20%20%20%20%20%0A%09%09System.out.println(var)%3B%0A%09%7D%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09variable%20obj%20%3D%20new%20variable()%3B%0A%09%09obj.show()%3B%0A%09%7D%0A%7D” message=”A Java program with Static Variable” highlight=”” provider=”manual”/]


Data Types in Java

Datatype refers to the the type of the variable. Broadly it can be categorizes in to parts:

  • Primitive data type (Intrinsic)
  • Non-Primitive data type (Reference or Derived)
    Variable and Data Types in Java

1. Primitive data types

There are 8 primitive types in Java:

Data Type Default Value Default size
boolean false 1 bit
char ‘\u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

Note: In java String is not data type, it is a class.

2. Non primitive data types

  • Object reference variable created in java is Non primitive.

Some Java Programs to understand the concept of Typecasting, Widening and Overflow

[pastacode lang=”java” manual=”class%20Test%7B%20%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%7B%20%20%0Aint%20x%3D10%3B%20%20%0Afloat%20f%20%3D%20a%3B%20%20%2F%2F%20the%20concept%20of%20Widening%0ASystem.out.println(x)%3B%20%20%0ASystem.out.println(f)%3B%20%20%0A%7D%7D%20%20″ message=”Example of Widening in Java” highlight=”” provider=”manual”/]

Output:

10
10.0

[pastacode lang=”java” manual=”class%20Test%7B%20%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%7B%20%20%0Afloat%20f%20%3D%2010.5f%3B%20%20%0A%2F%2Fint%20a%3Df%3B%2F%2FCompile%20time%20error%20%20%0Aint%20a%3D(int)f%3B%20%2F%2F%20Narrowing%20or%20Explicit%20Typecasting%20%0ASystem.out.println(f)%3B%20%20%0ASystem.out.println(a)%3B%20%20%0A%7D%7D%20%20″ message=”Example of Narrowing or Typecasting” highlight=”” provider=”manual”/]

Output:

10.5
10

[pastacode lang=”java” manual=”class%20Test%7B%20%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%7B%20%20%0A%0Aint%20a%3D130%3B%20%20%0Abyte%20b%3D(byte)a%3B%20%20%2F%2FOverflow%20%0ASystem.out.println(a)%3B%20%20%0ASystem.out.println(b)%3B%20%20%2F%2FDue%20to%20overflow%20it%20will%20not%20print%20130.%20Maximum%20range%0A%7D%7D%20%20″ message=”Example of Overflow in Java” highlight=”” provider=”manual”/]

Output:

130
-126



2 Replies to “Variable and Data Types in Java”

  1. Vaibhav

    Nice work. keep it continue . I am expecting more and more post. This post helps me lot to understand the variable concept.

Leave a Reply