0% found this document useful (0 votes)
4 views

JAVA Notes

Java is a high-level, object-oriented programming language that is platform-independent, allowing programs to run on any system with the Java Virtual Machine (JVM). Key concepts include object-oriented principles, data types, control flow statements, methods, classes, inheritance, polymorphism, encapsulation, abstraction, exception handling, and multithreading. Java's extensive libraries and frameworks, along with its development tools, contribute to its popularity for a wide range of applications.

Uploaded by

devjorhat987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JAVA Notes

Java is a high-level, object-oriented programming language that is platform-independent, allowing programs to run on any system with the Java Virtual Machine (JVM). Key concepts include object-oriented principles, data types, control flow statements, methods, classes, inheritance, polymorphism, encapsulation, abstraction, exception handling, and multithreading. Java's extensive libraries and frameworks, along with its development tools, contribute to its popularity for a wide range of applications.

Uploaded by

devjorhat987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Notes

Java is a widely-used, high-level, object-oriented programming language. It was designed to be platform-


independent, meaning that Java programs can run on any system that has the Java Virtual Machine (JVM)
installed, making it popular for cross-platform applications.

Here are key points about Java:

1. Overview of Java

• Object-Oriented: Java is based on object-oriented principles, such as classes, objects, inheritance,


polymorphism, and encapsulation.
• Platform-Independent: Java follows the principle of "Write Once, Run Anywhere" (WORA), meaning
that compiled Java code (bytecode) can run on any platform that has a JVM.
• Compiled and Interpreted: Java is first compiled into bytecode by the Java compiler (javac), and then
the bytecode is interpreted or compiled to native machine code by the JVM.

2. Basic Java Program Structure

A simple Java program typically follows this structure:

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

• 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

Java supports several data types, categorized into:

• 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.

4. Control Flow Statements


Java provides various control flow statements for decision-making and looping:

• 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;
• }

6. Classes and Objects

• 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

• A constructor is a special method used to initialize objects.


• It has the same name as the class and does not have a return type.
• Example:
• public class Person {
• String name;
• int age;

• // Constructor
• public Person(String name, int age) {
• this.name = name;
• this.age = age;
• }
• }

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.

12. Exception Handling

• Java uses try-catch blocks to handle exceptions and errors.


• Example:
• try {
• int result = 10 / 0;
• } catch (ArithmeticException e) {
• System.out.println("Error: " + e.getMessage());
• } finally {
• System.out.println("This block is always executed.");
• }

13. Collections Framework

• 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.

14. Java Libraries and Frameworks

• 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

• Java supports multithreading, allowing multiple threads to run concurrently.


• Threads can be created by either extending the Thread class or implementing the Runnable interface.

16. Java Development Tools


• Popular tools for Java development include:
o JDK (Java Development Kit): Contains the tools and libraries for developing Java applications.
o IDE (Integrated Development Environment): Examples include IntelliJ IDEA, Eclipse, and
NetBeans.

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.

You might also like