DEV Community

Cover image for Java Basic Syntax
Melody Mbewe
Melody Mbewe

Posted on • Edited on

Java Basic Syntax

Java is one of the most popular programming languages in the world, known for its simplicity, portability, and robustness. Whether you're developing web applications, mobile apps, or enterprise-level software, understanding Java syntax is key to success. This guide covers essential Java syntax fundamentals that every beginner should know, helping you build a solid foundation for your Java programming journey.

1. Basic Structure of a Java Program
A typical Java program follows a simple structure that consists of classes and methods. Below is a basic example of a "Hello, World!" program:

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

Key Components:

- Class: Every Java program contains at least one class. In this case, it's called HelloWorld.

*- Method: * The main method is the starting point of the program and executes when the application runs.

- System.out.println: This prints output to the console. In this case, it prints "Hello, World!".

- Question for you: Can you think of another way to structure a basic program in Java? What would you change?

- 2. Comments
Comments are notes added to your code for clarity, and they are ignored by the compiler. There are two types of comments in Java:

- Single-line comment: // for single-line notes.

// This is a single-line comment
Enter fullscreen mode Exit fullscreen mode

- Multi-line comment: /* */ for multiple lines.

/* This is
   a multi-line
   comment */
Enter fullscreen mode Exit fullscreen mode

Interactive challenge: Try writing comments in your own code! It’s a great habit to explain your logic, especially in complex parts of the program.

3. Data Types and Variables
In Java, you must declare the type of data a variable holds. Here are some commonly used data types:

int: For whole numbers.

int age = 25;
Enter fullscreen mode Exit fullscreen mode

- double: For decimal numbers.

double price = 9.99;
Enter fullscreen mode Exit fullscreen mode

- char: For single characters.

char grade = 'A';
Enter fullscreen mode Exit fullscreen mode

- boolean: For true/false values.

boolean isJavaFun = true;
Enter fullscreen mode Exit fullscreen mode

- String: For text.

String greeting = "Hello";
Enter fullscreen mode Exit fullscreen mode

What do you think? Which data types would you use for a simple calculator program? Share your ideas below!

4. Operators
Operators perform operations on variables and values. Some common operators include:

  • Arithmetic: +, -, *, /, %.
int sum = 10 + 5;
Enter fullscreen mode Exit fullscreen mode
  • Comparison: ==, !=, >, <, >=, <=.
boolean isEqual = (5 == 5);
Enter fullscreen mode Exit fullscreen mode

- Logical: && (AND), || (OR), ! (NOT).

boolean result = (5 > 3 && 8 > 5);
Enter fullscreen mode Exit fullscreen mode

Can you test this? Create a program that compares two numbers and prints whether they are equal or not.

5. Control Flow Statements
Control flow statements let you control the execution based on conditions.

If-else statement

if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are a minor.");
}
Enter fullscreen mode Exit fullscreen mode

Switch statement

int day = 2;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println("Invalid day");
}
Enter fullscreen mode Exit fullscreen mode

Discussion: How would you use a switch statement to handle a calculator's operations (addition, subtraction, etc.)?

6. Loops
Loops repeat a block of code multiple times.

For Loop

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

While Loop

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Do-While Loop

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 5);
Enter fullscreen mode Exit fullscreen mode

Task for you: Which loop structure do you think is best for reading a list of student names? Let’s discuss!

7. Arrays
Arrays store multiple values in a single variable.

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]);  // Output: 1
Enter fullscreen mode Exit fullscreen mode

Arrays can also be declared without initialization:

int[] numbers = new int[5];  // Creates an array of size 5
numbers[0] = 10;
Enter fullscreen mode Exit fullscreen mode

Try this: Create an array to store the scores of five students. How would you access and display the highest score?

8. Methods
Methods are blocks of code that only run when called.

public class MyClass {
    public static void main(String[] args) {
        greet();
    }

    public static void greet() {
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Method Parameters

public static void greet(String name) {
    System.out.println("Hello, " + name);
}
Enter fullscreen mode Exit fullscreen mode

What’s your approach? Create a method that takes two integers as input and returns their sum. What kind of scenarios could you use this method in?

9. Object-Oriented Programming (OOP) Concepts
Java is an object-oriented language, so it relies on principles like encapsulation, inheritance, and polymorphism.

Class: A blueprint for creating objects.
Object: An instance of a class.

Example:

class Dog {
    String name;
    int age;

    void bark() {
        System.out.println("Woof!");
    }
}

Dog myDog = new Dog();
myDog.name = "Rex";
myDog.bark();
Enter fullscreen mode Exit fullscreen mode

Can you imagine? How would you extend this Dog class to include more behaviors (like fetching or rolling over)? What methods would you add?

Conclusion
Java syntax is clean, structured, and designed to be beginner-friendly. Once you grasp the basics, you’ll find it easier to write more complex programs, exploring advanced features like inheritance, interfaces, and multithreading. By practicing the essentials, you'll build the foundation for more challenging and exciting Java projects.

Let's Engage!
Now that you've learned the basics of Java syntax, try writing a small program of your own. What did you build? Share your experience in the comments, or feel free to ask any questions you have!

Also, what part of Java syntax do you find most intriguing?

Top comments (0)