Java Datatypes :-

Java datatypes are two types:-

1)    Primitive
2)    Non primitive

Primitive Datatypes :-

1.    byte

2.    short

3.    int

4.    long

5.    float

6.    double

7.    char

8.    bool

 

1)       byte

 byte is to store the values in the range of -128 to -127.

Ex:
 
          public class num
{
          public static void main(String[] args) {
              byte number=126;
                    System.out.println(number);
          }

}

OUTPUT :

          126

2)          short

short datatype is to store the values in the range of -32768 to 32767.
       Ex:
                    public class num
{
          public static void main(String[] args) {
              short number=3278;
                    System.out.println(number);
                    }
}

Output:

3278

 

3)    int

 int datatype is to store the values in the range of -2147483648 to 2147483647.
Ex: public class num
{
                    public static void main(String[] args) {
                                int number=2225555525;
                              System.out.println(number);
                              }
}

Output:

2225555525


4)    long

long is to store larger value
ex:
public class Num
{
          public static void main(String[] args) {
              long number=3274789658985548;
                    System.out.println(number);
          }
}                                       output: 3274789658985548

      

5)    float

  float is to store decimal numbers

ex:

          public class Main
{
          public static void main(String[] args) {
           float value2 = 9.87f;
        System.out.println(value2);
}
}

o/p: 9.87

 

6)    double

ex:

public class number

{

public static void main(String[] args) {

double num=5.5; // double num=5.5d;

        System.out.println(num):

}

}

 

 

o/p:
5.                5
 

 

 

 

7)    char

 

public class num
{
          public static void main(String[] args) {
              char var='a';
                    System.out.println(var);
          }
}

 

o/p: a

8)    bool

 true / false

 
public class Example
{
          public static void main(String[] args) {
              boolean num = true;
boolean alpha = false;
System.out.println(num);     // Outputs true
System.out.println(alpha);
          }
}
 

o/p:    true

          false

 

Non Primitive datatypes:

     1.    classes
2.    interfaces
3.    Arrays

1)                    1) Classes & objects:

 Class is a java userdefined datatype.it is created by the user . it consists of member variable and methods.
An object is the variable of the class, which can access the member variables and methods from class.
Ex:
public class Main{ 
        int a = 20; 
        int b = 10; 
        int c; 
        public void add () { 
            int c = a + b; 
            System.out.println("Addition of numbers is: " + c); 
        } 
        public void sub () { 
            int c = a - b; 
            System.out.println("Subtraction of numbers is: " + c); 
        } 
        public static void main (String[] args) { 
        // creating the object of class 
        Main obj = new Main(); 
 
        // calling the methods 
        obj.add(); 
        obj.sub();
}
}

o/p:

          Addition of numbers is: 30
Subtraction of numbers is: 10.
 

          2)interfaces:

An interface is just like java class, but it only has static constants and abstract method. All methods in an interface are implicitly public and abstract.
          Ex:
interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}

o/p:

Interface Method Implemented

 

Ex2:

interface Animal {
  public void animalSound();
  public void sleep();
}
class Pig implements Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
  public void sleep() {
    System.out.println("Zzz");
  }
}
class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}
o/p:
The pig says: wee wee
Zzz
 
3) Arrays
          An array is a data type which can store multiple homogenous variables i.e., variables of same type in a sequence. They are stored in an indexed manner starting with index 0. The variables can be either primitive or non-primitive data types.
Ex:
import java.io. * ; 
import java.util. * ; 
 
public class Main { 
  public static void main(String[] args) throws IOException { 
    int i; 
    Scanner ss = new Scanner(System. in ); 
    int arr[] = {1, 2, 3, 6, 9}; 
    int arr1[] = new int[5]; 
    System.out.println("Enter the numbers (size = 5) :"); 
    for (i = 0; i < 5; i++) { 
      arr1[i] = ss.nextInt(); 
    } 
    System.out.println("Previous array with initialized size is: "); 
    for (i = 0; i < 5; i++) { 
      System.out.print(arr[i] + " "); 
    } 
    System.out.println("\nThe new array we have entered is:"); 
    for (i = 0; i < 5; i++) { 
      System.out.print(arr1[i] + " "); 
    } 
  } 
} 


    Type Casting

                    It is used to convert one datatype to other.
          We have two types of type coversions:
                    1) widening casting
                    2) Narrowing casting

                                1) Widening Casting :[automatically]

          Converting a smaller datatype to larger datatype.
v Byte Ã  short Ã  char Ã  int Ã  long Ã  float Ã  double
v Ex:
public class wideCast
{
          public static void main(String[] args) {
              int num=9;
              double mynum=num;
              System.out.println("before :"+num);
                    System.out.println("after:"+mynum);              
          }
}
 
o/p:
before :9
after:9.0
2) Narrow Casting :[manually]
          Converting a large datatype to a smaller datatype.
v Double Ã  float Ã  long Ã  int Ã  char Ã  short Ã  byte
v Ex:
public class Main
{
          public static void main(String[] args) {
              double mydouble=9.78d;
              int myint=(int) mydouble;
              System.out.println("before:"+mydouble);
                    System.out.println("after:"+myint);
                   
          }
}
 

o/p:

before:9.78

after:9

 

 

Range of datatypes:

          We can find range of datatypes by using formula= 2n-1 to 2n-1-1

                    Here, n is number of bits.

§  Byte has 8 bits

i.e., n=8

28-1 to 28-1 -1  (i.e., -128 to 127)

§  Short  has 2 bytes = 16 bits

i.e., n=16

216-1 to 216-1 -1  (i.e., -32768 to 32767)

§  integer has 4 bytes = 32 bits

i.e., n=32

232-1 to 232-1 -1  (i.e., -2147483648 to 2147483647)

§  long  has 8 bytes = 64 bits

i.e., n=64

264-1 to 264-1 -1  (i.e., -92233472036854775808 to 92233472036854775807).