Mastering Java Basics: From Syntax to Object-Oriented Programming
1. Basic Syntax and Structure
Every Java program starts with a class definition, followed by a `main` method which serves as the entry point. Java syntax is largely similar to C and C++, with statements ending in semicolons, and code blocks being enclosed within curly braces `{}`.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Data Types, Variables, and Operators
Java supports various data types including primitive types (`int`, `char`, `boolean`, `double`) and reference types (such as objects). Variables are used to store values that can be manipulated using operators like `+`, `-`, `*`, and `/`.
Example:
int age = 25;
double salary = 50000.50;
boolean isEmployed = true;
char grade = 'A';
3. Control Structures (if-else, loops, switch)
Java provides control structures for decision-making (`if-else`), looping (`for`, `while`, `do-while`), and branching (`switch-case`). These allow you to control the flow of execution based on conditions.
Example:
int number = 10;
if (number > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
}
4. Object-Oriented Programming (OOP)
Java is an object-oriented programming language. This means that concepts such as encapsulation, inheritance, polymorphism, and abstraction are core to the language. Classes define objects and methods define behavior.
Classes and Objects
A class is a blueprint for creating objects. Objects are instances of a class. Classes encapsulate data (fields) and methods (behavior) into a single unit.
Example:
class Car {
String model;
int year;
void start() {
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.model = "Tesla";
myCar.year = 2024;
myCar.start();
}
}
Inheritance
Inheritance allows a class (child or subclass) to inherit fields and methods from another class (parent or superclass). This promotes code reusability.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.bark(); // Method from Dog
}
}
Polymorphism
Polymorphism allows one interface to be used for different data types. In Java, polymorphism can be achieved through method overloading and method overriding.
Example (Method Overriding):
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Cat();
myAnimal.sound();
}
}
Encapsulation
Encapsulation involves bundling data (variables) and methods that operate on the data into a single unit (class) and restricting outside access using access modifiers like `private`, `protected`, and `public`.
Example:
class Person {
private String name;
private int age;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("Alice");
System.out.println(person.getName());
}
}
Abstraction
Abstraction hides implementation details and only exposes functionality. In Java, abstraction can be achieved using abstract classes and interfaces.
Example:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound();
}
}