0% found this document useful (0 votes)
11 views9 pages

Java Short Notes

Uploaded by

hjkfdbns28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Java Short Notes

Uploaded by

hjkfdbns28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Short Notes

Java is a popular, object-oriented programming language that is designed to be platform-


independent. It’s widely used for developing web applications, mobile applications (Android),
and large-scale enterprise systems.

1. Introduction to Java

 Java is a high-level, class-based, object-oriented programming language designed to


have as few implementation dependencies as possible.

 Key Features:

o Platform Independence: "Write Once, Run Anywhere" (WORA) philosophy.

o Object-Oriented: Everything in Java is treated as an object.

o Memory Management: Automatic garbage collection.

o Multithreading: Supports multiple threads of execution within a program.

o Rich API: Large standard library for networking, utilities, data structures, and
more.

o Security: Strong security features like bytecode verification and security


managers.

2. Java Basics

2.1. Java Syntax

 Class Declaration: All Java programs are written inside classes.

 class MyClass {

 // Code goes here

 }

 Main Method: Entry point for Java programs.

 public class Main {

 public static void main(String[] args) {

 System.out.println("Hello, World!");
 }

 }

2.2. Variables and Data Types

 Primitive Data Types: byte, short, int, long, float, double, char, boolean.

 Reference Types: Arrays, Objects.

Example:

int age = 25; // primitive

String name = "John"; // reference

2.3. Control Structures

 If-Else:

 if (x > 10) {

 // do something

 } else {

 // do something else

 }

 Switch-Case:

 switch (day) {

 case 1: System.out.println("Monday"); break;

 case 2: System.out.println("Tuesday"); break;

 default: System.out.println("Invalid day");

 }

3. Object-Oriented Programming (OOP) in Java

3.1. Classes and Objects

 Class: Blueprint for creating objects.

 Object: Instance of a class.


 class Dog {

 String breed;

 int age;

 void bark() {

 System.out.println("Woof!");

 }

 }

 Object Creation:

 Dog myDog = new Dog();

 myDog.bark();

3.2. Encapsulation

 Encapsulation is the technique of wrapping data (variables) and methods into a single
unit called a class.

 class Person {

 private String name; // private variable

 public String getName() {

 return name; // getter method

 }

 public void setName(String name) {

 this.name = name; // setter method

 }

 }

3.3. Inheritance

 Inheritance allows one class to inherit fields and methods from another class.
 class Animal {

 void eat() {

 System.out.println("Eating...");

 }

 }

 class Dog extends Animal {

 void bark() {

 System.out.println("Barking...");

 }

 }

3.4. Polymorphism

 Polymorphism allows methods to do different things based on the object they are acting
upon.

 class Animal {

 void sound() {

 System.out.println("Animal sound");

 }

 }

 class Dog extends Animal {

 @Override

 void sound() {

 System.out.println("Bark");

 }

 }
3.5. Abstraction

 Abstraction hides the complex implementation details and shows only the essential
features.

 abstract class Animal {

 abstract void sound();

 }

 class Dog extends Animal {

 void sound() {

 System.out.println("Bark");

 }

 }

3.6. Interfaces

 An interface defines a contract of methods that a class must implement.

 interface Animal {

 void sound();

 }

 class Dog implements Animal {

 public void sound() {

 System.out.println("Bark");

 }

 }

4. Exception Handling

 Purpose: Handle runtime errors so that the program can continue executing.
 Try-Catch Block:

 try {

 int result = 10 / 0; // ArithmeticException

 } catch (ArithmeticException e) {

 System.out.println("Cannot divide by zero");

 }

 Finally Block: Code that always runs after try-catch block.

 try {

 // code

 } catch (Exception e) {

 // error handling

 } finally {

 // cleanup code

 }

5. Collections Framework

 List: Ordered collection (e.g., ArrayList, LinkedList).

 Set: Collection that does not allow duplicates (e.g., HashSet).

 Map: Collection of key-value pairs (e.g., HashMap).

Example:

import java.util.*;

public class Example {

public static void main(String[] args) {

List<String> list = new ArrayList<>();

list.add("Apple");
list.add("Banana");

for (String fruit : list) {

System.out.println(fruit);

6. Multithreading

 Thread: A lightweight process that allows concurrent execution of code.

 class MyThread extends Thread {

 public void run() {

 System.out.println("Thread is running");

 }

 }

 public class Main {

 public static void main(String[] args) {

 MyThread t1 = new MyThread();

 t1.start(); // Start the thread

 }

 }

 Synchronization: Ensures that multiple threads do not conflict when accessing shared
resources.

 synchronized void syncMethod() {

 // synchronized block of code


 }

7. Java Memory Management

 Heap: Used for dynamic memory allocation (objects).

 Stack: Used for method calls and local variables.

 Garbage Collection: Automatic process to remove unused objects from memory.

8. Java I/O

 File I/O: Reading and writing files using classes like FileReader, BufferedReader, and
FileWriter.

 import java.io.*;

 public class FileExample {

 public static void main(String[] args) throws IOException {

 FileWriter writer = new FileWriter("example.txt");

 writer.write("Hello, Java!");

 writer.close();

 }

 }

9. Java 8 Features

 Lambda Expressions: Anonymous function that can be used to implement functional


interfaces.

 (int a, int b) -> a + b;

 Streams API: Allows processing sequences of elements, like collections, in a functional


way.

 List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


 numbers.stream().filter(n -> n > 2).forEach(System.out::println);

10. Java Virtual Machine (JVM)

 JVM is the engine that executes Java bytecode, providing platform independence.

 Key components:

o Class Loader: Loads class files.

o Bytecode Verifier: Ensures code safety.

o Execution Engine: Executes bytecode.

11. Java Tools and Frameworks

 Maven/Gradle: Build tools for managing dependencies and building Java projects.

 Spring Framework: A powerful framework for building enterprise applications.

 Hibernate: ORM (Object-Relational Mapping) framework for database interaction.

You might also like