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.

OperationCodeWhat It DoesResult
Additionint sum = 5 + 2;5 plus 27
Subtractionint diff = 5 - 2;5 minus 23
Multiplicationint product = 5 * 2;5 times 210
Divisionint quotient = 5 / 2;5 divided by 2, decimal gets cut off2
Remainderint rem = 5 % 2;What’s left after dividing 5 by 21

📝 Note: If you divide 5 / 2 using int, you don’t get 2.5 — Java chops off the decimal and gives just 2.


🧮 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;
OperationCodeWhat It DoesResult
Multiplicationdouble r = 4.5 * 1.2;4.5 times 1.25.4
Order of Opsdouble r = 4.5 * 1.2 + 3;Multiply first, then add8.4
Divisiondouble d = 5.0 / 2.0;Divides with decimals2.5

Reminder: To get a decimal answer, at least one number must have a .0 in it! If both numbers are int, you’ll get an int result.


🔤 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 = '!';
OperationCodeWhat It DoesResult
Assign letterchar letter = 'A';Stores letter A'A'
Next letterchar next = (char)(letter + 1);Adds 1 to A (ASCII code) = B'B'
Symbolchar symbol = '!';Stores exclamation mark'!'

💡 Fun Fact: Letters are secretly stored as numbers (ASCII). 'A' is 65, 'B' is 66, 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!";
OperationCodeWhat It DoesResult
Basic join"Hello" + " World"Joins the two parts together”Hello World”
Add variable"Hello, " + nameAdds the value inside name”Hello, Robo”
Add number to string"Score: " + 10Turns 10 into text, then joins it”Score: 10”
Multi-line"Line1\nLine2"\n makes a new lineLine1 (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 TypeWhat It’s ForExamplesNotes
intWhole numbers5 + 2, 10 / 3, 7 % 4No decimals, does regular math
doubleDecimal numbers5.0 / 2.0, 3.14 * 2, 1.5 + 3Gives accurate decimal answers
charSingle characters'A', 'Z', '!', '1'Must use single quotes ' ', ASCII aware
StringWords and sentences"Hello" + name, "Score: " + 5Double 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 Example

You’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!

⬅️ Back: Variables ➡️ Output