Java Syntax and Structure with Examples


 

Java Syntax and Structure with Examples

1. Java Program Structure (Classes and Methods)

  • Understanding Java Classes

    • A class is a blueprint for creating objects in Java. Here’s a simple example of a Java class:
      java

      public class MyFirstClass { // This is a member variable int number = 5; // This is a method public void displayNumber() { System.out.println("Number: " + number); } }
    • In this example, MyFirstClass is a Java class, number is a variable, and displayNumber is a method that prints the value of number.
  • The Main Method

    • The main method is the entry point for any Java application. Here’s a simple "Hello, World!" program:
      java

      public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
    • In this code, public static void main(String[] args) is the main method, where execution starts. System.out.println("Hello, World!"); prints text to the console.
  • Creating and Calling Methods

    • Methods allow us to break down code into smaller, reusable parts. Here’s an example with method declaration and invocation:
      java
      public class MathOperations {
      // Method to add two numbers public int add(int a, int b) { return a + b; } public static void main(String[] args) { MathOperations math = new MathOperations(); int result = math.add(5, 3); System.out.println("Sum: " + result); } }
    • Here, add is a method that takes two parameters (int a and int b), adds them, and returns the sum. The main method calls add, and the result is printed.

2. Comments in Java

  • Single-line Comments (//)
    java

    // This is a single-line comment explaining the line below System.out.println("Hello, World!");
  • Multi-line Comments (/* ... */)
    java

    /* * This is a multi-line comment. * It can span multiple lines. */ System.out.println("Java comments are helpful!");
  • Documentation Comments (/** ... */)
    java

    /** * This method returns the sum of two numbers. * @param a The first number * @param b The second number * @return The sum of a and b */ public int add(int a, int b) { return a + b; }
    • Documentation comments like these are used for creating JavaDocs.

3. Java Statements

  • Example of Variable Declaration and Assignment Statements
    java

    int number = 10; // Declaring and initializing a variable String message = "Java is fun!"; System.out.println(message);
  • Syntax Rules
    • Every statement ends with a semicolon (;). Code blocks are wrapped in braces ({}). For example:
      java

      if (number > 5) { System.out.println("Number is greater than 5"); }

4. Java Keywords

  • Common Keywords Example

    java

    public class Example { public static void main(String[] args) { int number = 10; // 'int' is a keyword for integer type final int CONSTANT = 100; // 'final' makes CONSTANT unchangeable System.out.println(number + CONSTANT); } }
    • Here, public, class, static, void, int, and final are keywords.
  • Reserved Words in Java

    • Words like new, if, else, switch, etc., are reserved in Java and cannot be used as variable names. Here’s an example using if and else:
      java

      public class CheckEvenOdd { public static void main(String[] args) { int number = 4; if (number % 2 == 0) { System.out.println("Number is even"); } else { System.out.println("Number is odd"); } } }
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.