JAVA BASICS

Java variables

A variable is a named location for storing a data value. In Java, every variable has a type that determines the data it can hold, such as text, an integer or a boolean value.

Basic variable types

These are the Java types you will encounter most often at first:

  • String stores text, such as "Labas".
  • int stores whole numbers without a fractional part, such as 123 or -15.
  • float stores numbers with a fractional part, such as 19.99f.
  • char stores a single character in single quotes, such as 'A'.
  • boolean stores one of two values: true or false.

Text values use double quotes, while a single char character uses single quotes. The rules for different types help the compiler detect invalid assignments before the program runs.

Declaring a variable

To create a variable, choose a type, give it a name and optionally assign a value:

tipas kintamojoPavadinimas = reikšmė;
  1. Type determines what kind of value will be stored.
  2. Name lets you refer to the variable later in the code.
  3. The equals sign= assigns the value on the right to the variable.
  4. The semicolon; ends the declaration statement.

Storing text

Type String is for text. This example creates the variable vardas, assigns it a value and outputs that value to the console:

String vardas = "Jonas";
System.out.println(vardas);

Storing an integer

Type int stores an integer. The numeric value is written without quotes:

int skaicius = 15;
System.out.println(skaicius);

Writing a number in quotes makes it text. The values 15 and "15" may look similar to a person, but Java treats them as different data types.

You can assign the value later

You can declare a variable first and assign its value in a later statement:

int skaicius;
skaicius = 15;
System.out.println(skaicius);
A local variable must be assigned a value before it is used.

If you try to output an uninitialised local variable, the compiler will report an error.

Changing a variable’s value

A new assignment replaces the variable’s previous value:

int skaicius = 15;
skaicius = 20; // The previous value is replaced
System.out.println(skaicius);

The console will display 20, because this is the last value assigned to the variable.

Unchanging values with final

If a variable’s reference or primitive value should not change after assignment, use the keyword final. You cannot assign another value to this variable:

final int MAKSIMALUS_BANDYMU_SKAICIUS = 15;
MAKSIMALUS_BANDYMU_SKAICIUS = 20; // Compilation error

A common Java convention writes constant names in uppercase, with underscores between words. This is a readability convention, not a compiler requirement.

Examples of different types

int sveikasisSkaicius = 5;
float trupmeninisSkaicius = 5.99f;
char raide = 'D';
boolean arAktyvus = true;
String tekstas = "Labas";

Notice the f after the float value, the single quotes for char and the double quotes for String .

Clear variable names

  • Choose a name that describes the purpose of the stored value.
  • Java variable names usually start with a lowercase letter, for example, userAge.
  • Names cannot contain spaces; multiple words are usually joined using camelCase.
  • Do not use Java keywords such as int, class or public.