JAVA BASICS

Java data types

Every Java variable is declared with a data type. The type defines which values the variable can store, which operations are allowed on them and how much memory a primitive value requires.

Values of different types

In Java, the type comes before the variable name:

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

Each value follows the rules of its type: text uses double quotes, a single character uses single quotes, a float literal has an f suffix, and a boolean value is true or false.

Two main groups of types

Java data types fall into two groups:

  • Primitive types – byte, short, int, long, float, double, boolean and char.
  • Reference types – objects such as , arrays, classes and interfaces.

A primitive variable stores the simple value itself. A reference variable stores a reference to an object or can hold null. String is not a primitive type, although it is used very often.

The eight primitive Java types

A primitive type defines a set of possible values and how they are stored:

Java primitive data types
TypePurpose and values
byteIntegers from −128 to 127.
shortIntegers from −32,768 to 32,767.
intIntegers from −2,147,483,648 to 2,147,483,647.
long64-bit integers from −263 to 263−1.
float32-bit floating-point numbers with approximately 6–7 significant digits of precision.
double64-bit floating-point numbers with approximately 15–16 significant digits of precision.
booleanThe Boolean value true or false.
charOne 16-bit UTF-16 code unit from '\u0000' to '\uffff'.
char is not limited to characters.

It stores one code unit. Some characters, such as certain emoji, require two char values.

Primitive type examples

byte mazasSkaicius = 100;
short trumpasSkaicius = 30_000;
int gyventojuSkaicius = 2_800_000;
long atstumasMetrais = 9_000_000_000L;
float temperatura = 21.5f;
double kaina = 19.99;
boolean arPrisijunges = true;
char kategorija = 'A';

For a large long use the L suffix, and for a float literal, use f. Underscores in numbers improve readability without changing their values.

Reference types

describe objects and more complex data structures. They include String, , standard Java classes and classes you create yourself. Unlike primitive variables, reference variables can hold null, meaning they currently refer to no object.

Objects can have fields and methods. For example, the String value's length() method returns the length of the text. Classes, objects, arrays and strings are covered in separate lessons.

The declared type does not change

Once a variable is declared, its type stays the same throughout its scope. int variables cannot be assigned text directly:

int taskai = 100;

// A String value cannot be assigned to an int variable:
// taskai = "šimtas";

You can change a variable's value, but the new value must be compatible with its declared type. If you need a different type, create another variable or perform an explicit conversion.

Type casting

Some numeric values can be . When converting double to int you must explicitly specify the target type, and the fractional part is discarded:

double kaina = 19.99;
int suapvalintaKaina = (int) kaina;

System.out.println(suapvalintaKaina); // 19
Conversion can lose information.

The result is 19, not 20, because conversion to int does not round.