Time to explore 2 - Data Type Examples 😎
📊 Examples with Numbers and Strings
In this lesson, we’ll learn about different data types in Java — these are the kinds of values your robot can use, like numbers, letters, and words. Each type has its own special rules and powers. Let’s check them out!
🔢 Integers (int)
Integers are whole numbers. That means they don’t have decimals. You can use them to count things, measure scores, or store positions.
int score = 10;
int lives = 3;Java automatically uses integer math when you’re working with just int numbers.
| Operation | Code | What It Does | Result |
|---|---|---|---|
| Addition | int sum = 5 + 2; | 5 plus 2 | 7 |
| Subtraction | int diff = 5 - 2; | 5 minus 2 | 3 |
| Multiplication | int product = 5 * 2; | 5 times 2 | 10 |
| Division | int quotient = 5 / 2; | 5 divided by 2, decimal gets cut off | 2 |
| Remainder | int rem = 5 % 2; | What’s left after dividing 5 by 2 | 1 |
📝 Note: If you divide
5 / 2usingint, you don’t get 2.5 — Java chops off the decimal and gives just2.
🧮 Decimal Numbers (double)
A double can store decimal numbers like 2.5 or 3.14. It’s great for when you need precise values, like distances or percentages.
double speed = 2.5;
double batteryLevel = 75.5;| Operation | Code | What It Does | Result |
|---|---|---|---|
| Multiplication | double r = 4.5 * 1.2; | 4.5 times 1.2 | 5.4 |
| Order of Ops | double r = 4.5 * 1.2 + 3; | Multiply first, then add | 8.4 |
| Division | double d = 5.0 / 2.0; | Divides with decimals | 2.5 |
✅ Reminder: To get a decimal answer, at least one number must have a
.0in it! If both numbers areint, you’ll get anintresult.
🔤 Characters (char)
A char (short for character) stores a single letter or symbol. It’s surrounded by single quotes like 'A' or '%'.
char grade = 'A';
char symbol = '!';| Operation | Code | What It Does | Result |
|---|---|---|---|
| Assign letter | char letter = 'A'; | Stores letter A | 'A' |
| Next letter | char next = (char)(letter + 1); | Adds 1 to A (ASCII code) = B | 'B' |
| Symbol | char symbol = '!'; | Stores exclamation mark | '!' |
💡 Fun Fact: Letters are secretly stored as numbers (ASCII).
'A'is65,'B'is66, and so on!
🧵 Strings
A String is a bunch of characters stuck together — like a word or sentence. You use double quotes for strings: "Hello"
String name = "Robo";
String message = "Hello, world!";| Operation | Code | What It Does | Result |
|---|---|---|---|
| Basic join | "Hello" + " World" | Joins the two parts together | ”Hello World” |
| Add variable | "Hello, " + name | Adds the value inside name | ”Hello, Robo” |
| Add number to string | "Score: " + 10 | Turns 10 into text, then joins it | ”Score: 10” |
| Multi-line | "Line1\nLine2" | \n makes a new line | Line1 (newline) Line2 |
💡 Tip: Strings aren’t just text — they’re objects that have cool powers like
.length()(how long it is) and.toUpperCase()(shouty version!)
🧠 Summary Table
| Data Type | What It’s For | Examples | Notes |
|---|---|---|---|
int | Whole numbers | 5 + 2, 10 / 3, 7 % 4 | No decimals, does regular math |
double | Decimal numbers | 5.0 / 2.0, 3.14 * 2, 1.5 + 3 | Gives accurate decimal answers |
char | Single characters | 'A', 'Z', '!', '1' | Must use single quotes ' ', ASCII aware |
String | Words and sentences | "Hello" + name, "Score: " + 5 | Double quotes, use + to build messages |
🚀 Practice in Visual Studio Code
Time to try these datatypes yourself! Open Visual Studio Code and create a new file called Example.java.
public class Example {
public static void main(String[] args) {
System.out.println("Hello Robocode!");
}
}Save the file, then run the following in your VS Code terminal:
javac Example.java
java ExampleYou’ll see the greeting printed to the console. Modify the message or add variables from the table above, recompile with javac, and run again to observe the changes.
🧪 Challenge: Try printing out these different types using
System.out.println()and see what happens!
System.out.println(5 + 2); // int math
System.out.println(5.0 / 2.0); // double math
System.out.println('A' + 1); // adds to char, prints number!
System.out.println("Hi " + "there!"); // String join➡️ 🤖 Minigame!
Navigation
⬅️ Back: Variables ➡️ Output