![]() |
VOOZH | about |
Java is an object-oriented programming language that is known for its simplicity, portability, and robustness. The syntax of Java programming language is very closely aligned with C and C++, which makes it easier to understand.
Java Syntax refers to a set of rules that define how Java programs are written and interpreted by the compiler. These rules ensure that your code is readable, logically correct, and error-free. Now, let's understand the syntax and structure of Java programs with a basic "Hello World" program.
A basic Java program consists of several components that create a functional application. We can learn about basic Java syntax using the following program:
Hello World
Explanation: The above program shows the basic Java program that contains class declaration, main method, statements, etc. Let’s try to understand them one by one.
Note: In the above code, although we did not explicitly import any package, the java.lang package is automatically imported by default in every Java program. This package contains essential classes like System, Math, and String, which is why we can use System.out.println("Hello World"); without any additional imports.
1. Class Declaration: Every Java program starts with a class declaration using the class keyword. "A class is a blueprint of an object" and we can also define class as a logical template that shares common properties and methods.
2. Object: The object is an instance of a class. It is an entity that has behavior and state.
3. Main Method: The public static void main(String args[]) method is the entry point where the program starts execution.
4. Statements: Each line of code inside the method must end with a semicolon(;)
1. Run the javac command to compile the Java Program
javac Geeks.java
The Java compiler(javac) reads the source code(Geeks.java) and generates a bytecode file(Geeks.class) in the same directory
2. Use the java command to execute the compiled bytecode
java Geeks
Note:
There are three types of comments in Java.
// This is a single-line comment
/* This is
a multi-line comment */
It is also known as a doc comment
/** This is a doc comment */
Source File Name Rule in Java.
1. When there is a public class in the file
The name of a source file must exactly match the name of the public class name with the extension. java
Example: Assume you have a public class named Geeks.
Note:
2. When there is no public class in the file
The source file name can be anything, but it must have the .java extension.
Here, the class is not declared as public so you can make the file name anything like Example.java, Program.java etc
Java is a case-sensitive language, which means that the identifiers AB, Ab, aB, and ab are different in Java.
System.out.println("Hello World"); // valid syntax
system.out.println("Hello World"); // invalid syntax because of the first letter of System keyword is always uppercase.
class MyJavaProgram // valid syntax
class 1Program // invalid syntax
class My1Program // valid syntax
class $Program // valid syntax, but discouraged
class My$Program // valid syntax, but discouraged (inner class Program inside the class My)
class myJavaProgram // valid syntax, but discouraged
public void employeeRecords() // valid syntax
public void EmployeeRecords() // valid syntax, but discouraged
Identifiers are the names of local variables, instance and class variables, and labels, but also the names for classes, packages, modules and methods.
Rules for defining the Identifiers:
A line containing only white spaces, possibly with the comment, is known as a blank line, and the Java compiler ignores it.
These modifiers control the scope of class and methods.
| Access Modifier | Within Class | Within Package | Outside Package by subclass only | Outside Package |
| Private | Yes | No | No | No |
| Default | Yes | Yes | No | No |
| Protected | Yes | Yes | Yes | No |
| Public | Yes | Yes | Yes | Yes |
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects.
Note: goto and const are reserved keywords in Java. It means we cannot use them as identifiers. They are not implemented and have no functionality in the language.
Category | Keywords |
|---|---|
Data Types | byte, short, int, long, float, double, boolean, char |
Access Modifiers | public, private, protected |
Control Flow | if, else, switch, case, default, while, do, for, break, continue, return |
Error Handling | try, catch, finally, throw, throws |
Object-Oriented | class, interface, extends, implements, this, super |
Memory/Threads | new, static, final, abstract, synchronized, volatile, transient, native, strictfp |
Other Useful | instanceof, import, package, assert, enum, void |