Documentation In Progress
dev@4e24cb4
Hytale Modding
GuidesJava basics

01 - Variables & Data Types

An introduction to variables and data types in Java programming.

Variables are containers that store data in your program. Think of them as labeled boxes where you can put different types of information.

What is a Variable?

A variable has three key components:

  1. Type: What kind of data it holds (e.g., integer, string, boolean)
  2. Name: The label you use to refer to the variable
  3. Value: The data it contains

Declaring/Creating Variables

In Java, you must declare a variable before using it, like you'd need a box before putting something in it. The syntax is:

int age;
age = 18;

int age = 18; // Declaration and assignment in one line

Primitive Data Types

You might wonder what "int" means. It's a primitive data type in Java. Java has 8 primitive data types. Here are the most important ones for modding:

Data TypeDescriptionExample
intInteger (whole numbers)int score = 100;
doubleDecimal numbersdouble pi = 3.14;
booleanTrue or false valuesboolean isActive = true;
charSingle characterchar grade = 'A';
StringSequence of characters (text)String name = "Hytale";

Type Conversion

Sometimes you might not want a variable to stay the same type. You can convert between types:

Automatic

int num = 10;
double decimalNum = (double) num; // 10.0 - works automatically

Manual (Casting)

double decimalNum = 9.78;
int num = (int) decimalNum; // 9 - decimals are dropped

Constants

If you want a variable that shouldn't change, use the final keyword:

final double PI = 3.14159;

Convention: Constants use ALL_CAPS_WITH_UNDERSCORES.

Practice

It's best to practice what you just learnt - don't look above now and try to do this yourself. If you get stuck, look back up!

  1. Declare an integer variable named lives and set it to 3.
  2. Declare a double variable named gravity and set it to 9.81.
  3. Declare a boolean variable named isGameOver and set it to false.
  4. Convert the lives variable to a double and store it in a new variable named livesAsDouble.
  5. Declare a constant named MAX_LEVEL and set it to 100.

Great! You've learned about variables and data types in Java. Now we're going to learn about operators and expressions in the next lesson.