Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billon devices run Java.
It is used for:
- Mobile applications (specially Android apps)
- Dekstop applications
- Web applications
- Web servers and application servers
- Games
- Database connection
- And much, much more!
Why Use Java ?
- Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
- It is one of the most popular programming language in the world
- It has a large demand in the current job market
- It is easy to learn and simple to use
- It is open-source and free
- It is secure, fast and powerful
- It has a huge community support (tens of millions of developers)
- Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
- As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
In Java, every application begins with a class name, and that class must match the filename. Let's create our first Java file to print a "Hello World" message on the screen!
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Explained Code :
The main() method is required and you will see it in every Java program:
public static void main(String[] args)
Any code inside the main() method will be executed. Don't worry about the keywords before and after main. You will get to know them bit by bit while reading this tutorial.
For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.
Inside the main() method, we use the println() method to print a line of text to the screen:
public static void main(String[] args) {
System.out.println("Hello World");
}
Java Data Types and Variables
Variables are containers for storing data values. In Java, there are different types of variables, for example:
- String - stores text, such as "Hello". String values are surrounded by double quotes
- int - stores integers (whole numbers), without decimals, such as 123 or -123
- float - stores floating point numbers, with decimals, such as 19.99 or -19.99
- char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
- boolean - stores values with two states: true or false
To create a variable, you must specify the type and assign it a value:
type variableName = value;
Where type is one of Java's types (such as int or String), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.
Operators are used to perform operations on variables and values. Java divides the operators into the following groups:
- Arithmetic Operators
Arithmetic operators are used to arithmetic values to variable.
In the example below, we use the + operator to add together two values:
int x = 100 + 50;
// x = 150
- Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:
int x = 10;
- Comparison Operators
Comparison operators are used to compare two values (or variables).
The return value of a comparison is either true or false. These values are known as Boolean values. In the following example, we use the greater than operator (>) to find out if 5 is greater than 3:
int x = 5;
int y = 3;
System.out.println(x > y); // returns true, because 5 is higher than 3
- Logical Operators
You can also test for true or false values with logical operators. Logical operators are used to determine the logic between variables or values.
- Boolean Values
A boolean type is declared with the boolean keyword and can only take the values true or false:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
- Boolean Expression
A Boolean expression returns a boolean value: true or false. For example, you can use a comparison operator, such as the greater than (>) operator, to find out if an expression (or a variable) is true or false:
int x = 10;
int y = 9;
System.out.println(x > y); // returns true, because 10 is higher than 9
or even easier:
System.out.println(10 > 9); // returns true, because 10 is higher than 9
Java has the following conditional statements:
- Use if to specify a block of code to be executed, if a specified condition is true
if (condition) {
// block of code to be executed if the condition is true
}
- Use else to specify a block of code to be executed, if the same condition is false
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
- Use else if to specify a new condition to test, if the first condition is false
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
- The switch expression is evaluated once.
- The value of the expression is compared with the values of each case.
- If there is a match, the associated block of code is executed.
- The break and default keywords are optional, and will be described later in this chapter
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
- Java While Loop
The while loop loops through a block of code as long as a specified condition is true:
while (condition) {
// code block to be executed
}
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
- Java Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
- Java For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}