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

1.Java-review,History, Puzz Words, JVM

Uploaded by

s.hemaswathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

1.Java-review,History, Puzz Words, JVM

Uploaded by

s.hemaswathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

School of Computing

DEPARTMENT OF INFORMATION TECHNOLOGY


Academic Year 2024-2025 (Odd Semester)

212INT2304

Object Oriented Programming Using JAVA


UNIT 1
UNIT 1
OOPS using Java
Introduction
Two Programming Paradigms
• Process-oriented: Code-centric, linear steps

• Object-oriented: Data-centric, objects control access to


code

• OOP helps manage complexity in larger programs


POP OOP

Procedure Oriented Programming Object Oriented Programming

C,FORTRAN,PASCAL C++,Java,python,C#

Programs divided into small parts Programs divided into small parts
called Functions called Objects

Top down Approach Bottom up Approach

No Security Access Specifiers

Adding new data and function is not easy Easy to add new data and functions

Overloading is not possible Overloading is possible

Operation is more focused than data Data is more focused than functions

Based on unreal world entities Based on real world entities


Review of OO Concepts
What is OOP?
• Programming paradigm focused on "objects“

• Objects bundle data (attributes) and behavior


(methods)
• Organizes programs around data, not just code
Core OOP Concepts in Java
Classes: Blueprints for creating objects
Objects: Instances of a class
Methods: Functions defined within a class, define object
behavior
Attributes: Variables within a class, hold object data
CLASS
Objects
History of Java
• Java was originally developed by Sun Microsystems
starting in 1991
– James Gosling
– Patrick Naughton
– Chris Warth
– Ed Frank
– Mike Sheridan
• This language was initially called Oak
• Renamed Java in 1995
What is Java
• A simple, object‐oriented, distributed, interpreted,
robust, secure, architecture neutral, portable,
high‐performance, multithreaded, and dynamic
language ‐‐ Sun Microsystems
Java Buzz words
Simple: Clean syntax, easy to learn for experienced
programmers
Secure: Memory management, strong typing prevent
vulnerabilities
Portable: Write once, run anywhere (JVM)
Object-Oriented: Objects encapsulate data (attributes)
and behavior (methods)
Robust: Strict type checking, automatic memory
management minimize errors
Multithreaded: Handle multiple tasks concurrently
Architecture-Neutral: JVM translates bytecode for
optimal performance
Interpreted & High Performance: Bytecode interpreted,
JIT compilation for speed
Distributed: Handles TCP/IP protocols, Remote Method
Invocation (RMI)
Abstraction in OOP
Key concept for managing complexity
Humans use abstraction (e.g., thinking of a car as a whole)
Objects encapsulate details, provide well-defined
interfaces
Power of Abstraction
Hierarchical classifications: Break complex systems into
parts
Example: Car as a whole, then subsystems (steering,
brakes, etc.)
Each subsystem can be further broken down (sound
system = radio, CD player)
Applying Abstraction to Programs
Process-oriented data becomes objects in OOP

Code becomes messages sent between objects

Objects define unique behavior, respond to messages


Benefits of OOP
Modular code: Objects are self-contained units

Reusability: Objects can be reused in different programs

Maintainability: Easier to modify and update code

Scalability: Easier to add new features to existing code


The Three
Encapsulation:
OOP Principles
Bundles data and code together: Encapsulation binds
data (attributes) and the code that manipulates it
(methods) within a single unit called a class.
Protects data and code: It acts as a protective barrier,
restricting arbitrary access from external code.
Controlled access through interface: Access to the
internal data and code is provided through well-defined
methods, forming the public interface of the class.
Hides implementation details: Encapsulation allows you
to hide the inner workings of a class, exposing only
necessary functionalities through public methods.
Promotes data integrity: By controlling access,
encapsulation helps ensure data integrity and prevents
unexpected modifications.
Encapsulation
Controls Member Visibility: Access modifiers define
whether a class member (method or variable) can be
accessed from outside the class.Public Members: Public
members are accessible by any code, providing the class's
functionality.
Private Members: Private members are hidden and can
only be accessed by methods within the same class,
promoting data protection.
Encapsulation through Privacy: By making essential data
members private, encapsulation ensures controlled access
and data integrity.
Well-designed Public Interface: The public interface,
consisting of public methods, should carefully expose
Inheritance
Code Reusability: Inheritance allows creating new classes
(subclasses) that inherit properties (attributes) and
behaviors (methods) from existing classes (superclasses).
This saves time and effort by reducing redundant code.
Hierarchical Relationships: Inheritance establishes a
parent-child hierarchy between classes. Subclasses inherit
from superclasses, creating an "is-a" relationship. For
example, a “German Shepard” object "is-a" type of "Dog"
object.
Polymorphism
One Interface, Multiple Methods: Polymorphism allows
using a single interface (method names) for a group of
related functionalities. The specific implementation
(method body) is determined at runtime based on the
object's type.
Reduces Complexity: By using a generic interface,
polymorphism simplifies code by avoiding the need for
multiple sets of methods with different names for similar
actions.
Compiler Responsibility: The Java compiler selects the
appropriate method implementation (based on object
type) during runtime, freeing the programmer from
manual selection.
Example: Runtime Behavior: Similar to a dog's sense of
smell reacting differently based on the scent, polymorphic
methods exhibit varied behavior depending on the
Java – The Most Popular (2002-2020)
Java Editions
• Java 2 Platform, Standard Edition (J2SE)
– Used for developing desktop-based applications and
networking applications
• Java 2 Platform, Enterprise Edition (J2EE)
– Used for developing large‐scale, distributed networking
applications and web‐based applications
• Java 2 Platform, Micro Edition (J2ME)
– Used for developing applications for small
memory‐constrained devices, such as cell phones,
pagers
and PDAs
Java platform
public class HelloWorld
{
public static void main( String [] args
)
{
System.out.println(“hello”);
}
}
Java Program ( Class File

Platform
Java
)
HelloWorld.java Windows NT Java API
Compile
Java Java Virtual
javac
Interpreter Machine
Hardware-Based
2387D47803
A96C16A48 Java Platform
4
54B646F541
06515EE464
Bytecode

Java
Interpreter
HelloWorld.class
P
o
Java Development Environment
• Edit
– Create/edit the source code
• Compile
– Compile the source code
• Load
– Load the compiled code
• Verify
– Check against security restrictions
• Execute
– Execute the compiled code
Phase 1: Creating a Program
• Any text editor or Java IDE (Integrated Development
Environment) can be used to develop Java programs
• Java source‐code file names must end with
the .java
extension
• Some popular Java IDEs are
– IntelliJ
– NetBeans
– Eclipse
Phase 2: Compiling a Java Program
• javac Welcome.java
– Searches the file in the current directory
– Compiles the source file
– Transforms the Java source code into bytecodes
– Places the bytecodes in a file named Welcome.class
Bytecodes
• They are not machine language binary code
• They are independent of any particular
microprocessor or hardware platform
• They are platform‐independent
instructions
• Another entity (interpreter) is required to convert
the bytecodes into machine codes that the
underlying microprocessor understands
• This is the job of the JVM (Java Virtual Machine)
JVM (Java Virtual Machine)
• It is a part of the JDK and the foundation of the Java
platform
• It can be installed separately or with JDK
• A virtual machine (VM) is a software application that
simulates a computer, but hides the underlying
operating system and hardware from the programs
that interact with the VM
• It is the JVM that makes Java a portable language
JVM (Java Virtual Machine)
• The same bytecodes can be executed on any
platform containing a compatible JVM
• The JVM is invoked by the java command
– java Welcome
• It searches the class Welcome in the current
directory and executes the main method of class
Welcome
• It issues an error if it cannot find the class Welcome
or if class Welcome does not contain a method called
main with proper signature
Phase 3: Loading a Program
• One of the components of the JVM is the class loader
• The class loader takes the .class files containing the
programs bytecodes and transfers them to RAM
• The class loader also loads any of the .class files
provided by Java that our program uses
Phase 4: Bytecode Verification
• Another component of the JVM is the bytecode
verifier
• Its job is to ensure that bytecodes are valid and
do
not violate Java’s security restrictions
• This feature helps to prevent Java programs arriving
over the network from damaging our system
Phase 5: Execution
• Now the actual execution of the program begins
• Bytecodes are converted to machine language
suitable for the underlying OS and hardware
• Java programs go through two compilation
phases
– Source code ‐> Bytecodes
– Bytecodes ‐> Machine language
Editing a Java Program
Examining Welcome.java
• A Java source file can contain multiple classes, but
only one class can be a public class
• Typically, Java classes are grouped into packages
(similar to namespaces in C++)
• A public class is accessible across packages
• The source file name must match the name of the
public class defined in the file with the .java
extension
Examining Welcome.java
• In Java, there is no provision to declare a class, and
then define the member functions outside the class
• Body of every member function of a class (called
method in Java) must be written when the method is
declared
• Java methods can be written in any order in the
source file
• A method defined earlier in the source file can call a
method defined later
Examining Welcome.java
• public static void main(String[] args)
– main is the starting point of every Java application
– public is used to make the method accessible by all
– static is used to make main a static method of class
Welcome. Static methods can be called without using any
object; just using the class name. JVM call main using the
ClassName.methodName (Welcome.main) notation
– void means main does not return anything
– String args[ ] represents an array of String objects that
holds the command line arguments passed to the
application. Where is the length of args array?
Examining Welcome.java
• Think of JVM as a outside Java entity who tries to
access the main method of class Welcome
– main must be declared as a public member of class
Welcome
• JVM wants to access main without creating an object
of class Welcome
– main must be declared as static
• JVM wants to pass an array of String objects
containing the command line arguments
– main must take an array of String as parameter
Examining Welcome.java
• System.out.println()
– Used to print a line of text followed by a new line
– System is a class inside the Java API
– out is a public static member of class System
– out is an object of another class of the Java API
– out represents the standard output (similar to stdout or
cout)
– println is a public method of the class of which out is an
object
Examining Welcome.java
• System.out.print() is similar to System.out.println(),
but does not print a new line automatically
• System.out.printf() is used to print formatted output
like printf() in C
• In Java, characters enclosed by double quotes ("")
represents a String object, where String is a class of
the Java API
• We can use the plus operator (+) to concatenate
multiple String objects and create a new String
object
Compiling a Java Program
• Place the .java file in the bin directory of your Java
installation
– C:\Program Files\Java\jdk-17.0.4\bin
• Open a command prompt window and go to
the bin directory
• Execute the following command
– javac Welcome.java
• If the source code is ok, then javac (the Java
compiler) will produce a file called Welcome.class in
the current directory
Compiling a Java Program
• If the source file contains multiple classes then javac
will produce separate .class files for each class
• Every compiled class in Java will have their own .class
file
• .class files contain the bytecodes of each class
• So, a .class file in Java contains the bytecodes of a
single class only
Executing a Java Program
• After successful compilation execute the following
command
– java Welcome
– Note that we have omitted the .class extension here
• The JVM will look for the class file Welcome.class
and search for a public static void main(String
args[
]) method inside the class
• If the JVM finds the above two, it will execute the
body of the main method, otherwise it will generate
an error and will exit immediately
Another Java Program
Examining A.java
• The variable of a class type is called a reference
– ob is a reference to A object
• Declaring a class reference is not enough,
we have to use new to create an object
• Every Java object has to be instantiated using
keyword new
• We access a public member of a class using
the dot operator (.)
– Dot (.) is the only member access operator in Java
– Java does not have ->, & and *
Primitive (built‐in) Data types
• Integers
– byte 8‐bit integer (new)
– short 16‐bit integer
– int 32‐bit signed integer
– long 64‐bit signed integer
• Real Numbers
– float 32‐bit floating‐point
– double number 64‐bit floating‐
• Other point number
types
– char 16‐bit, Unicode 2.1 character
– boolean true or false, false is not 0 in Java
Boolean Type
Non‐primitive Data types
• The non‐primitive data types in java are
– Objects
– Array
• Non‐primitive types are also called reference types
Primitive vs. Non‐primitive type
• Primitive types are handled by value – the actual
primitive values are stored in variable and passed
to methods
int x = 10;
public MyPrimitive(int x) { }
• Non‐primitive data types (objects and arrays) are
handled by reference – the reference is stored in
variable and passed to methods
Box b = new Box(1,2,3);
public MyNonPrimitive(Box x) { }
Primitive vs. Non‐primitive type
• Primitive types are handled by value
– There is no easy way to swap two primitive integers in Java
– No method like void swap(int *x, int *y)
– Can only be done using object or array
• But do we actually need a method to swap?
– x += (y - (y = x)) does the same in a single statement

You might also like