Skip to Content
Course content

8: Variables and Primitive Types

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Let's imagine we're starting to build a small RPG engine. I want to track a player's stats—things like health, experience, and whether they've found a secret key. I've got my main method ready to go, so let's just start throwing some data at the compiler and see where it pushes back.

Wait, why won't this decimal fit?

I'll start with the basics. Every character needs health. I'm thinking 100 points. In Java, I can't just say health = 100; the compiler needs to know what kind of data I'm storing. I'll use an int, which is short for integer.

int health = 100;
System.out.println("Health: " + health);

That works perfectly. But then I realize I want to implement a "poison" effect that drains health by 2.5 points per second. I'll try to update my health variable:

health = 97.5; // Error!

The compiler immediately yells at me. It says incompatible types: possible lossy conversion from double to int. Basically, Java is telling me, "You told me this was a whole number, and now you're trying to shove a decimal in there. I'm not just going to throw away the .5 without your permission."

To fix this, I need a type that handles decimals. I'll use a double. I prefer doubles over floats most of the time because they have double the precision, and unless you're working on a massive array of millions of numbers, the extra memory usage doesn't really matter.

double health = 100.0;
health = 97.5; 
System.out.println("Current health: " + health);

When numbers get ridiculously large

Now let's talk about experience points (XP). In my game, I want players to potentially earn billions of XP. I'll start with an int again because XP is usually a whole number.

int experience = 2147483647; 
System.out.println("XP: " + experience);

That's the maximum value an int can hold. But what happens if the player gains just one more point? I'll try to add 1 to it.

experience = experience + 1;
System.out.println("New XP: " + experience);

I expected 2,147,483,648. Instead, I get -2,147,483,648. This is called "integer overflow." It's one of those things that can crash a program or create a weird bug that's a nightmare to track down. The number essentially wrapped around to the lowest possible value.

If I know my numbers are going to be huge, I have to use a long. It's a 64-bit integer, meaning it can hold numbers so large you'll likely never hit the limit. One quirk: when I assign a value to a long, I have to put an 'L' at the end of the number to tell Java, "Treat this literal as a long, not an int."

long totalExperience = 2147483648L;
System.out.println("Corrected XP: " + totalExperience);

The binary choice and the single character

Not everything is a number. I need to know if the player has the "Golden Key" to open the final door. This is a simple yes or no. For this, I'll use a boolean.

boolean hasGoldenKey = false;
// Later in the game...
hasGoldenKey = true;

Finally, I want to give the player a "Rank" grade, like 'S', 'A', or 'B'. I don't need a full String for this—just one single letter. Java has a specific type for this called char. The important thing here is that char uses single quotes (' '), not double quotes (" ") which are for Strings.

char playerRank = 'S';
System.out.println("Your rank is: " + playerRank);

So, looking back at our exploration, we've found our toolbox: int for whole numbers, double for decimals, long for massive integers, boolean for true/false, and char for single characters. These are the "primitive types"—the basic building blocks that don't have fancy methods attached to them, but are incredibly fast and efficient.




📋 Practical Task

Build a Space Cargo Manifest

You are writing a small program to track a shipment of minerals on a spaceship. Create a Java class that declares and initializes the following variables to represent a single cargo container:

  • An integer representing the containerId (e.g., 5021).
  • A double representing the mineralWeight in kilograms (e.g., 125.75).
  • A long representing the marketValueInCredits (use a value larger than 3 billion).
  • A boolean representing isHazardous (true or false).
  • A character representing the storageZone (a single letter, e.g., 'B').

After declaring these, print each value to the console with a descriptive label (e.g., "Storage Zone: B"). Ensure you use the correct literal suffix for the long value to avoid compiler errors.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.