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!
Why Use Java?
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
It is one of the most popular programming languages in the world
It has a large demand in the current job market
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has huge community support (tens of millions of developers)
Java is an object oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs
As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or
vice versa
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
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Every line of code that runs in Java must be inside a class. And the class name
should always start with an uppercase first letter. In our example, we named the
class Main.
Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.
The main Method
The main() method is required and you will see it in every Java program:
public static void main(String[] args)
Any code inside the main() method will be executed. Don't worry about the keywords
before and after it. 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:
public static void main(String[] args) {
System.out.println("Hello World");
}
Java Statements - Statements
A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements.
The following statement "instructs" the compiler to print the text "Hello World" to the
screen:
It is important that you end the statement with a semicolon ;.
If you forget the semicolon (;), an error will occur and the program will not run:
Example
System.out.println("Hello World!")
Result:
error: ';' expected
System.out.println("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:
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!");
System.out.println("Hello World!");
Double Quotes
Text must be wrapped inside double quotations marks "".
If you forget the double quotes, an error occurs:
Example
System.out.println("This sentence will work!");
System.out.println(This sentence will produce an error);
The Print() Method
There is also a print() method, which is similar to println().
The only difference is that it does not insert a new line at the end of the output:
Example
System.out.print("Hello World! ");
System.out.print("I will print on the same line.");