Syntax - Basics

Basic Data Types

  1. Java has built in primitive data types like int, long, char etc.
  2. Also, it has wrapper classes for these primitive types, eg. Integer, Long etc.
  3. Java provides Autoboxing and Unboxing for these types.
  4. Java has built-in array. Although on most scenarios Collection Framework's ArrayList will be good alternative.
  5. It has String data type.
  6. Also, Java has Class, Interface and Methods.
// Primitive data types
int anInt = 1000;
long aLong = 100000L;
float aFloat = 10.5f;
double aDouble = 20.99;
boolean aBoolean = true;
char ch = 'a';

// Reference (Object) data types

// string
String str = "Hello world";

//array
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println("Element at index " + i + ": " + numbers[i]);
}

// Wrapper classes : wrapper for primitive data types
Integer a = 12;
Long b = 11L;
Float c = 12.2f;
Double d = 12.2;
Character e = 'c';
Boolean f = true;

/*
  Java provides autoboxing and unboxing features, which automatically convert between
  primitive types and their corresponding wrapper classes.
 */
int num1 = 10;
Integer intObj = num1;  // Autoboxing: primitive int to Integer
int unboxedInt = intObj;  // Unboxing: Integer to primitive int

Class and Method

// A simple class definition
class Person {

    // Instance variables (fields)
    private String name;  // Private access modifier
    private int age;      // Private access modifier
    long value = 12l;     // Default access modified

    // Constructor method to initialize the object
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method with default access modifier (accessible within this class and within same package)
    void sayHello() {
        System.out.println(name + " is saying hello");
    }

    // Method with public access modifier
    public void introduce() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }

    // Method with private access modifier (only accessible within this class)
    private void secretMethod() {
        System.out.println("This is a secret method.");
    }

    // Getter method with public access modifier
    public String getName() {
        return name;
    }

    // Setter method with public access modifier
    public void setName(String name) {
        this.name = name;
    }

    // Main method - entry point of the program
    public static void main(String[] args) {
        // Creating an object of Person class
        Person person = new Person("Alice", 30);

        // Calling the public method
        person.introduce();

        // Accessing getter method
        System.out.println("Person's name is: " + person.getName());
    }
}

Rules for class:

  1. One java file can contain only one public class. File name should be same as the public class name, eg. Person.java

Access Modifiers:

NameDescription
publicThe public access modifier allows the class, method, or variable to be accessed from anywhere—inside or outside the package
privateThe private access modifier restricts the visibility of methods and variables to the class in which they are defined.
protectedThe protected access modifier allows the class, method, or variable to be accessed within the same package and by subclasses (even if they are in different packages).
default (no modifier)Members with default access are visible only within the same package, but not from outside the package. This is often used for methods and variables that are meant to be package-private.

Interface: An interface is a completely "abstract class" that is used to group related methods with empty bodies

interface Vehicle {
    void start();     // abstract method
    void stop();      // abstract method
}

class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("Car is starting");
    }

    @Override
    public void stop() {
        System.out.println("Car is stopping");
    }
}