Basic Data Types in Java with Example

Data types define the values and sizes that can be stored in a variable. There are basically two types of data types in java: 1) Primitive data types  2) Non-Primitive data types. Java is strongly or we can say that statically typed language that means the data type of a variable is known at compile time. This means that you must declare the variable before they can use it. In the previous tutorial, we learned about the variable, We learned how to create and declare a variable. In this tutorial, we will learn about the basic data types in java.


Basic Data Types in Java:

There are two types of data types in Java:
1. Primitive data types.
2. Non-primitive data types.
Basic data types in java


Primitive data types in Java:

In Java, there are 8 basic primitive data types: boolean, byte, char, short, int, long, float, double. These data types can be put into 4 groups.


i) Integer: This group includes byte, short, int, long.
ii) Floating-point: This group includes float and double.
iii) Character: This group includes char value.
iv) Boolean: This group includes boolean value.


1. boolean data type: 

The boolean data type has only two possible values either true or false. We can use this data type for simple flags that can track true/false conditions. This data type represents the one-bit information but the size of the boolean data type is dependent on a virtual machine.


Example of boolean data type:


class JavaStudyPoint { 
    public static void main(String args[]) 
    { 
        boolean b = true; 
        if (b == true) 
            System.out.println("Hi programmers!"); 
    } 
}


Output: 
 Hi Programmers! 


2. byte data type:

The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays. They are also useful when you are working with a stream of data from a network or a file.


Example of byte data type:


class JavaStudyPoint { 
    public static void main(String args[]) 
    { 
        byte b = 126; 
  
        // byte is 8 bit value 
        System.out.println(b); 
  
        b++; 
        System.out.println(b); 
  
        // It overflows here because 
        // the range of byte from -128 to 127 
        b++; 
        System.out.println(b); 
  
        // Looping back within the range 
        b++; 
        System.out.println(b); 
    } 
}


Output:

126
127
-128
-127


3. short data type:

The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.


Example of short data type:

class JavaStudyPoint {
    public static void main(String[] args) {
    	short num; //short data type.
    	num = 15000;
    	System.out.println(num);
    }
}


Output:
 15000 


4. int data type:

The most commonly used integer data type is int. It is a signed 32-bit type that has a range from -2,147,483,648 to 2,147,483,647. The variables of type int are commonly used to control loops and to index arrays.

Example of int data type:

 
class JavaStudyPoint {
    public static void main(String[] args) {
    	int num;
    	num = 15000000;
    	System.out.println(num);
    }
}

Output:
  15000000 


5. long data type:

The long is a signed 64-bit and is useful for those occasions where an int type is not large enough to hold the desired value. The range of long type is -2^63 to 2^63-1. Its minimum value is -2^63 and the maximum value is 2^63-1. This data type is useful when big, whole numbers are needed.


Example of long data type:

In this example, we compute the number of miles that light will travel in a specified number of days. 

class JavaStudyPoint{
  public static void main(String args[]){
     int lightspeed;
     long days;
     long seconds;
     long distance;

     //approximate speed of light in miles per second.
     lightspeed = 186000;
     days = 1000; // specify number of days.
     seconds = days * 24 * 60 * 60 //convert to seconds
     distance = lightspeed * seconds  // compute distance

     System.out.println("In " +days);
     System.out.println(" days light will travel about ");
     System.out.println(distance + " miles. ");
}
}


Output:
 In 1000 days light will travel about 16070400000000 miles.


6. float data type:

The float data type is a single-precision 32-bit IEEE 754 floating-point. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. This data type should never be used for precise values, such as currency.

Example of float data type:

class JavaStudyPoint {
    public static void main(String[] args) {
    	float num;
    	num = 20.67f;
    	System.out.println(num);
    }
}


Output:
 20.67 


7. double data type:

The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

Example of double data type:

In this example, we can use a double data type to compute the area of a circle.

class JavaStudyPoint{
  public static void main(String args[]){
     double pi, r, a;
     
     r = 10.8;  // radius of circle.
     pi = 3.14  // value of pi.
     a = pi * r * r; // compute area.
     System.out.println("Area of a circle is: " +a);
}
}


Output: 
  Area of a circle is: 366.2496 


8. char data type:

The char data type is used to store the character value. The char data type is a 
single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).


Example of char data type: 


class JavaStudyPoint{
  public static void main(String args[]){
     char ch1, ch2;
     
     ch1 = 88; // code for x;
     ch2 = 'Y';

    System.out.println("ch1 and ch2: ");
    System.out.prinytln(ch1 + " " +ch2);
}
}

Output:
 ch1 and ch2: X  Y. 


Reference: Java Official Documentation.


You may also like these posts:

1. Top 100 Java Interview Question and Answers.
2. Java Hello World Program with example.
3. How to set path in Java - Java Environment set up.
4. Java Hashtable with Example.
5. Java Dictionary with example.
6. Java Beans with Example.
7. Serialization in java with example.

Basic Data Types in Java with Example Basic Data Types in Java with Example Reviewed by Prashant Srivastava on December 13, 2019 Rating: 5

No comments:

Powered by Blogger.