Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Variables and Data Types in Java

Variable is a name of memory location. There are three types of variables in java: local, instance and static.

There are two types of data types in java: primitive and non-primitive.

Variable

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.


int x=50;//Here x is variable
double y=5.9;//Here y is variable


Types of Variable

There are three types of variables in java:

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

1) Local

A variable which is declared inside the method is called local variable.

2) Instance Variable

A variable which is declared inside the class but outside the method, is called instance variable . It is not declared as static.

Static variable

A variable that is declared as static is called static variable. It cannot be local. We will have detailed learning of these variables in next chapters.

Example to understand the types of variables in java


int x=50;//Here x is variable
double y=5.9;//Here y is variable


Data Types in Java

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

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



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

Why char uses 2 byte in java and what is \u0000 ?

It is because java uses Unicode system than ASCII code system. The \u0000 is the lowest range of Unicode system. To get detail explanation about Unicode visit next page.

Java Variable Example: local variable

package com.ashu;
public class Java64bit
{
  public void varAge()
  { //local variable age
    int age = 0;
  age = age + 10;
  System.out.println("Student age is : " + age);
  }
public static void main(String args[])
  {
    Java64bit obj = new Java64bit();
    obj.varAge();
  }
}


Output: Student age is : 10

Java Variable Example: Instance Variable


package com.ashu;
public class Java64bit {
  int x =100;   //instance variable, object created -> object destroyed
  int y =200;
  public void m1() {
    System.out.println(x);
    System.out.println(y);
}
public static void main(String[] args) {
  Java64bit s= new Java64bit();
  System.out.println(s.x);
  System.out.println(s.y);
  }
}


Output: 100  200

Java Variable Example: Static Variable


package com.ashu;
public class Java64bits {
   static int x =10; //static variable
   static int y =20;
  void m1() {
   System.out.println(Java64bits.x);
   System.out.println(Java64bits.y);
  }
public static void main(String[] args) {
     System.out.println(Java64bits.x);
     System.out.println(Java64bits.y);
    Java64bits s= new Java64bits();
   s.m1();
  }
}


Output: 10 20 10 20

Difference between Instance variable to Static variable


package com.ashu;
public class Java64bit {
  int a=10; //instance variable
  static int b=20; // static variable
public static void main(String[] args) {
Java64bit t= new Java64bit();
  System.out.println(t.a);
  System.out.println(t.b);
  t.a=999;
  t.b=888;
  System.out.println(t.a);
  System.out.println(t.b);
Java64bit t1= new Java64bit();
  System.out.println(t1.a);
  System.out.println(t1.b);
  t1.b=777;
Java64bit t2= new Java64bit();
  System.out.println(t2.a);
  System.out.println(t2.b);
  }
}


Output: 10 20,999 888,10 888, 10 777
Note : Instance var Every Object Create Separat Memory.
Static var WE can create any number of Object but Object is Overrride

Java Variable Example: Adding Lower Type


package com.ashu;
public class Java64bit {
public static void main(String[] args){
byte a=5;
byte b=15;
byte c=(byte)(a+b);
System.out.println(c);
}
}


Output:20

Copyright 2017 Design& development