Java

General-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible.


me Filip Hostinský

Start Here



Java Introduction


What is Java?


Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

  • Mobile applications (specially Android apps)
  • Desktop applications
  • Web applications
  • Web servers and application servers
  • Games
  • Database connection
  • And much, much more!

Why Use Java?


Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

  • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • It is one of the most popular programming language in the world
  • 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

Java Syntax


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

Example explained

Every line of code that runs in Java must be inside a class. In our example, we named the class MyClass. A class should always start with an uppercase first letter.

The main Method


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. You don't have to understand 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.

console.log(Object.getPrototypeOf({}) == Object.prototype);
// → true
console.log(Object.getPrototypeOf(Object.prototype));
// → null

Many objects don’t directly have Object.prototype as their prototype but instead have another object that provides a different set of default properties. Functions derive from Function.prototype, and arrays derive from Array.prototype.

System.out.println()


Inside the main() method, we can 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 Variables


Java Variables


In Java, there are different types of variables, for example:

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

type variable = value;

Where type is one of Java's types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

To create a variable that should store text, look at the following example:

String name = "John";
System.out.println(name);

To create a variable that should store a number, look at the following example:

int myNum = 15;
System.out.println(myNum);

Other Types


A demonstration of how to declare variables of other types:

type variable = value;

Where type is one of Java's types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

To create a variable that should store text, look at the following example:

int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";

Java Operators


Java Operators


Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

int x = 100 + 50;

Java divides the operators into the following groups:

For more informations, please visit w3schools

Java If ... Else


Java Conditions and If Statements


Java supports the usual logical conditions from mathematics:

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

Short Hand If...Else (Ternary Operator)


There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements:

variable = (condition) ? expressionTrue :  expressionFalse;

Instead of writing:

int time = 20;
if (time < 18) {
    System.out.println("Good day.");
    } else {
    System.out.println("Good evening.");
    }

Sources:

hackr.io
W3schools
Wikipedia
developer.mozzila
Eloquent Javascript (book)