Primitive Data Types: Part 1

Learn about primitive data types in Java and develop an understanding with the help of examples.

So far we have seen nine different examples, most of which have been integer values. We have also noticed one key thing at the time of declaring a variable, we write it like this: int speed = 0;.

This means we need to specify what type of variable it is. Stating the variable’s type and the name is essential because Java is statically-typed. This means that all variables should be declared before you can use it.

By naming and declaring a variable we actually declare the state of an object. In a Car class, when we declare a variable speed, our program knows that a field exists and that the field holds numerical data. It also has an initial value, such as 0 or 1.

The data type of a variable tells us what type of value it is. Because it represents the state of an object, we need to know what type of value it contains. Otherwise, we cannot change that state through a method. Basically, in a method, we do some kind of operation to change the state of that object.

Get hands-on with 1200+ tech skills courses.