Skip to main content

Variable

Variable

Variable is used to Store Data.
Types of Data Type:

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

1. Primitive data types

There are 8 primitive types

Name Size in Bits Range of Values
boolean 1 true or false
byte 8 -128 to +127
Char 16 ‘\u0000’ to ‘\uFFFF’
Short 16 -32,768 to +32,767
int 32 -2,147,483,648 to +2,147,483,647
Long 64 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float 32 approximately 10-38 to 10+38; 7 significant digits
double 64 approximately 10-308 to 10+308; 15 significant digits

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.
How to create and initialize variable in java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.spy.Variable;
 
public class Variable 
 {  
    public static void main(String[] args)    
    {        
 
     // variable declaration      
        int a;      
        float f;     
        double d; 
 
       //variable inialization 
 
         a=12;  
         f=2.3333f;   
         d=3.33333;       
 
         // printing value of variable     
          System.out.println(a);   
          System.out.println(f);   
          System.out.println(d);     
 
         System.out.println("********************");         
 
 
 
         char c = '\u0024';     
         System.out.println(c);
 
       System.out.println("*********************");  
 
         }
 
     }   
 
 
OUTPUT: 
12
2.3333
3.33333
******************** 
 $ 
*********************
  • Vaibhav

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

  • Bhavesh Lakhani

    Thanks vaibhav