QN002. Write a program which accept and print all basic data types of Java.
1. Objective
To see the use of all basic data types of Java.
2. Theory
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java.
- Primitive Data Types
- Non-Primitive Data Types
In this program we only look after primitive data types. Primitive data types are the building blocks of data manipulation. There are 8 types of primitive data types in Java.
| Data Type | Default Value | Default Size | 
|---|---|---|
| boolean | false | 1 bit | byte | 0 | 1 byte | char | '\u0000' | 2 byte | short | 0 | 2 byte | int | 0 | 4 byte | long | 0l | 8 byte | float | 0.0f | 4 byte | double | 0.0d | 8 byte | 
3. Source Code
      public class Qn002{ 
        public static void main(String[] args){
          //boolean 
          boolean b = true;
          System.out.println("boolean: " + b);
          
          //byte
          byte a = 127;
 
        
              // byte is 8 bit value 
              System.out.println("byte: " + a); 
          
          //char
          char ch = 'S';
          System.out.println("char: " + ch);
          
          //Short
          short sh = 306;
          System.out.println("short: " + sh);
          
          //int
 
          int num = 999;
          System.out.println("int: " + num);
          
          //long
          long lon = 25362814;
          System.out.println("long: " + lon);
          
          //float
          float fl = 4.1416197f;
          System.out.println("float: " + fl);
          
          //double
          double value= 25.264897456;
          System.out.println("double: " + value);
        }
      }
    
    4. Output
      boolean: ture
      byte: 127
      char: S
      short: 306
      int: 999
      long: 25362814
      float: 4.141697
      double: 25.264897456
    
5. Conclusion
In this way, I would like to conclude that we can use any of primite data types according to the type of data we want to store. For example, if we have to store integer number 50 byte would be the best choice for this. Because byte can hold integer value from -128 to 127.