Sitemap
Sithila Sihan Somaratne

Sithila Sihan Somaratne

I'm a student. I love programming and I would like to become a Senior Software Engineer.

JAVA — Syntax

3 min readJul 23, 2023

--

In the previous blog, we created a Java file called Example.java. In this blog , we’ll talk about the code in Example.java.

class Example{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Java — Class

A Java class is the template from which objects are created. It is a blueprint for an object. A class is created using the keyword class. Classes can contain methods, and fields (in the next blogs we’ll explain them).

class Example{

}

Main Method — Java

The Java main() method is usually the first method you learn about when you start programming in Java because its the entry point for executing a Java program.

class Example{
public static void main(String[] args) {

}
}

The main() method contains a body (which is the block between the body), where we can write fields and call methods (you will later know about methods after the next few blogs).

As you can see above the main() method is public and static. Public is an access specifier in Java. We should use a public keyword before the main() method so that JVM can identify the execution point of the program. If we use private, protected, and default access specifiers before the main() method, it will not be visible to JVM. The static keyword in Java is used to share the same variable or method of a given class. In the main() method is used to make it a class-related method. The main() method is static so that compiler can call it without the creation of an object or before the creation of an object of the class. The main method returns a void data type. Void is used at method declaration and definition to specify that the method does not return any type. Main is the name of the method. Then the main() method has a parameter(which is used to refer the list of variables in a method declaration.) which is : “String args[]” or “String[] args”. “String args[]” or “String[] args” is an array of type java. lang. String class that stores java command line arguments. (You will later know about java arrays in the next few blogs).

Get Sithila Sihan Somaratne’s stories in your inbox

Join Medium for free to get updates from this writer.

System.out.println() vs System.out.print()

class Example{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

As you may see, in the main method, the following code is written: “System.out.println(“Hello World!”);” With this piece of code, after you compiled and run the program, you will see in the console:

C:\Users\Your PC Name>F:

F:\>md Java

F:\>cd Java

F:\Java>notepad Example.java

F:\Java>javac Example.java

F:\Java>java Example
Hello World!

F:\Java>

But, you can also use this code, to print something in the console: “System.out.println(“Hello World!”);”

class Example{
public static void main(String[] args) {
System.out.print("Hello World!");
}
}

The output will be:

C:\Users\Your PC Name>F:

F:\>md Java

F:\>cd Java

F:\Java>notepad Example.java

F:\Java>javac Example.java

F:\Java>java Example
Hello World!
F:\Java>

We can say that : 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” and print method, are used to print a value to the screen (or a file).

But there’s a difference between “System.out.println();” and “System.out.print();”. The main difference between the two is that print() retains the cursor in the same line after printing the argument, while println() moves the cursor to the next line.

As you see in the console, after you compiled and run the code:

Using println():

F:\Java>java Example
Hello World!

F:\Java>

The output above is different than the output below:

Using print():

F:\Java>java Example
Hello World!
F:\Java>

Note: “System.out.println();” and “System.out.print();” must have the semicolon at the end and the “S” in System for both println(); and in print(); must be capital!

--

--

Sithila Sihan Somaratne
Sithila Sihan Somaratne

Written by Sithila Sihan Somaratne

I'm a student. I love programming and I would like to become a Senior Software Engineer.