WikiGalaxy

Personalize

Java Syntax: Introduction

Overview:

Java syntax forms the set of rules and guidelines that define how a Java program is written and interpreted by the Java compiler. It includes the structure of code, the use of keywords, operators, data types, and control flow statements.

Example 1: Basic Java Program

Point Heading:

This example demonstrates a simple Java program that prints "Hello, World!" to the console. This is typically the first program written by beginners to understand the basic structure of a Java application.


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

Console Output:

Hello, World!

Example 2: Variables and Data Types

Point Heading:

Java supports various data types to store different kinds of data. This example illustrates the declaration and initialization of different variable types such as int, double, and String.


public class VariablesExample {
    public static void main(String[] args) {
        int number = 10;
        double price = 19.99;
        String name = "Java";
        System.out.println("Number: " + number);
        System.out.println("Price: " + price);
        System.out.println("Name: " + name);
    }
}
        

Console Output:

Number: 10

Price: 19.99

Name: Java

Example 3: Conditional Statements

Point Heading:

Conditional statements allow you to execute specific code blocks based on certain conditions. This example shows how to use if-else statements in Java.


public class ConditionalExample {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18) {
            System.out.println("You are an adult.");
        } else {
            System.out.println("You are not an adult.");
        }
    }
}
        

Console Output:

You are an adult.

Example 4: Loops in Java

Point Heading:

Loops are used to execute a block of code repeatedly. This example demonstrates the use of a for loop to print numbers from 1 to 5.


public class LoopExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Number: " + i);
        }
    }
}
        

Console Output:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

Example 5: Arrays in Java

Point Heading:

Arrays are used to store multiple values of the same type in a single variable. This example shows how to declare, initialize, and access array elements.


public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Element at index " + i + ": " + numbers[i]);
        }
    }
}
        

Console Output:

Element at index 0: 10

Element at index 1: 20

Element at index 2: 30

Element at index 3: 40

Element at index 4: 50

Example 6: Methods in Java

Point Heading:

Methods are blocks of code that perform a specific task and can be called by their name. This example illustrates how to define and call a method in Java.


public class MethodExample {
    public static void main(String[] args) {
        greetUser("Alice");
    }

    public static void greetUser(String name) {
        System.out.println("Hello, " + name + "!");
    }
}
        

Console Output:

Hello, Alice!

Example 7: Object-Oriented Concepts

Point Heading:

Java is an object-oriented programming language. This example demonstrates the creation of a simple class and object, showcasing the concepts of encapsulation and instantiation.


class Car {
    String color;
    int year;

    void displayInfo() {
        System.out.println("Color: " + color + ", Year: " + year);
    }
}

public class OOPExample {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.color = "Red";
        myCar.year = 2020;
        myCar.displayInfo();
    }
}
        

Console Output:

Color: Red, Year: 2020

Example 8: Exception Handling

Point Heading:

Exception handling in Java allows a program to handle runtime errors and maintain normal flow. This example highlights the use of try-catch blocks to manage exceptions.


public class ExceptionExample {
    public static void main(String[] args) {
        try {
            int division = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero.");
        }
    }
}
        

Console Output:

Cannot divide by zero.

Example 9: Inheritance

Point Heading:

Inheritance allows a class to inherit fields and methods from another class. This example demonstrates how a subclass inherits properties from a superclass.


class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking...");
    }
}

public class InheritanceExample {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();
    }
}
        

Console Output:

Eating...

Barking...

Example 10: Interfaces

Point Heading:

Interfaces in Java provide a way to achieve abstraction. This example shows how to implement an interface in a class.


interface Animal {
    void makeSound();
}

class Cat implements Animal {
    public void makeSound() {
        System.out.println("Meow");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.makeSound();
    }
}
        

Console Output:

Meow

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025