Lecture 1-Introduction
Lecture 1-Introduction
What is Java?
Java is a popular programming language, created in 1995.
It is used for:
Get Started
It is not necessary to have any prior programming experience.
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):
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: In this tutorial, 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 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 us 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");
Do not worry if you do not 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
Try it Yourself »
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. You do not have to
understand the keywords before and after main. You will get to know them bit
by bit as the lectures continue.
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.
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");
Try it Yourself »
This example uses a single-line comment at the end of a line of code:
Example
System.out.println("Hello World"); // This is a comment
Try it Yourself »
Example
/* The code below will print the words Hello World
System.out.println("Hello World");
Try it Yourself »
It is up to you to decide which you want to use. Normally, we use // for short
comments, and /* */ for longer.
Test