Hello, World

Before writing any Java code, let’s understand some basic building blocks of Java.

Building Blocks

Hello World — The First Program

package io.rizwan;

public class Main {
	public static void main(String[] args) {
		 // print "Hello World" on the console
	   System.out.println("Hello World");
	}
}

System.out.println("Hello World"); is used to print any string that is passed to it on the console.

Detailed Explanation —

  1. The System is a class that has a field called out which returns a reference, of type PrintStream. The PrintStream class has a method called println which prints the text that is passed as an argument on the new line in the console or terminal.
  2. A String is a sequence of characters in “double quotes”. A String “Hello World” is passed as an argument in the println method
  3. Comments are statements that are written to help others understand the code. Single-line comments are prefixed by // (Double forward slashes). Comments don’t get executed. As a best practice we should comment about why we wrote the block of code instead of what and how.

Naming Convention, for Classes — PascalNamingConvention and for Methods — camelNamingConvention. Also, Starting curly braces should be on the same line as the method declaration.

<aside> 👨‍💻

Every statement in Java ends with a semicolon. Java is a case-sensitive language i.e. println and Println will be treated differently.

</aside>

Variables