Hytale Modding
Hytale Modding
Основи Java

01 - Змінні та типи даних

Вступ до змінних та типів даних у програмуванні на Java.

Змінні - це контейнери, які зберігають дані у вашій програмі. Think of them as labeled boxes where you can put different types of information.

Що таке змінна?

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. Значення: дані, які вона містить

Оголошення/створення Змінних

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; // A declaration
age = 18; // An assignment

int age = 18; // Declaration and assignment in one line, also considered an initialization

Data Types

You might wonder what "int" means. It's a primitive data type in Java. Java has 8 primitive and non-primitive (reference) data types. You will learn the difference between them later. 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 = 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.