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

Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the...
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 ...
Author

Write A Comment