Basics

Fundamental rules

We count from zero, not from one.

Quotation marks and apostrophes mean something slightly different, and I will explain it in a moment.

In Java, a semicolon is placed after each statement. We must indicate which instructions belong to, e.g., a loop, by putting them in braces (unless it's only one line).

We can create comments by using the // symbols. Things that we write after these signs don't count as code. Comments are often used to describe the code. Multiline comments can be written between /* and */ signs.

Variables

A variable is a "drawer" for data in the RAM. We can store every sort of data in them. We have a lot of basic variable types:

byte (1 byte) - integers in this range: [-128 to 127]

short (2 bytes) - integers in this range: [-32768, 32767]

int (4 bytes) - integers in this range: [-2147483648, 2147483647]

long (4 bytes) - integers in this range: [-2147483648, 2147483647]

float (4 bytes) - decimals (up to 6-7 digits after the decimal point)

double (8 bytes) - decimals with double precision (up to 15-16 digits after the decimal point)

String - texts (a number can also be a part of it, e.g., "abc123")

char (2 bytes) - one character. When using this type, we have to use apostrophes and not quotation marks. This rule goes both ways, and we can't use apostrophes with strings. A character can be converted to an integer (ASCII).

boolean (1 byte) - true (all numbers except 0) and false (0) values.

null - an object used to indicate the absence of a value.

We have to define the type when creating a variable.

Everything on the left side of the = sign is a variable's name (it cannot contain spaces), and on the right is a value.

Be cautious because, e.g., variables x and X are not the same!

int number = 4; will initialize an int variable called "number" and immediately assign it the value 4. Java will reserve 4 bytes for it. number = 5; will change the value of this variable to 5.

If we don't immediately have any value to assign to the variable, we can simply declare it, e.g., int number2;. We say that we declare a variable if it is empty at the beginning, and we initialize it when we give it a value. We can assign a value to the declared variable the same way as if we were changing the value of a variable that has already been initialized (number2 = 6;).


int number = 4; 
number = 5;
int number2;
number2 = 6;
                                    

*In the examples I provide, I will not always give the entire code but fragments of it.

While naming variables, we cannot add any numbers or spaces at the beginning. The first letter should not be in uppercase. The three correct ways of naming variables are: prime_number, primeNumber, nPrimeNumber (Camel case or Snake case).

We can define a variable as a constant, whose value cannot be changed, by adding final before the variable definition (e.g., final String NAME = "text";). There is a convention that the names of constants should be in uppercase.

Variables can be declared in bulk, e.g., int a, b, c;. They can also be defined in the same way: int a = 1, b, c = 2; (we can define only some of them if we want). In this case, the b variable is not empty, although it has no value assigned. It contains a value previously stored at this address in the computer memory. It is always safer to assign a value to a variable at the beginning to avoid errors (even if its value would be 0).

If, for example, the number 4 is assigned to a float variable, it will be written as 4.0. If we divide 4.5 by 2, we get 2.25, but if we divide 5 by 2, we get 2 because when we divide an int by an int, we get an int. When dividing an int by an int, we should write, e.g., 5.0 / 2, to get the exact result.

If we assign a char character to an int, it will be converted to the ASCII number corresponding to that character. A float number assigned to an int would be abbreviated without rounding (e.g., 2.6 would equal 2), and an int assigned to a float would be extended (e.g., 2 would equal 2.0).

We can concatenate (join) strings with the + operator, e.g., String a = b + c;, where b and c are String variables.

When we use the long or the float type, we have to add a corresponding letter at the end of the number: long a = 1234334124252345252L;, float b = 4.75f;.

If we divide 1234 by 10, the result will be 123 because we are dividing by 10, not 10.0 (numbers after the decimal point will not be included).

A "falsy" number is a number that, after conversion to a boolean, e.g., in a conditional statement, equals false, e.g., 0.

\n symbolizes the enter key and can be used inside of strings.

Retrieving data from the user is complicated, so I will show you how to do it later.

For now, a method is a special command that performs a specific task, often related to something in our program, like printing text or calculating a result (I will talk more about methods later).

Hello World program

The System.out.println() method displays data of all types on the screen. You will learn what the rest of the code means when you get to the lessons about OOP, and for now, let's say it's just a template for all code.


public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
                                    

We can compile a Java file from the terminal using the javac Main.java command, and then run it using java Main (the name of the file has to be the same as the name of the class within it). Each file should contain only one class.

Type casting

Type casting, also known as type conversion, is the process of converting one data type to another (e.g., double to int).


double x = 5.5;
int y = (int)x; // the type written inside the parenthesis is the one that we are converting to
System.out.println(y);
                                    

Type casting can be implicit, where a type is automatically converted to another (e.g., when dividing an integer by another integer, resulting in a floating-point number), or explicit, as shown above. An example of implicit type casting:


int x = 5;
int y = 10;
System.out.println("Example" + x + y); // the result is "Example510" because one value is a string, so + acts as the concatenation operator (it can't "add" a string)
                                    

Important terms

You can come back to this section later.

Garbage Collector

The garbage collector in Java is an automatic memory management system that helps reclaim memory by removing objects that are no longer in use. It operates in the background, allowing developers to focus on writing code without worrying about manual memory deallocation.

JVM (Java Virtual Machine)

The Java Virtual Machine (JVM) is an abstract computing machine that enables Java bytecode to be executed on any platform, providing the "write once, run anywhere" capability. It manages system resources, including memory and execution threads, ensuring that Java applications run efficiently and securely.

JDK (Java Development Kit)

The Java Development Kit (JDK) is a software development environment that provides the tools necessary for developing Java applications, including the Java compiler, libraries, and documentation. It includes the JRE (Java Runtime Environment) and additional tools for debugging, monitoring, and packaging Java applications.

JRE (Java Runtime Environment)

The Java Runtime Environment (JRE) is a part of the Java Development Kit (JDK) that provides the necessary libraries and components to run Java applications. It includes the Java Virtual Machine (JVM) and standard class libraries, allowing developers to execute Java programs on their machines without needing the full development tools.

.jar (Java Archive)

A .jar (Java Archive) file is a package file format used to bundle Java classes, metadata, and resources (such as images or libraries) into a single file. .jar files are used for distributing Java applications or libraries, making them easier to deploy and manage. They are typically compressed, enabling efficient storage and distribution of Java programs.

Bytecode

Bytecode is the intermediate representation of Java source code, which is generated after compiling the code with the Java compiler. This platform-independent code is executed by the Java Virtual Machine (JVM), allowing Java programs to be run on any device or operating system that supports the JVM. Bytecode is a crucial aspect of Java's "write once, run anywhere" philosophy.