Java: Primitive Data Types

Primitive Data Types

This Java video tutorial gives an overview of the primitive data types in Java. Primitive data types are the elements from which we can build up larger data types. As we mentioned, primitive data types are analogous the chemical elements in the periodic table. For future reference, we have posted of the table of the primitive data types, along with their properties.

Java Primitive Data Types

The table displays the eight Java primitive data types: byte, short, int, long, float, double, char, and boolean. These eight types can be categorized into four basic types: integers, floating-point numbers, characters, and booleans. The first four primitive types are integers: byte, short, int, and long. The next two types are floating-point numbers: float and double. The only character type is char, and the only boolean type is boolean.

The integer types are types that can hold numbers with no fractional part; the only difference between the four integer types is the amount of memory that they use and the range of values that they can represent. The floating-point types can store numbers with fractional parts; the only difference between the two floating-point types is, again, the memory that they use and the range that they can represent. Principally, we will use the int type for integers and the double type for floating-point numbers.

The character type, char is used to represent a single symbol, which can range from letters of virtually every known alphabet and numeric digits to symbols for mathematics and music. The boolean type is the simplest and is only used to represent the logical values true and false.

We illustrate the four basic data types with a program that uses four of our primitive Java data types. For this program, we created a project, as we did in Java Lesson 1, and added these eight lines of code to it.

        int MyInt = 65536;
        double MyDouble = 3.14159;
        char MyChar = 'X';
        boolean MyBoolean = true;

        System.out.print("MyInt = ");
        System.out.print("MyDouble = ");
        System.out.print("MyChar = ");
        System.out.print("MyBoolean = ");

The first four lines of this program create and assign literal values to each of the four types. In the next eight lines, we output the values preceded by string literals that specify which value it is. So, when we run this program, we see this

Java Program 1 Output

We can combine each adjacent pair of print commands to make the program more concise. To do this, we use a println command, along with a plus sign to join the string literal and the primitive data type value, like this

        int MyInt = 65536;
        double MyDouble = 3.14159;
        char MyChar = 'X';
        boolean MyBoolean = true;

        System.out.println("MyInt = " + MyInt);
        System.out.println("MyDouble = " + MyDouble);
        System.out.println("MyChar = " + MyChar);
        System.out.println("MyBoolean = " + MyBoolean);

If we execute this program, we see the same output as before.

Java Program 2 Output