Data Types In Java

You may watch full clip here and remember to subscribe;

Watch now!

In today’s tutorial we will learn data types in Java language. We will define what are data types and main types of data types in Java giving out the relevant examples.

What are data types?

Data types -refer to an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. Whereas we can say data types specifies the values and sizes which can be stored in a variable. There are two types of data types in Java, look at the diagram below.

Now in this part we will discuss each type and subtypes giving examples.

Primitive data types -They are the building blocks of data manipulation in Java, they are the most basic data types available. They include; boolean, char, short, int, float, double, byte & long.

Non-primitive data types – these data types are nor predefined in Java, instead they are created by programmers. They include; Strings, Arrays, classes, interfaces etc.

Let’s discuss some of the differences between the primitive and non-primitive data types in Java language;

  • Primitive type always have a value while non-primitive can be null.
  • Primitive type starts with a lowercase letter while non-primitive starts with an uppercase.
  • Primitive data types are already defined in java while non-primitive data types are created by the programmer.
  • Non-primitive types can be used to call methods to perform certain operations while primitive types cannot.
  • The size of a primitive type depends on the data type while non-primitive type they all have the same sizes.

-There are 8 primitive data types in Java as we have seen above, in the figure below illustrates their sizes and descriptions.

Now let’s discuss each primitive data type with examples;

a) Int data type

Int data type it is a 32-bit signed two’s complement integer which is used to represent integral values unless if there is no problem about memory. The default value is 0, for the range values look at the above figure. See this example here;


int x =100000, int y =-200000

b) Long data type

Long data type is a 64-bit two’s complement integer, for the range values refer to above figure. It is used when you need a range of values more than those provided by int. Its default value is 0. See this example here;


long x =100000L, long y =-200000L

c) Float data type

Float data type is a single-precision 32-bit IEE 754 floating point. Its value range is unlimited, its actually recommended to be used instead of double if you need to save memory in large arrays of floating point numbers. The float data type should not be used for precise values such as currency. We have it’s default value as 0.0F, for range values refer to the above figure, see this example;


float f1 =234.5f

d) Boolean data type

Boolean data type is used to store two possible values, which are true & false. They are used to execute true/false conditions. This data type specifies one bit of information, but its size cannot be defined precisely, look at the example below;


Boolean one = false

e) Byte data type

Byte data type is a 8-bit signed two’s complement integer. It’s used to save memory in large arrays where memory savings is most required. It saves space since it is 4 times smaller than an integer, it can be used instead of a int data type, for the range values refer to the figure above. It’s default value is 0. See the example here;


byte x =10, byte y =-20

f) Short data type

Short data type is a 16-bit signed two’s complement integer. For the range values refer to the above figure, its default value is 0. It can also be used to save on memory space since it’s 2 times smaller than an integer. See this example;


short x = 10000, short y =-5000

g) Double data type

Double data type is a double-precision 64-bit IEEE 754 floating point. It’s value range is unlimited, it is just used for decimal values just like float. As we also discussed in float data type, double type should not be used for precise values, such as currency. Its default value is 0.0d. See this example;


double d1 =14.6

h) Char data type

Char data type is a single 16-bit Unicode character. It used to store characters, for the value-range refer to the above figure. See this example;


char letterK = 'K'

i) String data type

String is a non-primitive data type since it refers to an object, it is used to store texts. The string values should be surrounded by double quotes(“”). We will discuss all these in a later chapters, see the example here;


String car = "Mercedez";
System.out.println(car);


It outputs; Mercedez
Thanks for reading through our tutorial, hope you now understand various data types in Java. Remember to subscribe to our YouTube Channel, for live sessions and video tutorials. As we continue learning Java hoping to meet you in our next tutorial.