Loading lesson path
Java
Java Tutorial focused on Java Introduction and related concepts.
Java is one of the world's most widely used programming languages.
Java is a popular and powerful programming language, created in 1995.
However, if you want to run Java on your own computer, follow the instructions below.
Every line of code that runs in Java must be inside a class . The class name should always start with an uppercase first letter. In our example, we named the class Main .
A computer program is a list of "instructions" to be "executed" by a computer.
You learned from the previous chapter that you can use the println() method to output values or print text in Java:
You can also use the println() method to print numbers.
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.
Variables are containers for storing data values.
The println() method is often used to display variables.
To declare more than one variable of the same type , you can use a comma-separated list:
All Java variables must be identified with unique names .
When you do not want a variable's value to change, use the final keyword.
As explained in the previous chapter, a variable in Java must be a specified data type:
Primitive number types are divided into two groups:
Very often in programming, you will need a data type that can only have one of two values, like:
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
Non-primitive data types are called reference types because they refer to objects.
The var keyword was introduced in Java 10 (released in 2018).
Type casting means converting one data type into another. For example, turning an int into a double .
Operators are used to perform operations on variables and values.
Arithmetic operators are used to perform common mathematical operations.
Assignment operators are used to assign values to variables.
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.
As with comparison operators , you can also test for true or false values with logical operators.
When a calculation contains more than one operator, Java follows order of operations rules to decide which part to calculate first.
Strings are used for storing text.
The + operator can be used between strings to combine them. This is called concatenation :
Java uses the + operator for both addition and concatenation.
Because strings must be written within quotes, Java will misunderstand this string, and generate an error:
The Java Math class has many methods that allows you to perform mathematical tasks on numbers.
Very often in programming, you will need a data type that can only have one of two values, like:
Conditions and if statements let you control the flow of your program - deciding which code runs, and which code is skipped.
The else statement lets you run a block of code when the condition in the if statement is false .
Use the else if statement to specify a new condition to test if the first condition is false .
There is also a short-hand if else , which is known as the ternary operator because it consists of three operands.
You can also place an if statement inside another if . This is called a nested if statement.
You can combine or reverse conditions using logical operators . These work together with if , else , and else if to build more complex decisions.
Instead of writing many if..else statements, you can use the switch statement.
Loops can execute a block of code as long as a specified condition is true.
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…
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:
It is also possible to place a loop inside another loop. This is called a nested loop .
There is also a " for-each " loop, which is used exclusively to loop through elements in an array (or other data structures ):
The break statement can also be used to jump out of a loop .
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
A multidimensional array is an array that contains other arrays.