Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Variable and Data type in Java language

Posted on January 15, 2018January 11, 2020

Table of Contents

  • Variable and Data type in Java
    • Datatype
    • Variable and Data type in Java language
    • Primitive Data types in Java

Variable and Data type in Java

In this article, we will discuss  the concept of Variable and Data type in Java

Datatype

The data type and variables are an important concept in every programming language. A variable can be similar to a container, which holds the value of your program in Java. Every variable in Java has a data type that is identical to the compiler: what type of variable it and what type of data store it

Variable and Data type in Java language

There are two types of data type in Java

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

Classification of data type in Java


Variable

The variable is used to allocate and name the memory location to hold data.

To identify the storage memory location, each variable should be provided with a unique name.

Declaration

int a;
float b;
String name;
char letter;

Initialization

int a=10;   //Here a is a integer type ,which takes the value 10
float b=24.67f; //Here a is a floating point type ,which takes the value 24.67
String name="kamalan"; //Here a is a String type,which takes the String "kamalan"
char letter='a'; //Here a is a character type ,which takes the char "a"

Variables in memory location

"<yoastmark

  • a is a memory address
  • 10 is the value of the variable

Java has three types of variable

  1. Local
  2. Instant
  3. Static

Local

The local variable is a variable that is declared inside the method. It is called the local variable.

Instant /Global

The global variable is a variable that is declared inside the class but outside of the method. It is called instant variable/global variable

Static

A static variable is a variable that is declared as static using the static keyword,. It is called the static variable. It cannot be local.

Declaration and initialization

We can define a variable at two location  inside the class.

for Example

Inside the class but outside the method – Global  or Instance
Inside the class and inside the method – Local

Declare local and global variable inside the class

Variable and
Example of Variable and data type

Example

Program 1

Variable declaration and displays its value in Java

 

Output
10
23.33
12.5
Kannan
A

Program 2

Addition of Two numbers using variable

Output
Total is :25

Primitive Data types in Java

1. Data type – boolean

The boolean data type has two posible values true and false
Default Value – false
Default Size – 1 bit
Range – True or false
    For example – boolean b=true

program

class BooleanEx{
public static void main(String args[]){
boolean isTrue=true;
System.out.print(isTrue);

}

}

When the above code is executed, The following output is displayed

true

 

2. Data type – char

it,s 16 bit unicode charactor
Default Value – ‘u0000’
Default Size – 2 byte
For example – char c=’A’

Program

class CharEx{
public static void main(String args[]){
char letter='A';
System.out.print(letter);

}
}

When the above code is executed, The following output is displayed

A

 

3. Data type – byte

It is used to apply int type data type to save memory
Default Value – 0
Default Size – 1 byte
    Range – –126 to 127
For example – byteb=15;

Program

class ByteEx{
public static void main(String args[]){
byte num=124;
System.out.print(num);

}
}

When the above code is executed, The following output is displayed

124

4. Data type – short

It is used to apply int type data type to save memory
Default Value – 0
Default Size – 2 byte
    Range –  –32768 to 32767
For example – short s=1000;

Program

class ShortExample1{
public static void main(String args[]){
short num=1234;
System.out.print(num);

}
}

When the above code is executed, The following output is displayed

1234

 

5. Data type – int
Default Value – 0
Default Size – 4 byte
Range –  –2147483648 to 2147483647
For example – int i=100;

Program

class Int_Ex1{
public static void main(String args[]){
int num=12345678;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

12345678

6. Data type – long
Default Value – 0L
Default Size – 8 byte
Range –  –9223372036854775808 to 9223372036854775807
For example – long l=100;

Program

class Long_Ex1{
public static void main(String args[]){
long num=-1234567800L;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

-1234567800

 

7. Data type – float
Default Value – 0.0f
Default Size – 4 byte
Range – 
For example – float f=34.5f;

Program

class Float_Ex1{
public static void main(String args[]){
float num=234.56f;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

234.56

8. Data type – double
Default Value – 0.0d
Default Size – 8 byte
    Range –  –32768 to 32767
For example – double d=56.78d;

Program
class Double_Ex1{
public static void main(String args[]){
Double num=3433.56;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

3433.56

Non-primitive data types

  1. Array
  2. String

Related post

Keyword in C language

Variable type in Java language

Data type in Python language

Operator in Java language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes