JAVA Notes
JAVA Notes
1. Overview of Java
• public class HelloWorld: Defines a class named HelloWorld. The class name must match the
filename.
• public static void main(String[] args): The entry point for any Java application. This method is
executed when the program runs.
• System.out.println(): Prints output to the console.
3. Data Types
• Primitive Types:
o byte: 1 byte, range -128 to 127.
o short: 2 bytes, range -32,768 to 32,767.
o int: 4 bytes, range -2^31 to 2^31-1.
o long: 8 bytes, range -2^63 to 2^63-1.
o float: 4 bytes, single-precision floating point.
o double: 8 bytes, double-precision floating point.
o char: 2 bytes, single character (Unicode).
o boolean: Represents true or false.
• Non-Primitive Types:
o Arrays, Strings, Objects.
• Conditionals:
o if, else if, else: Standard conditional checks.
o switch: Used for checking multiple conditions based on a single variable.
• Loops:
o for: Used when the number of iterations is known.
o while: Used when the number of iterations is not known but the condition is checked before
each iteration.
o do-while: Similar to while, but checks the condition after each iteration.
• Break and Continue: break exits a loop, and continue skips the current iteration.
5. Methods
• Methods are functions defined within classes in Java. They can return values and accept parameters.
• Syntax:
• returnType methodName(parameters) {
• // method body
• }
• Example:
• public int add(int a, int b) {
• return a + b;
• }
• Class: A blueprint for creating objects. It defines properties (fields) and methods.
• Object: An instance of a class.
• Example:
• public class Car {
• String model;
• int year;
•
• public void drive() {
• System.out.println("The car is driving.");
• }
• }
•
• public class Main {
• public static void main(String[] args) {
• Car myCar = new Car();
• myCar.model = "Toyota";
• myCar.year = 2020;
• myCar.drive();
• }
• }
7. Constructors
8. Inheritance
• Inheritance allows a class to inherit properties and methods from another class.
• The extends keyword is used for inheritance.
• Example:
• public class Animal {
• void sound() {
• System.out.println("Animal makes sound");
• }
• }
•
• public class Dog extends Animal {
• void sound() {
• System.out.println("Dog barks");
• }
• }
•
• public class Main {
• public static void main(String[] args) {
• Dog myDog = new Dog();
• myDog.sound(); // Output: Dog barks
• }
• }
9. Polymorphism
• Polymorphism allows one interface to be used for different underlying forms (methods or classes).
• There are two types of polymorphism:
o Compile-time (Method Overloading): Defining multiple methods with the same name but
different parameters.
o Runtime (Method Overriding): Redefining a method in the subclass that is already defined in
the superclass.
10. Encapsulation
• Encapsulation is the practice of hiding the internal details of a class and providing access through public
methods (getters and setters).
• Example:
• public class Employee {
• private int salary;
•
• // Getter
• public int getSalary() {
• return salary;
• }
•
• // Setter
• public void setSalary(int salary) {
• if (salary > 0) {
• this.salary = salary;
• }
• }
• }
11. Abstraction
• Abstraction involves hiding complex implementation details and exposing only essential features.
• It is achieved using abstract classes and interfaces.
• Java provides a set of classes and interfaces (like List, Set, Map) for working with groups of objects.
• Common collection classes:
o ArrayList: A resizable array.
o HashMap: A key-value pair storage.
o HashSet: A collection of unique elements.
o LinkedList: A doubly-linked list implementation.
• Java has a rich set of libraries and frameworks that simplify development. Some popular ones include:
o Spring: A powerful framework for building enterprise-level applications.
o Hibernate: A framework for Object-Relational Mapping (ORM).
o JUnit: A framework for unit testing.
o JavaFX: For building rich desktop applications.
15. Multithreading
Conclusion
Java remains one of the most popular programming languages due to its portability, scalability, and extensive
ecosystem. It's widely used for building a range of applications, from web and mobile apps to large-scale
enterprise systems. Understanding its key concepts such as object-oriented principles, exception handling, and
collections will help in becoming proficient in Java.