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,booleanandchar. - Reference types – objects such as
String , 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:
| Type | Purpose and values |
|---|---|
byte | Integers from −128 to 127. |
short | Integers from −32,768 to 32,767. |
int | Integers from −2,147,483,648 to 2,147,483,647. |
long | 64-bit integers from −263 to 263−1. |
float | 32-bit floating-point numbers with approximately 6–7 significant digits of precision. |
double | 64-bit floating-point numbers with approximately 15–16 significant digits of precision. |
boolean | The Boolean value true or false. |
char | One 16-bit UTF-16 code unit from '\u0000' to '\uffff'. |
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 longL suffix, and for a float literal, use f. Underscores in numbers improve readability without changing their values.
Reference types
String, 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 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); // 19The result is 19, not 20, because conversion to int does not round.