Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
8: Variables and Primitive Types
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
mineralWeightin 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.
There are no comments for now.