Java Basics

Java is a powerful, versatile, and platform-independent programming language. This guide covers the basic concepts and syntax to get you started on your journey with Java development.

Basic Syntax Breakdown

Let's look at a simple Java program. Every line of code that can run is inside a class. The `main()` method is the entry point of any Java application.

Main.java

public class Main {
  public static void main(String[] args) {
    System.out.println("This is a basic Java program.");
  }
}
            

Case Sensitivity

Java is case-sensitive. This means that identifiers myVariable and myvariable are completely different in Java.

Naming Conventions

Java follows specific naming conventions to maintain readability:

  • Classes: Start with an uppercase letter and follow CamelCase (e.g., MyFirstJavaClass).
  • Methods: Start with a lowercase letter and follow camelCase (e.g., myFirstMethod).
  • Variables: Start with a lowercase letter and follow camelCase (e.g., myVariableName).
  • Constants: All uppercase letters, with words separated by underscores (e.g., MAX_SIZE).

The `main` Method

The public static void main(String[] args) method is the mandatory entry point for any Java application. Let's break it down:

  • public: It is an access modifier, which means it is visible to all.
  • static: It is a keyword that means the method can be invoked without creating an instance of the class.
  • void: It is a keyword that specifies that a method should not have a return value.
  • main: It is the name of the method.
  • String[] args: This stores Java command-line arguments.

Java Keywords

Java has a set of reserved keywords that cannot be used as identifiers (like variable names, method names, etc.). Here are a few common ones:

publicclassstaticvoidintdoublebooleanifelseforwhilereturn