Java Introduction

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
Hello World

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 Comment

Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code. There are two ways to write a comment in Java :

  1. Single-line Comments
  2. Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java (will not be executed).

    This example uses a single-line comment before a line of code:

    // This is a comment System.out.println("Hello World");

    This example uses a single-line comment at the end of a line of code:

    System.out.println("Hello World"); // This is a comment
  3. Multi-line Comments
  4. Multi-line comments start with /* and ends with */.

    Any text between /* and */ will be ignored by Java.

    This example uses a multi-line comment (a comment block) to explain the code:

    /* The code below will print the words Hello World to the screen, and it is amazing */ 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
Java Declaring Variables

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.

Java Operators

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.

Java Booleans
  1. Boolean Values
  2. 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
  3. Boolean Expression
  4. 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 If..Else

Java has the following conditional statements:

Java Switch Case

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:

Java Loop

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.

  1. Java While Loop
  2. 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++; }
  3. Java Do/While Loop
  4. 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);
  5. Java For Loop
  6. 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); }
Reference
  • All the documentation in this page is taken from W3School.