Data types and variable
node to self • Jan 24, 2026In an effort to study for my upcoming exam in one of my most favorite course, I have decided to recreate a blog series I used to write in when I was in school, learning for my final exams.
This series will start with basics about programming, data structures and algorithms. Over the course of three years, that is how long studying will probably take, I want to adjust this series with more entries and more complex topics.
Today, we are starting with the basics about data types and variables. Enjoy reading and if you find any issues, feel free to contact me and suggest improvements.
The examples and explanations here are based on how things work in Java. Other programming languages may behave differently. For example, in C and C++,
intis only guaranteed to be at least 16 bits wide, whileint32_tis explicitly 32 bits, whereas in Java,intis always 32 bits long.
Data-Types
Numbers
Numbers can be represented by different data types. Those are:
int
int is standard data-type of whole numbers. It is 32 bits long and can thus represent number from -2,147,483,648 to
2,147,483,647. Normally, if you try to assign a larger number than that to an int, your IDE will throw an error. If
somehow (e.g. through calculations) the number becomes bigger than the number, int can represent, it will start at the
smallest representable number (-2,147,483,648) and just add the rest on top of it.
So:
int i = 2,147,483,647;
int num = i + 2; // num = -2,147,483,647
long
The second data-type that represents numbers is long. Long is 64 bits long and thus can represent numbers from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
float
To represent decimal numbers, you can either use float or double.
Float uses 32 bits to store an approximate real number and provide about 6-7 digits of precision.
double
Double is the standard data-type for decimal numbers. It uses 64 bits to store an approximate real number and provide about 15-16 digits of precision.
Using
floatordoublemeans to give up precision. Handling the loss of precision is up to the developer to care about. This can be done by using a bigger unit of measurement (kilometers instead of meters) or by warning the user about the loss of precision, or whatever.
short
short is almost never used in Java. It is 16 bits long and can represent numbers from −32,768 to 32,767.
byte
byte is the smallest integer type Java has. With only 8 bits, it can represent numbers from -128 to 127.
Java's JVM optimized around
int. Usingbyteorshortdoes not gain you performance, since arithmetic onbyteandshortis promoted tointanyway.
Arithmetic operations for numbers
Arithmetic operations can be performed on real and on whole numbers. Addition, substraction, division, multiplication and modulo are available in Java with:
int i = 2 + 1; // 3
int j = 2 - 1; // 1
int k = 2 / 2; // 1
int l = 2 * 2; // 4
When combining whole and real numbers in an arithmetic operation, the result of the operation will always be a real number.
double i = 3.0 / 2; // 1.5
For the operation itself, the data-type is irrelevant. If you try to run int i = 3 / 2;, Java will cut every decimal
number, so the result of this execution will be 1, instead of 1.5.
We will learn about type-conversion later in this post.
Boolean
Booleans are used to represent simple true or false statements. In theory, a boolean value requires only one bit, with
0 representing false and 1 representing true. In practice, the actual amount of memory used depends on the
implementation. Based on boolean values, logical operations can be performed. Negation expresses the logical "not" of a
value and is written as $\neg$A; in Java, this operation is written as !a.
Conjunction represents "A and B" and is written as A$\land$B; in Java, this corresponds to a && b.
Disjunction represents "A or B" and is written as A$\lor$B; in Java, this is written as a || b.
In Java, the logical "or" operator does not represent an exclusive or. The expression
a || bevaluates to true if either operand is true or if both are true. A xor (exclusive or) is true, when exactly one operand is true. If both are true, the result is false.
Characters
A single character is represented by the data-type char. It can be used to show single printable symbols (symbols that
can directly be entered through the keyboard) and non-printable and special characters, including, but not limited to \n
(for new lines), \t (a tabulator), and \u03B1 (to represent $\alpha$)
chars are represented as 16-bit Unicode numbers internally (e.g. 'A' = 65 = 00000000 01000001) and are sorted by their
Unicode number.
String
To represent a chain of characters, you use String. String is the only non-elemental data-type in this entry, as it
only represents a referenz to the storage area where the string can be found. Thus String is called a reference-type.
A String has no set limit of size and is only limited by the available memory. Internally, String is a class, that
is why its name starts with a capital letter.
Different to the other data-types, comparing strings in Java is not done by the == operator, but by its built-in
function equals():
String name = "Lijucay";
String blogName = "Node to self";
boolean isEqual = name.equals(blogName); // false
// Or directly:
"Lijucay".equals("Node to self"); // false
String concatenation
It is possible to combine two strings with the + operator:
String name = "Lijucay";
String combined = name + " is the author of this post";
System.out.println(combined); // prints "Lijucay is the author of this post" into the terminal.
Interesting fact: Strings in Java cannot be modified directly. When a string appears to be changed, a new string object is actually created to represent the modified version, while the original string remains unchanged in the memory. In this sense,
Stringobjects behave like constants in Java.
Values
Declarations of variables
Declaring a variable means to name an instance of an object. This helps to improve the readability, and to save necessary memory for the values.
Initializing
Initializing a variable means to give the instance of an object an initial value.
Example: int number = 5;
Assigning values
Assigning values means to set a variable to a specified value. However upon assignment, the value has to be of the same data type as the variable.
Example:
int number = 0;
number = 314;
number = number + 27;
// All of these instructions assign a new value to the declared and initialized variable.
Possible assignments: Expressions
- Expressions can be constant values:
int number = 4;
double pi = 3.14159;
String greeting = "Hello World!";
- Variables:
int x = 4;
int number = x; // number will stay 4 even after x was changed.
- Operators
int x = 4;
int number = x + 2;
boolean greaterThan = number > 4;
String world = "World";
String greeting = "Hello " + world + "!";
Output values
Values can be output to the terminal using:
System.out.println("Hello World!"); // Automatically creates a new line at the end
System.out.print("Hello "); // No new line at the end
System.out.println("World!");
Read values
Values can be read with different approaches. One way would be to use the Scanner:
import java.util.Scanner; // needs to be added, otherwise, it can't find Scanner
public class Main {
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
// or to read integers
int number = scanner.nextInt()
scanner.close(); // Necessary to prevent resource leaks
Type conversion
As we know, the variable and its value must be of the same type, otherwise, the code will run into an error.
But sometimes, we need to convert a type into another (e.g. a double into an int).
There are two ways this can happen.
Implicit type conversion
Implicit type conversions are conversions that are defined by the programming language itself, in this case Java. They allow values of certain types to be assigned to variables of other types without requiring an explicit cast, as long as no information is lost.
Because of implicit conversion, the following examples are valid:
float x = 33; // equivalent to: float x = 33F;
int codeNumber = 'A'; // works because characters are stored by Unicode numbers, here, codeNumber is 65, the Unicode number of 'A'
However, implicit conversion is not allowed when precision would be lost:
int num = 5.5; // does not work, because the decimal part would be discarded
Arithmetic operations also follow the rule of the operand types:
double y = 5 / 2; // results in 2.0 because both operands are integers, so integer devision is performed before assignment
In general, Java allows implicit conversion along the following hierarchy:
byte ⊆ short ⊆ int ⊆ long ⊆ float ⊆ double
Additionally, characters can be implicitly converted to integers:
char ⊆ int
Explicit conversion
The explicit conversion is done by specifying the desired type.
int number = (int) 5.5; // 5
If the decimals of 5.5 are not otherwise stored by the developer, the information about the decimals are lost for the rest of the code's execution and cannot be recreated.
There is no rounding when casting a decimal to an integer, every decimal number is stripped away. (Casting 0.99 to an integer will result in 0)
Sincedoubleis bigger thanint, numbers before the decimals are radically stripped away too, if the cast would result in a number thatintcannot represent.
Since there is a loss of precision and information, casting should be handled carefully
Constants
A constant's value is the same throughout the whole execution of a program and cannot be changed from within the
execution. Its declaration is similar to that of a variable, but it starts with the keyword final before the data-type
is declared. Since you have only one value to change, it can improve the maintenance of the code.
Usually, following the code convention for Java, a constant's name is written in caps and single words are separated by
and underscore.
Example:
final String AUTHOR_NAME = "Lijucay";