Java - Data Types Data Types

What Is a Data Type?

A data type defines three components:

  • A set of values
  • A set of operations that can be applied to all values in the set
  • A data representation, which determines how the values are stored

What is a primitive data type?

A primitive data type consists of an atomic, indivisible value, and is defined without the help of any other data types.

What is User-defined data type?

User-defined data types can have primitive data types and other user-defined data types.

Typically, a programming language does not let the programmers extend or redefine primitive data types.

Java provides many built-in primitive data types, such as int, float, boolean, char, etc.

For example, the three components that define the int primitive data type in Java are as follows:

  • An int data type can have all integers between -2,147,483,648 and 2,147,483,647.
  • Operations such as addition, subtraction, multiplication, division, comparison, bit operation and many more are defined for the int data type.
  • A value of the int data type is represented in 32-bit memory in 2's compliment form.

You can give a name to a value of the int data type as

int count; 

This statement defines that count is of int type.

count is a name (an identifier).

It can have one value from the set of values that defines values for the int data type.

For example, you can associate integer 1 to the name count using an assignment statement like

count = 1; 

The following code shows how to declare variables of different data types using literals.


public class Main { 
        public static void main(String[] args) { 
                int anInt = 100; 
                long aLong = 200L; 
                byte aByte = 99; 
                short aShort = -902; 
                char aChar = 'A'; 
                float aFloat = 99.98F; 
                double aDouble = 999.89; 
                  /*from w  w w  . j ava  2 s.c  om*/
                // Print values of the variables 
                System.out.println("anInt = " + anInt); 
                System.out.println("aLong = " + aLong); 
                System.out.println("aByte = " + aByte); 
                System.out.println("aShort = " + aShort); 
                System.out.println("aChar = " + aChar); 
                System.out.println("aFloat = " + aFloat); 
                System.out.println("aDouble = " + aDouble); 



Related Topics