OopsWithJava Unit1 byMultiAtomsPlus
OopsWithJava Unit1 byMultiAtomsPlus
s
reusable code. loading.
lu
3. Robust and Secure: Strong memory Robust: Strong memory management,
management and security features. exception handling, and automatic garbage
4. Rich API and Libraries: Extensive standard collection.
library and third-party support. Multithreaded: Supports concurrent
sP
5. Community Support: Large, active execution of multiple threads for better
community with ample resources. performance.
6. Performance: Balanced performance with High Performance: Just-In-Time (JIT)
Just-In-Time (JIT) compilation. compiler enhances runtime performance.
om
7. Enterprise Adoption: Widely used in Distributed: Designed for building
enterprise applications and web services. applications that can be distributed across
8. Job Market: High demand for Java networks.
developers. Dynamic: Can adapt to evolving
1.2 History of Java environments with its dynamic linking and
loading capabilities.
At
Early 1990s: Developed by James Gosling at Rich Standard Library: Provides a vast set of
Sun Microsystems; originally named "Oak" APIs for various tasks like I/O, networking,
for interactive TV. and GUI development.
1995: Renamed to "Java" and released
1.4 JVM Vs JVM Vs JRE
publicly, emphasizing platform
ti
s
core libraries but no development tools. improved performance.
3. Usage: Needed for running Java applications Garbage Collector: Manages memory by
lu
without compiling them. reclaiming memory used by objects that are
4. JRE = JVM + libraries to run Java applications no longer reachable.
sP
JVM Architecture 4. Native Method Interface (JNI):
Purpose: Enables Java code to interact with
native applications and libraries written in
other languages like C or C++.
om
5. Native Method Libraries:
Purpose: Provides libraries required for the
native methods that interact with the
underlying hardware and operating system.
At
components:
1. Class Loader Subsystem:
1. Package Declaration (optional):
ul
Purpose: Loads class files into memory and Specifies the package name.
performs memory management. Must be the first statement in the file (if
Components: Includes three main parts: present).
M
s
(JVM) can execute.
parameters, and a body that contains
executable statements.
lu
7. Classes and Objects: Classes serve as
blueprints or templates for creating objects,
sP
which are instances of classes. Classes
encapsulate data (fields) and behavior
(methods) related to a concept or entity.
Keywords: Reserved words in Java that have 9. Polymorphism: The ability of objects to take
a specific meaning in the language syntax on multiple forms. In Java, polymorphism is
(e.g., class, public, void). achieved through method overriding (runtime
polymorphism) and method overloading
Identifiers: Names used for classes,
(compile-time polymorphism).
methods, variables, etc. Must begin with a
ti
s
when an object is created. It initializes the method.
lu
object's state. Parameters: Variables passed into the
Rules for creating Java constructor method.
Constructor name must be the same as its Method Body: The code to be executed when
the method is called.
sP
class name
A Constructor must have no explicit return public class MyClass {
type // Method with no return value
public void displayMessage() {
A Java constructor cannot be abstract,
System.out.println("Hello, World!");
static, final, and synchronized
om
}
Types: Default and Parameterised
s
System.out.println("This is a static For More: Click Here
lu
method."); int data=50;//Here data is variable
}
} 2.9 Operators:
Arithmetic Operators: +, -, *, /, %
sP
2.6 Final Members: Relational Operators: ==, !=, <, >, <=, >=
Final members are constants that cannot be Logical Operators: &&, ||, !
changed once initialized. Assignment Operators: =, +=, -=, *=, /=
For variables, once a final variable is Increment/Decrement Operators: ++, --
assigned a value, it cannot be reassigned. For More: Click Here
om
For methods, a final method cannot be
overridden by subclasses. 2.10 Control Flow:
For classes, a final class cannot be Java compiler executes the code from top to
subclassed. bottom.
Final members are often used to define Java provides statements that can be used
constants or to prevent further modification
At
System.out.println("This is a final
1. Decision Making Statements -> if statements
method.");
and switch statement
}
2. Loops Statements -> for, while, do while, for
M
}
each loop
public final class FinalClass { 3. Jump Statements -> Break and Continue
// Class implementation Statement
} if statement: Executes a block of code if a
specified condition is true.
2.7 Data Types in Java: int x = 10;
Data types specify the different sizes and values if (x > 0) {
that can be stored in the variable. There are two System.out.println("x is positive");
types of data types in Java: }
s
System.out.println("x is positive"); System.out.println("i = " + i);
} else if (x < 0) { }
lu
System.out.println("x is negative");
} else { continue statement: Skips the current
System.out.println("x is zero"); iteration of the nearest enclosing loop and
sP
} proceeds with the next iteration.
for (int i = 0; i < 10; i++) {
switch statement: Selects one of many code if (i % 2 == 0) {
blocks to execute based on the value of an continue; // Skip the even numbers
expression. }
om
int day = 3; System.out.println("i = " + i);
switch (day) { }
case 1:
System.out.println("Monday"); 2.11 Arrays in Java::
break; Arrays in Java are used to store multiple values
of the same type in a single variable, rather than
At
case 2:
System.out.println("Tuesday"); declaring separate variables for each value.
break; Arrays are a fundamental part of Java
// Other cases programming and provide a way to organize data
default: efficiently.
ti
System.out.println("Invalid day");
int[] myArray;
break;
myArray = new int[5]; // Array of 5 integers
}
ul
for (int i = 0; i < 5; i++) { Strings in Java are objects that represent
System.out.println("i = " + i); sequences of characters. The String class is part
} of the java.lang package, and it provides various
while loop: Repeats a block of code while a methods to manipulate and process strings.
condition is true. String str1 = "Hello, World!";
int i = 0;
String str2 = new String("Hello, World!");
while (i < 5) {
System.out.println("i = " + i); int length = str1.length();
i++; char ch = str1.charAt(0); // 'H'
} int index = str1.indexOf("World"); // 7
s
System.out.println("x is positive"); System.out.println("i = " + i);
} else if (x < 0) { }
lu
System.out.println("x is negative");
} else { continue statement: Skips the current
System.out.println("x is zero"); iteration of the nearest enclosing loop and
sP
} proceeds with the next iteration.
for (int i = 0; i < 10; i++) {
switch statement: Selects one of many code if (i % 2 == 0) {
blocks to execute based on the value of an continue; // Skip the even numbers
expression. }
om
int day = 3; System.out.println("i = " + i);
switch (day) { }
case 1:
System.out.println("Monday"); 2.11 Arrays in Java::
break; Arrays in Java are used to store multiple values
of the same type in a single variable, rather than
At
case 2:
System.out.println("Tuesday"); declaring separate variables for each value.
break; Arrays are a fundamental part of Java
// Other cases programming and provide a way to organize data
default: efficiently.
ti
System.out.println("Invalid day");
int[] myArray;
break;
myArray = new int[5]; // Array of 5 integers
}
ul
for (int i = 0; i < 5; i++) { Strings in Java are objects that represent
System.out.println("i = " + i); sequences of characters. The String class is part
} of the java.lang package, and it provides various
while loop: Repeats a block of code while a methods to manipulate and process strings.
condition is true. String str1 = "Hello, World!";
int i = 0;
String str2 = new String("Hello, World!");
while (i < 5) {
System.out.println("i = " + i); int length = str1.length();
i++; char ch = str1.charAt(0); // 'H'
} int index = str1.indexOf("World"); // 7
s
3.1 Class and Object: overriding in Java.
Rules for Java Method Overriding:
lu
Class: A blueprint for creating objects. It defines
a datatype by bundling data and methods that The method must have the same name as in
work on the data. the parent class
public class Car { The method must have the same parameter as
sP
String color; in the parent class.
String model; There must be an inheritance.
int year; Code Link: Click Here
void displayInfo() { 3.5 Encapsulation
om
System.out.println("Model: " + model ); Encapsulation in Java is a process of wrapping
}} code and data together into a single unit, for
example, a capsule which is mixed of several
public class Main {
medicines.
public static void main(String[] args) {
We can create a fully encapsulated class in Java
Car myCar = new Car();
by making all the data members of the class
At
myCar.color = "Red"; }}
private. Now we can use setter and getter
methods to set and get the data in it.
3.2 Inheritance
Inheritance is a mechanism where a new class
inherits the properties and behaviors of an The Java Bean class is the example of a fully
encapsulated class.
ti
existing class.
Super Class: The class being inherited from. 3.5 Polymorphism
Polymorphism in Java is a fundamental concept
ul
void eat() {
System.out.println("This animal eats food.");
}
for flexibility and extensibility in your code.
}
class Dog extends Animal { (Think of it this way: you have a group of
void bark() { superheroes with different abilities, but they can
System.out.println("The dog barks.");
}} all be referred to as "superheroes".)
public class Main {
public static void main(String[] args) { There are two main types of polymorphism in
Dog dog = new Dog();
Java:
dog.eat(); // Inherited method
dog.bark(); // Sub class method Compile-time Polymorphism: This is
}}
achieved through method overloading.
Runtime Polymorphism: This is achieved
through method overriding.
s
software systems. Code Link: Click Here
lu
Abstract Classes: It is a class that cannot be package mypack;
instantiated on its own and may contain public class Simple{
abstract methods (methods without a body) as public static void main(String args[]){
System.out.println("package");
sP
well as concrete methods (methods with a
body). Abstract methods serve as }
placeholders for methods that must be }
implemented by subclasses. Abstract classes
4.2 CLASSPATH in Java
can have constructors and instance variables
om
just like any other class. The CLASSPATH is an environment variable used
by the Java compiler (javac) and the Java runtime
abstract class Shape {
(java) to locate classes that are referenced in your
abstract void draw();
program. Setting the CLASSPATH correctly is
}
crucial for compiling and running Java programs
class Circle extends Shape {
that are organized into packages.
At
void draw() {
You can set the CLASSPATH in various ways:
System.out.println("Drawing a circle");
a. Setting CLASSPATH Temporarily
}}
On Windows:
Interfaces: An interface in Java is a blueprint setPATH=.;C:\ProgramFiles\Java\JDK1.6.20
of a class that defines a set of abstract
ti
\bin
methods. Unlike abstract classes, interfaces On Unix/Linux/Mac:
cannot have instance variables or sudo <editor name> /etc/environment
ul
interface Animal {
void sound(); export JAVA_HOME
}
class Dog implements Animal { CLASSPATH=".:/usr/lib/jvm/<java
public void sound() { folder>/lib:/home/name/Desktop"
System.out.println("Dog barks");
}} export CLASSPATH
s
typically used to aggregate many Java class files import static java.lang.Math.*;
and associated metadata and resources (text,
lu
images, etc.) into one file to distribute application
software or libraries on the Java platform. Unit-1 is Completed
sP
In simple words, a JAR file is a file that contains a Please Subscribe
compressed version of .class files, audio files,
image files, or directories. We can imagine a .jar Multi Atoms
file as a zipped file(.zip) that is created by using &
WinZip software. Even, WinZip software can be
om
used to extract the contents of a .jar . So you can Multi Atoms Plus
use them for tasks such as lossless data
compression, archiving, decompression, and Playlist: Click Here
archive unpacking. Code Link: Click Here
Create a JAR file
At