Java Syllabus

Java Syllabus

Data Types

Definition: Data types specify the type of data that a variable can hold. Java is a statically-typed language, which means you must declare a variable’s type before using it.

Explanation: Java provides two main types of data types: primitive and reference types.

Primitive Data Types:

  • byte: 8-bit integer
  • short: 16-bit integer
  • int: 32-bit integer
  • long: 64-bit integer
  • float: 32-bit floating-point
  • double: 64-bit floating-point
  • char: 16-bit Unicode character
  • boolean: true or false

Reference Data Types: These include objects and arrays.

Example:

public class DataTypesExample {
            public static void main(String[] args) {
                int num = 10;
                double price = 19.99;
                char letter = 'A';
                boolean isJavaFun = true;
        
                System.out.println("Integer: " + num);
                System.out.println("Double: " + price);
                System.out.println("Character: " + letter);
                System.out.println("Boolean: " + isJavaFun);
            }
        }

Variables

Definition: Variables are containers for storing data values. Each variable in Java must be declared with a data type.

Explanation: Variables are used to store data that can be manipulated later in the program. They can be of primitive types or reference types.

Example:

public class VariablesExample {
            int instanceVariable = 5; // Instance variable
        
            public static void main(String[] args) {
                int localVariable = 10; // Local variable
                VariablesExample example = new VariablesExample();
                
                System.out.println("Local Variable: " + localVariable);
                System.out.println("Instance Variable: " + example.instanceVariable);
            }
        }

Loops

Definition: Loops are used to execute a block of code repeatedly based on a condition.

Explanation: Java supports several types of loops: for loop, enhanced for loop (for-each), while loop, and do-while loop.

For Loop:

for (int i = 0; i < 5; i++) {
            System.out.println("Iteration: " + i);
        }

Enhanced For Loop (For-Each):

int[] numbers = {1, 2, 3, 4, 5};
        for (int number : numbers) {
            System.out.println("Number: " + number);
        }

While Loop:

int i = 0;
        while (i < 5) {
            System.out.println("Iteration: " + i);
            i++;
        }

Do-While Loop:

int i = 0;
        do {
            System.out.println("Iteration: " + i);
            i++;
        } while (i < 5);

Conditions

Definition: Conditions control the flow of the program based on certain criteria.

Explanation: Java supports conditional statements like if, if-else, and switch to make decisions in the code.

If-Else Statement:

int number = 10;
        if (number > 0) {
            System.out.println("Number is positive.");
        } else {
            System.out.println("Number is non-positive.");
        }

Switch Statement:

int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            default:
                System.out.println("Other day");
                break;
        }

Operators

Definition: Operators perform operations on variables and values.

Explanation: Java supports several types of operators: arithmetic, relational, logical, and assignment operators.

Arithmetic Operators:

int a = 10;
        int b = 5;
        System.out.println("Sum: " + (a + b)); // Output: 15
        System.out.println("Difference: " + (a - b)); // Output: 5
        System.out.println("Product: " + (a * b)); // Output: 50
        System.out.println("Division: " + (a / b)); // Output: 2
        

Relational Operators:

System.out.println("Is a greater than b? " + (a > b)); // Output: true
        

Logical Operators:

System.out.println("Is a greater than 5 and b less than 10? " + (a > 5 && b < 10)); // Output: true
        

Object-Oriented Programming (OOP)

Definition: OOP is a programming paradigm based on the concept of objects, which can contain data and code.

Explanation: Java uses OOP principles to create organized and reusable code. Key concepts include:

  • Encapsulation: Bundling data and methods that operate on the data within a single unit (class). Example: private fields with public getter and setter methods.
  • Abstraction: Hiding complex implementation details and showing only the essential features. Example: abstract classes and methods.
  • Inheritance: Mechanism where a new class is derived from an existing class. Example: `extends` keyword.
  • Polymorphism: Ability to take many forms, e.g., method overriding and method overloading.
  • Interface: A contract that classes can implement. Example: `implements` keyword.
  • Method Overloading: Multiple methods with the same name but different parameters.
  • Method Overriding: Redefining a method in a subclass that already exists in the superclass.

Encapsulation Example:

public class Person {
            private String name; // Private field
        
            public String getName() { // Getter method
                return name;
            }
        
            public void setName(String name) { // Setter method
                this.name = name;
            }
        }

Inheritance Example:

public class Animal {
            public void eat() {
                System.out.println("This animal eats food.");
            }
        }
        
        public class Dog extends Animal {
            @Override
            public void eat() {
                System.out.println("This dog eats bones.");
            }
        }

Polymorphism Example:

public class PolymorphismExample {
            public static void main(String[] args) {
                Animal myDog = new Dog();
                myDog.eat(); // Calls Dog's eat method
            }
        }

Interface Example:

interface Drawable {
            void draw();
        }
        
        public class Circle implements Drawable {
            @Override
            public void draw() {
                System.out.println("Drawing a circle.");
            }
        }

Method Overloading Example:

public class MathOperation {
            public int add(int a, int b) {
                return a + b;
            }
        
            public double add(double a, double b) {
                return a + b;
            }
        }

Method Overriding Example:

public class Animal {
            public void sound() {
                System.out.println("Animal makes a sound.");
            }
        }
        
        public class Cat extends Animal {
            @Override
            public void sound() {
                System.out.println("Cat meows.");
            }
        }

Arrays

Definition: Arrays are containers that hold a fixed number of values of a single type.

Explanation: Arrays are indexed from 0 and have a fixed size. Once created, their size cannot be changed.

Example:

public class ArraysExample {
            public static void main(String[] args) {
                int[] numbers = {1, 2, 3, 4, 5};
                
                for (int i = 0; i < numbers.length; i++) {
                    System.out.println("Number at index " + i + ": " + numbers[i]);
                }
            }
        }

Linked Lists

Definition: A linked list is a data structure where each element (node) points to the next element.

Explanation: Linked lists allow efficient insertions and deletions but have a slower access time compared to arrays.

Example:

import java.util.LinkedList;
        
        public class LinkedListExample {
            public static void main(String[] args) {
                LinkedList list = new LinkedList<>();
                list.add("Java");
                list.add("Python");
                list.add("C++");
                
                for (String language : list) {
                    System.out.println("Language: " + language);
                }
            }
        }

HashMaps

Definition: HashMap is a collection that stores key-value pairs with unique keys.

Explanation: HashMap provides efficient insertion, deletion, and access operations based on keys.

Example:

import java.util.HashMap;
        
        public class HashMapExample {
            public static void main(String[] args) {
                HashMap map = new HashMap<>();
                map.put("1", "Java");
                map.put("2", "Python");
                map.put("3", "C++");
                
                for (String key : map.keySet()) {
                    System.out.println("Key: " + key + ", Value: " + map.get(key));
                }
            }
        }

LinkedHashMaps

Definition: LinkedHashMap is a HashMap with predictable iteration order (insertion order).

Explanation: It combines the features of HashMap and LinkedList, providing predictable iteration order and efficient key-based access.

Example:

import java.util.LinkedHashMap;
        
        public class LinkedHashMapExample {
            public static void main(String[] args) {
                LinkedHashMap map = new LinkedHashMap<>();
                map.put("1", "Java");
                map.put("2", "Python");
                map.put("3", "C++");
                
                for (String key : map.keySet()) {
                    System.out.println("Key: " + key + ", Value: " + map.get(key));
                }
            }
        }

Sets

Definition: Sets are collections that do not allow duplicate elements.

Explanation: Java provides several implementations of the Set interface, including HashSet, LinkedHashSet, and TreeSet, each with different properties regarding order and performance.

Example:

import java.util.HashSet;
        
        public class HashSetExample {
            public static void main(String[] args) {
                HashSet set = new HashSet<>();
                set.add("Java");
                set.add("Python");
                set.add("C++");
                
                for (String language : set) {
                    System.out.println("Language: " + language);
                }
            }
        }
Drag to resize