Java Basics
Java Basics
It is used for:
Any text between // and the end of the line is ignored by Java (will not be
executed).
Java Variables
Variables are containers for storing data values.
Syntax
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:
Example
Create a variable called name of type String and assign it the value "John":
To create a variable that should store a number, look at the following example:
Example
Create a variable called myNum of type int and assign it the value 15:
Java Data Types
As explained above, a variable in Java must be a specified data type:
Example
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:
Example
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
Example
You can use these conditions to perform different actions for different decisions.
The if Statement
Use the if statement to specify a block of Java code to be executed if a
condition is true.
Syntax
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
In the example below, we test two values to find out if 20 is greater than 18. If
the condition is true, print some text:
Example
Example
Example explained
Syntax
Example
Example explained
In the example above, time (20) is greater than 18, so the condition is false.
Because of this, we move on to the else condition and print to the screen "Good
evening". If the time was less than 18, the program would print "Good day".
Syntax
Example
Example explained
In the example above, time (22) is greater than 10, so the first
condition is false. The next condition, in the else if statement, is also false, so
we move on to the else condition since condition1 and condition2 is
both false - and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
Syntax
The example below uses the weekday number to calculate the weekday name:
Example
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no
need for more testing.
The default Keyword
The default keyword specifies some code to run if there is no case match:
Loops
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.
Syntax
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:
Example
Note: Do not forget to increase the variable used in the condition, otherwise
the loop will never end!
Syntax
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:
Example
Do not forget to increase the variable used in the condition, otherwise the loop
will never end!
Syntax
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example
Example explained
Statement 2 defines the condition for the loop to run (i must be less than 5). If
the condition is true, the loop will start over again, if it is false, the loop will
end.
Statement 3 increases a value (i++) each time the code block in the loop has
been executed.
Java Break
You have already seen the break statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.
Example
Java Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
Example
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
We have now declared a variable that holds an array of strings. To insert values
to it, we can use an array literal - place the values in a comma-separated list,
inside curly braces:
Example
Array Length
To find out how many elements an array has, use the length property:
Example
Example
Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.
To create a two-dimensional array, add each array within its own set of curly
braces:
Example
To access the elements of the myNumbers array, specify two indexes: one for
the array, and one for the element inside that array. This example accesses the
third element (2) in the second array (1) of myNumbers:
Example
We can also use a for loop inside another for loop to get the elements of a two-
dimensional array (we still have to point to the two indexes):
Example
We can also use a for loop inside another for loop to get the elements of a two-
dimensional array (we still have to point to the two indexes):
Example
What is OOP?
OOP stands for Object-Oriented Programming.
Look at the following illustration to see the difference between class and
objects:
Another example:
When the individual objects are created, they inherit all the variables and
methods from the class.
Create a Class
To create a class, use the keyword class:
Create an Object
In Java, an object is created from a class. We have already created the class
named MyClass, so now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the object
name, and use the keyword new:
Multiple Objects
You can create multiple objects of one class:
Java Methods
A method is a block of code which only runs when it is called.
Methods are used to perform certain actions, and they are also known
as functions.
Why use methods? To reuse code: define the code once, and use it many
times.
Create a Method
A method must be declared within a class. It is defined with the name of the
method, followed by parentheses (). Java provides some pre-defined methods,
such as System.out.println(), but you can also create your own methods to
perform certain actions:
Example
Create a method inside Main:
Example Explained
In the following example, myMethod() is used to print a text (the action), when it
is called:
Example
Inside main, call the myMethod() method:
Example
Parameters and Arguments
Information can be passed to methods as parameter. Parameters act as
variables inside the method.
Parameters are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String called fname as
parameter. When the method is called, we pass along a first name, which is
used inside the method to print the full name:
Example
When a parameter is passed to the method, it is called an argument. So, from the
example above: fname is a parameter, while Liam, Jenny and Anja are arguments.
Multiple Parameters
You can have as many parameters as you like:
Note that when you are working with multiple parameters, the method call must
have the same number of arguments as there are parameters, and the
arguments must be passed in the same order.
Return Values
The void keyword, used in the examples above, indicates that the method
should not return a value. If you want the method to return a value, you can
use a primitive data type (such as int, char, etc.) instead of void, and use
the return keyword inside the method:
Example
You can also store the result in a variable (recommended, as it is easier to read
and maintain):
Example