Java Syntax

In java every program contain a class, every line of code must be inside a class.

Java Syntax
Java Syntax

In java every program contain a class, every line of code must be inside a class.

Example:

class Hello
{
public static void main(String[] args)
{
System.out.println("Welcome To Neural Beast's Java Tutorial");
}
}

Open a notepad file and save the file name as the class name with “.java” as extension.

To compile the source code to byte code, which is executed by JVM, we have to use javac command with the file name.

javac Hello.java

Note: Java is case-sensitive programming language, “hello.java” and “Hello.java” are different. The file name must be the class name.

When the source code is compiled successfully, this will create a class file with same file name. (eg: Hello.class)

This class file is a compiled code that can be executed by java command and the class name

java Hello

When you run the above program the output will be,

Welcome To Neural Beast’s Java Tutorial

Code Explanation

class Hello

class is an keyword, which is used to declare a class in java.

public static void main(String[] args)

public is access specifier, this will visible to all.

Static keyword is used for memory management, we can use static with variable, blocks, nested classes and methods. static is used for constant variable.

void doesn’t return any value.

main() code inside the main method will be executed, every program must contain a main method.

String[] args is used to store java command line arguments and it is an array type java.

System.out.println("Welcome To Neural Beast's Java Tutorial");

This is a printing statement in java. System is a class in java.lang package, out is a variable which is a static member of system class and println() is a method of java.io.PrintStream.

Keywords

Keywords have special meaning to the language compiler. These are reserved word. In java there are 51 reserved keywords, 49 are in use and 2 are mot in use, keywords cannot be used as a variable, method, class or identifier.

abstract assert boolean
break byte case
catch char class
record continue default
do double else
enum extends final
finally float for
yield if implements
import instanceof int
interface long native
new package private
protected public return
short static strictfp
super switch synchronized
this throw throws
transient try void
volatile while var
Keywords