Java Basic Syntax

Java is a popular programming language that is widely used for building a variety of applications, including web, mobile, and desktop applications. It is known for its simplicity, reliability, and portability, which makes it a good choice for beginners as well as experienced developers. In this article, we will provide an overview of some basic syntax rules for writing Java code. These rules will help you get started with writing Java programs and provide a foundation for learning more advanced concepts.

Java Basic Syntax With Example

Here are some basic syntax rules for writing Java code:

  1. Class names should be written in UpperCamelCase, with the first letter of each word capitalized. For example: MyClass, HelloWorld.
  2. Method names should be written in lowerCamelCase, with the first letter of each word except the first one capitalized. For example: doSomething, getData.
  3. Variable names should also be written in lowerCamelCase, with the first letter of each word except the first one capitalized. For example: firstName, customerAddress.
  4. Java is a case-sensitive language, so firstName and firstname are treated as two different variables.
  5. A statement in Java must end with a semicolon (;).
  6. Java code is typically written inside a class, and a class can contain fields (variables) and methods (functions).

Here is a simple example of a Java class that has a field and a method:

public class MyClass {
  // field
  private String name;
  
  // method
  public void setName(String name) {
    this.name = name;
  }
}

This class has a field called name and a method called setName that sets the value of the name field.

Note: This is just a basic overview of Java syntax. There are many more details and features that you will need to learn in order to write Java code effectively.

Here is an example of a Java program that demonstrates some basic syntax rules:

public class HelloWorld {
  // field
  private String message;
  
  // constructor
  public HelloWorld(String message) {
    this.message = message;
  }
  
  // method
  public void greet() {
    System.out.println(message);
  }
  
  // main method
  public static void main(String[] args) {
    HelloWorld hw = new HelloWorld("Hello, World!");
    hw.greet();
  }
}

In this example, we have a class called HelloWorld with a field called message, a constructor, a method called greet, and a main method.

The message field is a variable that is used to store a string value. It is marked as private, which means that it can only be accessed within the HelloWorld class.

The constructor is a special method that is used to create an instance of the HelloWorld class. It has the same name as the class and is used to initialize the object. In this case, the constructor takes a string parameter called message and assigns it to the message field using the this keyword.

The greet method is a simple method that prints the value of the message field to the console using the System.out.println method.

The main method is the entry point of the Java program. It is a special method that is automatically called when the program is executed. In this case, the main method creates a new instance of the HelloWorld class using the constructor and calls the greet method on it.

When this program is run, it will print “Hello, World!” to the console.

Similar Posts

Leave a Reply

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