Java
Java
What is Java?
Java is a popular programming language, created in 1995.
It is used for:
Java Install
Some PCs might have Java already installed.
To check if you have Java installed on a Windows PC, search in the start bar for
Java or type the following in Command Prompt (cmd.exe):
C:\Users\Your Name>java -version
If Java is installed, you will see something like this (depending on version):
If you do not have Java installed on your computer, you can download it for free
at oracle.com.
Note: we will write Java code in a text editor. However, it is possible to write
Java in an Integrated Development Environment, such as IntelliJ IDEA,
Netbeans or Eclipse, which are particularly useful when managing larger
collections of Java files.
Step 1
Step 2
Step 3
Step 4
Step 5
Write the following in the command line (cmd.exe):
If Java was successfully installed, you will see something like this (depending on
version):
Let's create our first Java file, called Main.java, which can be done in any text
editor (like Notepad).
The file should contain a "Hello World" message, which is written with the
following code:
Main.java
System.out.println("Hello World");
Don't worry if you don't understand the code above - we will discuss it in detail
in later chapters. For now, focus on how to run the code above.
This will compile your code. If there are no errors in the code, the command
prompt will take you to the next line. Now, type "java Main" to run the file:
Hello World
Congratulations! You have written and executed your first Java program.
Java Syntax
In the previous chapter, we created a Java file called Main.java, and we used
the following code to print "Hello World" to the screen:
Main.java
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 Main. A class should always start with an uppercase first
letter.
The name of the java file must match the class name. When saving the file,
save it using the class name and add ".java" to the end of the filename. To run
the example above on your computer, make sure that Java is properly installed:
The output should be:
Hello World
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.
System.out.println()
Inside the main() method, we can use the println() method to print a line of
text to the screen:
System.out.println("Hello World");
Note: The curly braces {} marks the beginning and the end of a block of code.
System is a built-in Java class that contains useful members, such as out, which
is short for "output". The println() method, short for "print line", is used to
print a value to the screen (or a file).
Don't worry too much about System, out and println(). Just know that you
need them together to print stuff to the screen.
You should also note that each code statement must end with a semicolon (;).
Java Output / Print
(Print Text /Print Numbers)
Print Text
You learned from the previous chapter that you can use the println() method
to output values or print text in Java:
Example
System.out.println("Hello World!");
You can add as many println() methods as you want. Note that it will add a
new line for each method:
Example
System.out.println("Hello World!");
System.out.println("It is awesome!");
Double Quotes
When you are working with text, it must be wrapped inside double quotations
marks "".
Example
System.out.println("This sentence will work!");
The only difference is that it does not insert a new line at the end of the output:
Example
System.out.print("Hello World! ");
Note that we add an extra space (after "Hello World!" in the example above),
for better readability.
NB. In this UNIT, we will only use println() as it makes it easier to read the
output of code.
Print Numbers
You can also use the println() method to print numbers.
Example
System.out.println(3);
System.out.println(358);
System.out.println(50000);
You can also perform mathematical calculations inside the println() method:
Example
System.out.println(3 + 3);
Example
System.out.println(2 * 5);
Java Comments
Java Comments
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.
Single-line Comments
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).
Example
// This is a comment
System.out.println("Hello World");
Example
System.out.println("Hello World"); // This is a comment
Example
/* The code below will print the words Hello World
System.out.println("Hello World");
NB: It is up to you which you want to use. Normally, we use // for short
comments, and /* */ for longer.
Java Variables
Java Variables, Java Print Variables,
Java Declare Multiple Variables and
Java Identifiers
Java Variables
Variables are containers for storing data values.
Syntax
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.
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":
System.out.println(name);
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:
System.out.println(myNum);
You can also declare a variable without assigning the value, and assign the
value later:
Example
int myNum;
myNum = 15;
System.out.println(myNum);
Note that if you assign a new value to an existing variable, it will overwrite the
previous value:
Example
Change the value of myNum from 15 to 20:
System.out.println(myNum);
Final Variables
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant", which
means unchangeable and read-only):
Example
final int myNum = 15;
Other Types
A demonstration of how to declare variables of other types:
Example
int myNum = 5;
Example
String name = "John";
You can also use the + character to add a variable to another variable:
Example
String firstName = "John ";
System.out.println(fullName);
Example
int x = 5;
int y = 6;
Example
Instead of writing:
int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Example
int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
Java Identifiers
Identifiers
All Java variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
Example
// Good
int m = 60;