Introduction to Java Primitives

March 28, 2022
Java Primitives



Overview

A data type specifies what type of data is to be stored in a variable. There are various data types offered in high-level programming languages including Java.

Java, being a statically-typed language requires all the variables to be declared before any value is assigned to them. The data type is supposed to be mentioned in the declaration process.

 

See this declaration statement:

int marks;

Here, ‘marks’ is a variable, and the data type of this variable is int (Integer). The int data type indicates that the marks variable can only store integer values.

There are 8 predefined data types in Java, commonly known as primitive data types. In this article, we will be discussing all primitive data types in Java with their appropriate examples.

Primitive Data Types in Java

Following are the 8 predefined or primitive data types in Java. These data types are classified based on the type of data each of them can store.

1. Primitive data type for logical values

– Boolean type

This primitive data type can store only two possible values, either true or false. The default value is always set as false.

The most common use of Boolean data type is to use it for Flag variables that are used to identify whether a task is executed or not.

For instance, to search for a value in an array, a variable of Boolean data type is often used to control the loop. It is switched to TRUE if the value is found, which stops the loop.

 

See this small example demonstrating a Java boolean data type declaration and assignment statement:

class Main {

  public static void main(String[] args)

    boolean isFound = false;

System.out.println(isFound);    // The output will be false.

  }

}

2. Primitive data types for INTEGER values

– Int type

The int data type can store an integer value ranging from -231 to 231-1 (32-bit signed two’s complement integer).

In Java 8 or later versions, you can also use an unsigned 32-bit integer. It extends the range for the positive numbers as the minimum value is 0 and a maximum value would be 232-1.

You should only use the signed version when you are sure about using only positive values like for storing the area of land or the distance between objects which can never be negative.

 

Here is a simple method for using the Java int data type:

class Main {

  public static void main(String[] args) { 

    int distance = 425050400;

System.out.println(distance);  // It will now output 42050400

  }

}

– Byte type

The byte primitive data type also contains integer values but it is limited to a specific range of numbers.

It cannot store any integer beyond -128 to 127 (8-bit signed two’s complement integer).

Due to this limit, the size of a byte type variable is also significantly smaller than an integer data type.

You must only use the byte type when you are certain that the value will be in this range at all times.

A value beyond this range assigned to a byte data type variable will result in an error.

 

See this Example demonstrating a Java byte data type:

public class Main {

  public static void main(String[] args) {

    byte num1 = 110;

    byte num2 = 100;

    if (num1 > num2) {

System.out.println("num1 is greater than num2");

    } 

  }

– Short type

The short data type in Java can also store an integer value but within a specific range. You can assign values from -32768 to 32767 (16-bit signed two’s complement integer) in a short type variable.

Contrary to its name, the short data type offers a wide range, especially when compared to byte type.

Still, it must only be used when you are not dealing with big integer values. In that case, you can go with other data types such as int or long data types (discussed later in the article).

 

Here is a small Example showing Java short data type:

class Main {

  public static void main(String[] args) {

    short temperature;

    temperature = -150;

System.out.println(temperature);  // output-150 on screen

  }

}

– Long type

The long data type in Java or most of the programming languages stores the biggest integer value possible. It can store a huge value from -263 to 263-1 (64-bit signed two’s complement integer).

Similar to int, you can use long type to store unsigned (positive) 64-bit integer If you are using Java 8 or later versions. It will allow you to store a minimum value of 0 and a maximum value of 264-1.

 

See this demonstration for Java long data type:

class LongExample {

  public static void main(String[] args) {

    long range = 14948147001L;

System.out.println(range);    // output 14948147001

  }

}

The ‘L’ at the end of ‘14948147001’ represents that this integer value is of a long type.

3. Primitive data types for REAL values

After discussing the primitive data types in Java to store integer values, Following are the two primitive data types used to store real values in Java.

– Float type

The float data type is a single-precision data type used to store 32-bit floating-point values.

The single-precision floating-point representation requires a 32-bit word as per IEE standards. These 32 bits represent different parts of the value. Starting from left,

  • The first bit is the sign bit, S,
  • The next eight bits are the exponent bits, ‘E’, and
  • The final 23 bits are the fraction ‘F’:

Java Primitives types

Here is a simple demonstration for Java float data type:

class Main {

  public static void main(String[] args) {                 

    float value = -13.2f;  

System.out.println(value);  // outputss -13.2

  }

}

The ‘f’ is used in the above example as -13.2 is a double literal. It tells the compiler to treat the value as a float rather than double.

– Double type

The double data type is used to store a 64-bit floating-point value. The default stored value is  0.0 also represented as (0.0d) where d stands for double. The reason it is called ‘double’ is that it offers double precision.

The term double-precision does not mean that the precision is doubled. The word double is used because a double-precision number uses the double number of bits than a float data type.

The extra bits not only increase the precision but also the range of value stored in a double type variable.

 

This is how you can easily use Java double data type in your code:

class Main {

  public static void main(String[] args) {                 

    double percentage = 85.33;

System.out.println(percentage);  // outputs -85.33

  }

}

4. Primitive data types for character values

like any other programming language, along with numbers and Boolean, text and characters are also assigned to variables in Java. Java offers a single primitive data type to store characters.

– Char type

Char data type is used to store a 16-bit Unicode character. The minimum value of the char data type is ‘u0000’ (0) and the maximum value of the is ‘uffff’ (�). The ‘u’ represents Unicode and the 4 characters represent a unique value in hexadecimal assigned to every character in Unicode.

 

See this example below:

class Main {

  public static void main(String[] args) {

    char alphabet = 'u0054';

    char number = '9';

    char alphabet2 = 65;

  

System.out.println(alphabet);  // outputs ‘T’

    System.out.println(number);  // outputs ‘9’

    System.out.println(alphabet2);  // outputs ‘A’

  }

}

The ‘u0054’ is the Unicode value for ‘T’, Hence, we get T as the output.

9 is also assigned to a char variable but it is assigned as a character, specified by single quotes.

This shows that char data type also supports the direct assignment of a character to a char variable instead of using its Unicode value.

The last variable, alphabet2 is assigned 65 and ‘A’ is printed as the output. This is because char can also be assigned the ASCII values and 65 is the ASCII value for ‘A’.

The strings do not make the list as it is not a Java primitive type instead it is an object and is used via java.lang.String class.

Also Read: Java Compiler Error: “class, interface, or enum expected”

Conclusion

The primitive data types are among the fundamentals of programming in Java.

A primitive data type is a mandatory part of any code hence a proper understanding of every Java primitive data type is quite essential for any Java developer.



author

jordan

Full Stack Java Developer | Writer | Recruiter, bridging the gap between exceptional talent and opportunities, for some of the biggest Fortune 500 companies.


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US