Basic Java Syntax

When writing Java code, proper syntax is very crucial because even a small typo could result in an error message.

The name of the class and the java file should match exactly. Additionally, every line of code must be contained within a class.

A programme with different file and class names is an example.

package syntax1;

public class DEtails {
    public static void main(String[] args) {
        System.out.println("Java program with diff file name and class name");
      }
}

Output:

The public type Details must be defined in its own file.

A programme with the same file name and class name is an example.

package syntax1;

public class Details {
    public static void main(String[] args) {
        System.out.println("Java program with same file name and class name");
      }
}

Output:

Java program with same file name and class name

As we can see from both cases, even the tiniest modification to the filename or class name results in an error.

The curly brackets include an indentation for each block of code.

See how each block is indented inside the parent block as an illustration.

package syntax1;

public class Details {
    public static void main(String[] args) {
        System.out.println("Java program with same file name and class name");
      }
}

A main method, which is required for java file execution, must be present in every java file.

For instance, Java programming without a main method will fail.

package syntax1;

public class Details {
    System.out.println("Java program with same file name and class name");
}

Output:

Build failed
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in