0% found this document useful (0 votes)
34 views10 pages

OopsWithJava Unit1 byMultiAtomsPlus

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, highlighting its key features such as platform independence, object-oriented structure, and security. It covers the history of Java, the architecture of the Java Virtual Machine (JVM), and the components of the Java Development Kit (JDK) and Java Runtime Environment (JRE). Additionally, it discusses Java fundamentals, including syntax, data types, methods, and control flow structures.

Uploaded by

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

OopsWithJava Unit1 byMultiAtomsPlus

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, highlighting its key features such as platform independence, object-oriented structure, and security. It covers the history of Java, the architecture of the Java Virtual Machine (JVM), and the components of the Java Development Kit (JDK) and Java Runtime Environment (JRE). Additionally, it discusses Java fundamentals, including syntax, data types, methods, and control flow structures.

Uploaded by

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

Unit-1 OOPS WITH JAVA

1.3 Feature of JAVA.?


Platform Independence: "Write once, run
1. Introduction anywhere" capability due to the Java Virtual
1.1 why JAVA? Machine (JVM).
Java is a popular and versatile programming Object-Oriented: Encourages modular and
language that has been around for more than reusable code through classes and objects.
two decades. Simple: Easy to learn with a clean syntax
1. Platform Independence: Runs on any device similar to C++ but with fewer complexities.
with Java Virtual Machine (JVM). Secure: Built-in security features like
2. Object-Oriented: Encourages modular, bytecode verification and secure class

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

independence with the JVM.


Late 1990s: Gained popularity for web
ul

development with applets.


2004: Java 5.0 introduced major features
like generics and annotations.
2006: Became open-source under the GNU
M

General Public License.


2009: Oracle acquired Sun Microsystems.
JDK (Java Development Kit):
2011: Java 7 added features like the try-with-
resources statement. 1. Purpose: Provides tools to develop Java
2014: Java 8 introduced lambda expressions applications.
and the Stream API. 2. Components: Includes the JRE, compilers,
2017: Oracle announced a six-month release debuggers, and development tools.
cycle for new features. 3. Usage: Needed for writing, compiling, and
2018-Present: Continued regular releases debugging Java programs.
with incremental improvements. 4. JDK = JRE + development tools.

Notes by MULTI ATOMS PLUS Youtube Channel


JVM (Java Virtual Machine): Heap: Stores objects and arrays.
1. Purpose: Executes Java bytecode on any Java Stack: Stores frames containing local
device or operating system. variables, operand stack, and frame data for
2. Components: Consists of a class loader, a method calls.
bytecode verifier, and an execution engine. PC Register: Holds the address of the
3. Usage: Runs Java applications and ensures currently executing JVM instruction.
platform independence. Native Method Stack: Manages native
4. JVM = Engine to run Java bytecode. method calls used by Java programs.
3. Execution Engine:
JRE (Java Runtime Environment):
Interpreter: Executes bytecode instructions
1. Purpose: Provides the libraries and JVM one at a time.
necessary to run Java applications. Just-In-Time (JIT) Compiler: Compiles
2. Components: Includes the JVM and a set of bytecode Into native machine code for

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

1.5 Java Source File Stucture


The Java Virtual Machine (JVM) architecture is
designed to execute Java programs and A Java source file structure follows specific
provides platform independence. conventions and typically includes the following
ti

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

1. Bootstrap Class Loader: Loads core Java package com.example.myapp;


classes.
2. Extension Class Loader: Loads classes from 2. Import Statements (optional):
the Java Extensions Directory. Import other Java classes or packages.
3. Application Class Loader: Loads classes Must follow the package declaration (if
from the classpath. present) and precede the class definition

2. Runtime Data Areas: import java.util.List;


import java.util.ArrayList;
Method Area: Stores class structures like
metadata, constant pool, method data, and
field data.

Notes by MULTI ATOMS PLUS Youtube Channel


3. Class/Interface Declaration: 4. Operators: Symbols and keywords that
perform operations on variables and values,
The main content of the source file, usually
including arithmetic, relational, logical,
one public class/interface per file.
assignment, and increment/decrement
The class name must match the filename.
operators.
public class MyClass {
// Class content 5. Control Structures: Statements and
} constructs that dictate the flow of program
execution, such as conditional statements (if,
1.6 Compilation Process in Java else, switch) and loops (for, while, do-while).
The compilation process in Java involves
converting human-readable Java source code 6. Methods: Functions that encapsulate
reusable code and perform specific tasks.
into bytecode that the Java Virtual Machine
Methods are defined with a return type, name,

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.

8. Inheritance: The mechanism by which one


om
1.7 Java Fundamentals class (subclass or derived class) inherits
"fundamentals" refer to the essential concepts properties and behaviors from another class
and building blocks that form the core of the (superclass or base class). Inheritance promotes
language. code reuse and supports the "is-a" relationship.
1. Basic Syntax and Structure:
At

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

letter, a dollar sign $, or an underscore _,


10. Abstraction: The concept of hiding complex
followed by any combination of characters.
ul

implementation details and showing only the


Comments: Used to explain code, ignored by
essential features of an object. Abstraction in
the compiler:
Java can be achieved through abstract classes
1. Single-line: // This is a comment
and interfaces.
M

2. Multi-line: /* This is a multi-line comment */


2. Data Types: The various types of data that can
be used in Java, such as primitive types (int,
double, boolean, etc.) and reference types
(objects and arrays).

3. Variables and Constants: Declarations,


initialization, and usage of variables to store
data temporarily during program execution.
Constants, declared with the final keyword, hold
values that do not change.

Notes by MULTI ATOMS PLUS Youtube Channel


2. Programming Structures in Java
2.3 Methods in Java
2.1 Defining Classes in Java
It is a reusable block of code designed to
Class: A blueprint or template that defines the perform specific tasks. By calling or invoking it,
properties (fields) and behaviors (methods) we execute its functionality without needing to
common to all objects of that type. rewrite the code. This promotes code
public class MyClass { reusability, easy modification, and enhances
// Fields code readability.
// Constructor Method Components
// Methods Access Modifier: Defines the visibility of the
} method (public, private, protected).
Return Type: The type of data the method
2.2 Constructors returns (e.g., int, String, void).
A constructor is a special block of code that runs Method Name: The name used to call the

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

1. Default Constructors: // Method with a return value


Automatically provided by Java if no public int addNumbers(int a, int b) {
constructors are defined explicitly. return a + b;
}
At

public class MyClass {


public MyClass() { }
// Initialization code
} 2.4 Access Specifier:
} Access specifier or modifier is the access type of
ti

the method. It specifies the visibility of the


2. Parameterized Constructor:: method. Java provides four types of access
Accepts parameters to initialize object fields specifier:
ul

with specific values. Public: The method is accessible by all


public class Car { classes when we use public specifier in our
private String model; application.
M

private int year; Private: When we use a private access


// Parameterized constructor specifier, the method is accessible only in
public Car(String model, int year) { the classes in which it is defined.
this.model = model; Protected: When we use protected access
this.year = year; specifier, the method is accessible within the
}} same package or subclasses in a different
package.
Constructors are fundamental in Java
Default: When we do not use any access
programming for creating and initializing
specifier in the method declaration, Java
objects, ensuring they are ready for use with the
uses default access specifier by default. It is
appropriate state.
visible only from the same package only.

Notes by MULTI ATOMS PLUS Youtube Channel


2.5 Static Members:
Static members belong to the class rather 1. Primitive data types: The primitive data
than to any specific instance of the class. types include boolean, char, byte, short, int,
They are shared among all instances of the long, float and double.
class. 2. Non-primitive data types: The non-primitive
Static members can be accessed directly data types include Classes, Interfaces, and
using the class name without needing to Arrays.
instantiate an object. For More: Click Here
public class MyClass { 2.8 Variables in Java:
public static int staticVar; It is the name of a reserved area allocated in
} memory. In other words, it is a name of the
public class MyClass { memory location. It is a combination of "vary +
public static void staticMethod() { able" which means its value can be changed.

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

to control the flow of Java code. Such


of certain elements in a program. statements are called control flow
public class MyClass { statements.
public final int MY_CONSTANT = 100; It is one of the fundamental features of Java,
} which provides a smooth flow of program.
ti

public class MyClass { Java provides three types of control flow


public final void display() { statements:
ul

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: }

Notes by MULTI ATOMS PLUS Youtube Channel


if-else statement: Executes one block of do-while loop: Similar to a while loop, but
code if a condition is true, and another block the block of code is executed at least once.
if it is false. int i = 0;
int x = -10; do {
if (x > 0) { System.out.println("i = " + i);
System.out.println("x is positive"); i++;
} else { } while (i < 5);
System.out.println("x is not positive"); break statement: Exits the nearest enclosing
} loop or switch statement.
else-if ladder: Checks multiple conditions in for (int i = 0; i < 10; i++) {
sequence. if (i == 5) {
int x = 0; break; // Exit the loop when i is 5
if (x > 0) { }

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

myArray[0] = 1; // Assign values


for loop: Repeats a block of code a specific individually
number of times.
2.12 Strings in Java::
M

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

Notes by MULTI ATOMS PLUS Youtube Channel


if-else statement: Executes one block of do-while loop: Similar to a while loop, but
code if a condition is true, and another block the block of code is executed at least once.
if it is false. int i = 0;
int x = -10; do {
if (x > 0) { System.out.println("i = " + i);
System.out.println("x is positive"); i++;
} else { } while (i < 5);
System.out.println("x is not positive"); break statement: Exits the nearest enclosing
} loop or switch statement.
else-if ladder: Checks multiple conditions in for (int i = 0; i < 10; i++) {
sequence. if (i == 5) {
int x = 0; break; // Exit the loop when i is 5
if (x > 0) { }

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

myArray[0] = 1; // Assign values


for loop: Repeats a block of code a specific individually
number of times.
2.12 Strings in Java::
M

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

Notes by MULTI ATOMS PLUS Youtube Channel


3. Object Oriented Programming:
Object means a real-world entity such as a pen, 3.3 Method Overloading
chair, table, computer, watch, etc. Object-Oriented If a class has multiple methods having same name
Programming is a methodology or paradigm to but different in parameters, it is known as Method
design a program using classes and objects. It Overloading.
simplifies software development and maintenance There are two ways to overload the method in
by providing some concepts: java:
Object By changing number of arguments
Class By changing the data type
Inheritance Code Link: Click Here
Polymorphism 3.4 Method Overriding
Abstraction
If subclass (child class) has the same method as
Encapsulation
declared in the parent class, it is known as method

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

Sub Class: The class that inherits from the


super class. in object-oriented programming (OOP) that
class Animal {
allows objects of different classes to be treated
as objects of a common superclass. This allows
M

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.

Notes by MULTI ATOMS PLUS Youtube Channel


3.6 Abstraction
4. Packages:
Abstraction in Java is another core concept of 4.1 Defining Package:
object-oriented programming (OOP), alongside In Java, a package is a mechanism for organizing
encapsulation, inheritance, and polymorphism. It classes, interfaces, and Sub-Packages, which
involves hiding the complex implementation helps in avoiding name conflicts and controlling
details of a system and showing only the essential access. Packages are also used to group related
features of the object. classes and interfaces together, making the code
Abstraction can be achieved in Java using two more modular and easier to manage.
main mechanisms:
Abstract Classes Package in java can be categorized in two form,
Interfaces built-in package and user-defined package.
Abstraction allows you to focus on what an object
does rather than how it does it, promoting code There are many built-in packages such as java,
reusability, maintainability, and flexibility in large lang, awt, javax, swing, net, io, util, sql etc.

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

constructors. Classes can implement multiple


interfaces, allowing them to inherit behavior JAVA_HOME = "/usr/lib/jvm/<java folder
from multiple sources. (eg. java-1.8.0-openjdk-amd64>)/bin"
M

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

Notes by MULTI ATOMS PLUS Youtube Channel


b. Setting CLASSPATH Permanently
4.5 import and static import statement
On Windows: Java 5.0 introduced static import, also known as
1. Right-click on This PC or My Computer and the tiger version, as a new language feature. Static
select Properties. variables and static members can now be
2. Click on Advanced system settings. accessed directly without having to use class
3. Click on the Environment Variables button. names.
4. In the System variables section, click New or
select the existing CLASSPATH variable and We had normal import statements for every java
click Edit. class before static import was introduced; we
5. Add your paths to the CLASSPATH variable. used class names to call methods and classes.
Below is an example of a normal import for the
List class.
4.3 JAR files in Java
A JAR (Java Archive) is a package file format import java.util.List;

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

jar cf jarfilename inputfiles


Here, cf represents to create the file. For example
,assuming our package pack is available in
C:\directory , to convert it into a jar file into the
ti

pack.jar , we can give the command as:


C:\> jar cf pack.jar pack
ul

4.4 Naming Conventions for Java Packages:


For avoiding unwanted package names, we have
M

some following naming conventions which we use


in creating a package.
1. Use Lowercase Letters:
E.g: package com.example.myapp;
2. Period-Delimited Structure:
E.g: package com.example.myapp.controllers;
3. Base Names on Company or Organization:
E.g: package com.example.myapp.services;

Notes by MULTI ATOMS PLUS Youtube Channel

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy