Data Types in Java

Data types represent the different values to be stored in the variable, for example if a variable has int data type, it can only take integer values.
In java, there are two types of data types:

  1. Primitive Data Types
  2. Non-Primitive Data Types

Primitive Data Types

In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double.
Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.

1) byte:

This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.

Example:
class JavaExample{
    public static void main(String[] args){
        byte num;
        num=123;
        System.out.println(num);
    }
}        
Output:

113

2) short:

This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.

Example:
class JavaExample {
    public static void main(String[] args) {
        short num;
        num = 150;
        System.out.println(num);
    }
}
Output

150

3) int:

‘int’ is used when short is not large enough to hold the number, it has a wider range: -2,147,483,648 to 2,147,483,647.

Example:
class JavaExample {
    public static void main(String[] args) {
        int num;
        num = 1245638;
        System.out.println(num);
    }
}
Output

1245638

4) long:

‘long’ when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Example:
class JavaExample {
    public static void main(String[] args) {
        long num = -12332252626L;
        System.out.println(num);
    }
}
Output:

-12332252626

5) double:

‘double’ is sufficient for holding 15 decimal digits.

Example:
class JavaExample {
   public static void main(String[] args) {
       
       double num = -42937737.9d;
       System.out.println(num);
   }
}
Output

-4.29377379E7

6) float:

‘float’ is sufficient for holding 6 to 7 decimal digits.

Example:
class JavaExample {
   public static void main(String[] args) {
       float num = 19.98f;
       System.out.println(num);
   }
}
Output:

19.98

7) boolean:

‘boolean’ holds only two values either true of false.

Example:
class JavaExample {
  public static void main(String[] args) {
      boolean b = false;
      System.out.println(b);
  }
}
Output:

false

8) char:

‘char’ holds only characters values.

Example:
class JavaExample {
  public static void main(String[] args) {
      
      char ch = 'Z';
      System.out.println(ch);
  }
}
Output:

Z

Non-Primitive Data Types:

Arrays and Strings are non-primitive data types, we will discuss them later in the coming tutorials.

Literals in Java

A literal is any constant value that we assign to a variable in a Program.

int num=10;

Here value 10 is a Integer literal that is assigned to the variable ‘num’.

char ch =A;

Here A is a char literal assigned to ‘ch’.

Integer Literals:

Integer literals are assigned to the variables of data type byte, short, int and long.

byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;

Float Literals:

Float literals are assigned to the variable of the data type float and double.

double num1 = 22.4;
float num2 = 22.4f;
NOTE: Always suffix float value with the “f” else compiler will consider it as double.

Char and String Literal:

These literals are used for char and String type variables.

char ch = 'Z';
String str = "BeginnersBook";

Related Post

C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop wit...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Variables Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable must be declared be...
Author

Write A Comment