Lesson 3 Java Introduction and Syntax
Lesson 3 Java Introduction and Syntax
What is Java?
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:
Mobile applications (specially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
Java QuickStart
In Java, every application begins with a class name, and that class must match the filename.
The file should contain a "Hello World" message, which is written with the following code:
Main.java
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).
You should also note that each code statement must end with a semicolon (;).
Exercise #1: Insert the missing part of the code below to output "Hello World".
Java Output/Print
Print Text
You learned from the previous chapter that you can use the println() method to output values or print text in
Java: 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("I am learning Java.");
System.out.println("It is awesome!");
Double Quotes
When you are working with text, it must be wrapped inside double quotations marks “”.
If you forget the double quotes, an error occurs:
Example:
System.out.println("This sentence will work!");
Print Numbers
You can also use the println() method to print numbers.
However, unlike text, we don't put numbers inside double quotes:
Example:
System.out.println(3);
System.out.println(358);
System.out.println(50000);
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).
This example uses a single-line comment before a line of code:
Example:
// This is a comment
System.out.println("Hello World");
Exercise #2: Insert the missing part to create two types of comments.
Lesson 3.2: