...

/

Primitive Data Types: Part 2

Primitive Data Types: Part 2

Learn more about primitive data types in Java with the help of examples in this lesson.

Coding example: 12

Let’s take a look at the code first.

Press + to interact
class StatesOfAnObject {
byte age = 10;
byte length = 100;
int speedOfCar = 1_00_000;
void printState() {
System.out.println("The age is : " + age + ". The distance is : " + length +
". The speed of car is : " + speedOfCar);
}
}
public class ExampleTwelve {
public static void main(String[] args){
StatesOfAnObject state = new StatesOfAnObject();
state.speedOfCar = state.age + state.length;
state.printState();
}
}

Code explanation

Here, the state of the speed of the car represents int data types. The other two states, age and length, represent byte data types.

Coding example: 13

Before moving to the next chapter, let’s look at the final ...