General-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible.
Filip Hostinský
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 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:
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
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 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
.
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");
}
In Java, there are different types of variables, for example:
String
- stores text, such as "Hello". String values are surrounded by double quotesint
- stores integers (whole numbers), without decimals, such as 123 or -123float
- stores floating point numbers, with decimals, such as 19.99 or -19.99char
- stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesboolean
- stores values with two states: true or falseTo 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);
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";
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 supports the usual logical conditions from mathematics:
a < b
a <= b
a > b
a >= b
a == b
a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
if
to specify a block of code to be executed, if a specified condition is trueelse
to specify a block of code to be executed, if the same condition is falseelse if
to specify a new condition to test, if the first condition is falseswitch
to specify many alternative blocks of code to be executedThere 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.");
}