Search⌘ K
AI Features

Primitive Data Types: Part 2

Explore Java's primitive data types such as byte, short, int, and their default values. Understand the difference between primitive types and wrapper classes, and why local variables require initialization. This lesson helps you grasp essential concepts of Java data handling and variable initialization.

Coding example: 12

Let’s take a look at the code first.

Java
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 ...