Java Primitive Data Types

The term data type refers to the type of data that can be stored in a variable.

Java is a strongly typed language because when you declare a variable, you must specify the variable’s type.

The compiler ensures that you don’t try to assign data of the wrong type to the variable.

For example, we cannot assign a string such as “” to an int type variable.

The following example code generates a compiler error:

int x; 

x = 3.1415;

Because x is declared as a variable of type int, which holds whole numbers, we can’t assign the value 3.1415 to it.

Primitive types vs reference types

Java makes an important distinction between primitive types and reference types.

  • Primitive types are the data types defined by the language itself.
  • Reference types are types defined by classes in the Java application programming interface (API) or by classes you create rather than by the language itself.

The memory location associated with a primitive-type variable contains the actual value of the variable.

The primitive types are sometimes called value types.

The memory location associated with a reference-type variable contains an address that indicates the memory location of the actual object.

Note, Java array, which can hold multiple occurrences of primitive- or reference-type variables, is a reference type.

Primitive types

Java defines eight primitive types of data:

  1. byte,
  2. short,
  3. int,
  4. long,
  5. char,
  6. float,
  7. double, and
  8. boolean.

The primitive types are referred to as simple types.

These can be put in four groups:

IntegersThis group includes byte, short, int, and long, which are for whole-valued signed numbers.
Floating-point numbersThis group includes float and double, which represent numbers with fractional precision.
CharactersThis group includes char, which represents symbols in a character set, like letters and numbers.
BooleanThis group includes boolean, which is a special type for representing true and false values.

They form the basis for all other types of data that you can create.

The primitive types represent single values-not complex objects.

Although Java is otherwise completely object-oriented, the primitive types are not.

They are analogous to the simple types found in most other non-object-oriented languages.

The primitive types are defined to have an explicit range and mathematical behavior.

Because of Java’s portability requirement, all data types have a strictly defined range.

For example, an int is always 32 bits, regardless of the particular platform.

Java defines a total of eight primitive types, listed in the following table.

Of the eight primitive types, six are for numbers, one is for characters, and one is for true/false values.

intA 32-bit (4-byte) integer value
byteAn 8-bit (1-byte) integer value
shortA 16-bit (2-byte) integer value
longA 64-bit (8-byte) integer value
floatA 32-bit (4-byte) floating-point value
doubleA 64-bit (8-byte) floating-point value
charA 16-bit character using the Unicode encoding scheme
booleanA true or false value

Java Declarations for Primitive and String Types

public class Main { 
    public static void main (String[] args) { 
        boolean BooleanVal = true;  /* Default is false */ 

        char charval = 'G';     /* Unicode UTF-16 */ 
        charval = '\u0490';     /* Ukrainian letter Ghe(?) */ 

        byte byteval;       /*  8 bits, -127 to 127 */ 
        short shortval;     /* 16 bits, -32,768 to 32,768 */ 
        int intval;         /* 32 bits, -2147483648 to 2147483647 */ 
        long longval;       /* 64 bits, -(2^64) to 2^64 - 1 */ 

        float   floatval = 10.123456F; /* 32-bit IEEE 754 */ 
        double doubleval = 10.12345678987654; /* 64-bit IEEE 754 */ 

        String message = "Darken the corner where you are!"; 
        message = message.replace("Darken", "Brighten"); 

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Variables and Data Types

Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


Leave a Reply

Your email address will not be published.