Login Register
×

What Is The Java Syntax?

Java programming is like a grammar in a language. It has certain rules for writing the code. If you break even a small rule, like missing a semicolon or typing something in the wrong order, Java will give you an error on the screen.

Java syntax is essential because small syntax errors can prevent your program from running.

Basic Structure of a Java Program

Every Java program follows a specific structure. Hereโ€™s a simple example of a basic Java program:

Code Example:

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

Explanation of this code:

public class HelloWorld โ†’ This line makes a class name “HelloWorld”. The class name must match the file name in Java, so if your class is HelloWorld, your file should be HelloWorld.java.

public static void main(String[] args) โ†’ This is called main method. It is the “starting point” of your program. Java looks at this line first when it runs your code; anything inside its curly brackets { } will be executed.

System.out.println(“Hello, World!”); โ†’ It is a statement. This line instructs Java to print the “Hello World!” message on the screen.

Understand each word of the Java syntax:

1) public

  • It means anyone can access that class form anywhere.
  • Without the public, other parts of the program would not be able to run it.
  • For example, if you write a public word before the class or main method, it is visible to all parts of the program.

2) class

  • A class is a blueprint for creating objects in Java.
  • We have types public class HelloWord, which means we are creating a class (blueprint) named HelloWorld.

3) static

  • It belongs to the class itself, not to an object.
  • static allows Java to run the method without creating an object first.
  • If you don’t write static in the main method, the program won’t start automatically.

4) void

  • void means, no value will be returned from this method.
  • If the main method is not void, then it would have to return something.
  • For example, void says, โ€œThis method will just run the code and not give back a value.โ€

5) main

  • The main method is the starting point of any Java program.
  • When you run the program, Java looks for main first.
  • if the main is missing, the program won’t start.

6) String[] args

  • This allows the program to receive extra data (arguments) from the command line when it starts.
  • For example:
java HelloWorld Hello GPT
  • If you run the above code, then args[0] will be “Hello” and args[1] will be “GPT”.

7) System.out.println

  • System: It is a built-in Java class that handles system-related tasks.
  • out: An object in System that sends output to the console.
  • println: It is a method that prints text and moves the cursor to the next line.

Why always use System.out.println?

Because it is the official Java way to send text to the console. It’s like telling Java: โ€œHey, System! Send this text to the standard output (the console) and move to the next line.โ€

Important Rules of Java Syntax

  • Case Sensitivity
  • Class Names
  • Method Names
  • Curly Braces { }

1) Case Sensitivity:

Java programming is case-sensitive. Meaning, Java treats uppercase and lowercase letters as different scenarios.

  • For example, HelloWorld and helloworld are not the same in Java.

2) Class Names:

  • The Java class name should always start with a capital letter.
  • If the class name has multiple words, you can use the PascalCase method (capitalize each word). For example:
public class MyFirstProgram { }  

Here, you can see that each word starts with a capital letter in the class name.

3) Method Names:

  • Method names should start with a lowercase letter.
  • If you have multiple words, use camelCase (first word lowercase and subsequent words capitalized. For example:
public void printMessage() { }  

4) Curly Braces { }:

  • Curly braces mark the start and end of a block of code.
  • { } Combine all the code and treat it as one unit. For example:
public class Example {
public static void main(String[] args) {
System.out.println("Java Syntax");
}
}

Remember, if you miss any opening or closing brackets, it will give an error for the small mistake.

5) Semicolon ; :

  • In Java programming, every statement must end with a semicolon. If you miss it, you will get a syntax error.
  • For example:
System.out.println("Hello"); // Correct
System.out.println("Hello") // Error - missing ;

6) Comments:

  • Comments are the notes that you can add to your code to explain what kind of program or classes you use.
  • Types of comments in Java:
    • Single-line comment: // This is a single-line comment
    • Multi-line comment: /* This is a multi-line comment */
    • Documentation comment: /** * This is a documentation comment * for generating API docs. */
  • We can easily write the different types of comments in Java using these symbols:

Single-line comment โ†’ It is starts with //

// This prints Hello
System.out.println("Hello");

Multi-line comment โ†’ It contain multi line /* and */

/* This is a multi-line comment
It can span multiple lines */

Documentation comment โ†’ between /** and */

/**
* This is a documentation comment.
* It can be used for generating API docs.
*/

Java Syntax Example

Now, we have created a Java program that contains multiple syntax rules:

Code Example:

public class StudentDetails {  
public static void main(String[] args) {
// Variable declaration
String name = "John";
int age = 30;
double marks = 75.3;

// Output details
System.out.println("Student Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
}

In this code:

  1. Variable declaration: Declares variables of type String, int and double.
  2. Output: Uses System.out.println() to display variable values.

Learn Further Topics: