0% found this document useful (0 votes)
16 views33 pages

Basic Features of Java

Uploaded by

Subham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
16 views33 pages

Basic Features of Java

Uploaded by

Subham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 33

OOPs with JAVA

by
Mr. Bani Prasad Nayak
Assistant Professor
Department of Computer Science and Engineering
School of Engineering and Technology
GIET University, Gunupur, Odisha – 765022

Mobile No: 9938888341 E-Mail :


baninayak@giet.edu

12/20/2024 Prepared by Mr. Bani Prasad Nayak, 1


Asst. Prof., Dept. of CSE, GIETU, G
Basic Features of OOP
 Class and Object
 Data Abstraction
 Encapsulation
 Polymorphism
Compile Time Polymorphism
Run Time Polymorphism
 Inheritance
Class and Object
 In object oriented programming technique we should
design a program using classes and objects.
 Class is a template or blueprint from which objects are
created. it can also be told that, a group of objects
having common properties functionalities.
 Object is an entity that has state and behavior is
known as an object.
 An object has 3 characteristics:
State: State represents the data (value) of an object.
Behavior: It represents the behavior (functionality) of
an object.
Identity: Identity of an object is typically implemented
via unique ID.
For Example, Pen is an object. Its name is Reynolds;
Class Vs Object
No Object Class
.
1) Object is an instance of a Class is a blueprint or
class. template from which objects
are created.

2) Object is a real world Class is a group of similar


entity such as pen, laptop, objects.
mobile, bed. chair etc.

3) Object is a physical entity. Class is a logical entity.

4) Object is created Class is declared using class


through new keyword e.g.
keyword mainly class Student{}
e.g.
Student s1=new Student();
Data abstraction
 Data Abstraction basically deals with
hiding the details and showing the
essential things to the user. Whenever we
get a call, we will get an option to either
pick it up or just reject it. But in reality,
there is a lot of code that runs in the
background. So, you don’t know the
internal processing of how a call is
generated, that’s the beauty of
abstraction. Therefore, abstraction helps
to reduce complexity.
 It can be achieved in two ways:
Data Encapsulation
 Encapsulation is a
mechanism where you bind
your data and code together as
a single unit.
 It also means to hide your
data in order to make it safe
from any modification.
 The best way to understand
encapsulation is to look at the
example of a medical capsule,
where the drug is always safe
inside the capsule. Similarly,
through encapsulation the
Polymorphism
 Polymorphism means taking many forms, where ‘poly’
means many and ‘morph’ means forms. It is the ability of
a variable, function or object to take on multiple forms. In
other words, polymorphism allows you define one
interface or method and have multiple implementations.
 Polymorphism in Java is of two types:
o Run time Polymorphism
In Java, runtime polymorphism refers to a process in
which a call to an overridden method is resolved at
runtime rather than at compile-time.
o Compile time polymorphism
In Java, compile time polymorphism refers to a
process in which a call to an overloaded method is
resolved at compile time rather than at run time. Method
overloading is an example of compile time
Inheritance
 Inheritance in Java is a mechanism in which
one object acquires all the properties and
behaviors of a parent object.
 The idea behind inheritance in Java is that we
can create new classes that are built upon
existing classes.
 When you inherit from an existing class, you
can reuse methods and data(variables) of
the parent class.
 Moreover, we can add new methods and
data(variables)/ fields in the existing/ current
class also.
 Inheritance represents the IS-A
Main Features of Java
Main Features of Java
 Object Oriented
o Java is an Object-oriented programming language.
Everything in Java is an object. Object-oriented
means we organize our software as a combination
of different types of objects that incorporate both
data and behavior.
 Simple
o Java is very easy to learn, and its syntax is simple,
clean and easy to understand. According to Sun
Microsystem, Java language is a simple
programming language because:
• Java has removed many complicated and rarely-
used features, pointers.
Main Features of Java`
 Secured
o Java is best known for its security. With
Java, we can develop virus-free
systems. Java is secured because:
• No explicit pointer
• Java Programs run inside a virtual
machine sandbox
 Platform independent
o Java code can be executed on multiple
platforms, for example, Windows, Linux,
Sun Solaris, Mac/OS, etc. Java code is
compiled by the compiler and
converted into bytecode.
o This bytecode is a platform-
independent code because it can be run
on multiple platforms, i.e., Write Once
and Run Anywhere (WORA).
Main Features of Java
 Robust
o The English mining of Robust is strong. Java is robust
because:
• It uses strong memory management.
• There is a lack of pointers that avoids security
problems.
• Java provides automatic garbage collection which
runs on the Java Virtual Machine to get rid of
objects which are not being used by a Java
application anymore.
• There are exception handling which will handle run
type errors in Java. All these points make Java
robust.

Main Features of Java
 Architecture Neutral
o Java is architecture neutral because there are no
implementation dependent features, for example, the
size of primitive types is fixed.
o In C programming, int data type occupies 2 bytes of
memory for 32-bit architecture and 4 bytes of memory
for 64-bit architecture. However, it occupies 4 bytes of
memory for both 32 and 64-bit architectures in Java.
 Dynamic
o Java is a dynamic language. It supports the dynamic
loading of classes. It means classes are loaded on
demand. It also supports functions from its native
languages, i.e., C and C++.
o Java supports dynamic compilation and automatic
memory management (garbage collection).
Main Features of Java
 Interpreted
o An Java can be considered both a compiled and
an interpreted language because its source code
is first compiled into a binary byte-code. This
byte-code runs on the Java Virtual Machine (JVM),
which is usually a software-based interpreter.
 High performance
o Java is faster than other traditional interpreted
programming languages because Java bytecode
is "close" to native code. It is still a little bit
slower than a compiled language (e.g., C++).
Java is an interpreted language that is why it is
slower than compiled languages, e.g., C, C++,
etc.
Main Features of Java
 Multithreaded
o A thread is like a separate program, executing
concurrently.
o We can write Java programs that deal with many tasks
at once by defining multiple threads.
o The main advantage of multi-threading is that it
doesn't occupy memory for each thread.
o It shares a common memory area. Threads are
important for multi-media, Web applications, etc.
 Distributed
o Java is distributed because it facilitates users to create
distributed applications in Java. This feature of Java
makes us able to access files by calling the methods
from any machine on the internet.
How to write Java
programs
// Comment Line
public class <name> {
public static void main(String[] args) {
<statement>;
<statement>;
...
<statement>;
}
}
 Every executable Java program must consists
of a class which must contain a special
method main() with some additional
methods which should contain the variable
First program in Java
// Your First Program
class HelloWorld
{
public static void main(String[ ] args)
{
System.out.println("Hello,
World!");
}
}
 In Java every application begins with a class
definition.
 In the above program “HelloWorld” is the
class name.
 public static void main(String[ ] args) is
the main method.
 Every Java program must contain the main()
method. The Java compiler starts execution
the code from the main() method.
 Every valid Java Application must have a class
definition that matches the filename (class
name and file name should be same).

Execution process of
Java Program
Java Path Setting
 The path is required to be set for using tools such as javac,
java, etc.
 If you are saving the Java source file inside the JDK\bin
directory, the path is not required to be set because all the
tools will be available in the current directory.
 However, if you have your Java file outside the JDK\bin folder,
it is necessary to set the path of JDK. There are two ways to
set the path in Java:
 Temporary
 To set the temporary path of JDK, you need to follow the
following steps:
o Open the command prompt
o Copy the path of the JDK/bin directory
o Write in command prompt: set path=copied_path
 Example:
 set path=C:\Program Files\Java\jdk1.6.0_23\bin
Java Path Setting
 Permanent:
o Steps for permanent path Setting for Java:
o Open Jav\bin folder in MyComputer and copy
the path
o Go to MyComputer -> Properties
o Click on Advanced System settings
o Click on Environment Variables
o Click on New tab in user variable section
o Write the variable name as PATH
o Paste the copied path (in step 1) in variable
value section
o Click ok , Click Ok, Click Ok
 Java is a platform independent programming
language.
 A java application can be run or execute in
any platform. i.e., a java application created in
windows operating system can be executed in
other operating system.
 But, we cannot execute the java source file
created in windows platform in Linux or Mac.
 The .class file generated by the java compiler
can be executed or run in any platform.
 It does not depend on any type of platform.
 JVM(Java Virtual Machine) acts as a run-time engine
to run Java applications.

 JVM is the one that actually calls the main method


present in a java code and is a part of JRE(Java
Runtime Environment).

 JRE (Java Runtime Environment) is a software


package that provides Java class libraries, Java Virtual
Machine (JVM), and other components that are
required to run Java applications.
JDK (Java Development Kit)
 JRE is a part of JDK (which we will study later) but can
be downloaded separately.
 The Java Development Kit (JDK) is a software
development kit required to develop Java applications.

 When we download JDK, JRE is also downloaded with it.


 In addition to JRE, JDK also contains a number of
development tools (compilers, JavaDoc, Java
Debugger, etc).
 It contains JRE and several development tools, an
interpreter/loader (java), a compiler (javac), an
archiver (jar), a documentation generator
(javadoc) accompanied with another tool.
Java Program
 import java.util.Scanner;
 class Add
 {
 static int a,b,c;
 public static void main(String[] args)
 {
 Scanner in = new Scanner(System.in);
 System.out.print("Enter the seconds: ");
 a = in.nextInt();// Accept the integer value
to a
 b = in.nextInt();//Accept the integer value to
a
 c=a+b;
 System.out.println( "The sum
of"+a+"and"+b+"is"+c);
 import java.util.Scanner:
 Here, Scanner is a class used to get input which
is defined in java.util package.
 To use Scanner class, we have to create an object
of Scanner class using which we can able to
execute any method of the Scanner class.
Scanner in = new Scanner(System.in);
 Here, in is the object of the Scanner class.
n.nextInt()
 Here, nextInt() is executed by using the in
object, which is a type of Scanner class.
Scanner class used to
read the values of other
data types:
Method Description

nextBoolean() Reads a boolean value from the user

nextByte() Reads a byte value from the user

nextDouble() Reads a double value from the user

nextFloat() Reads a float value from the user

nextInt() Reads a int value from the user

nextLine() Reads a String value from the user

nextLong() Reads a long value from the user

nextShort() Reads a short value from the user


JVM Architecture
 JVM (Java Virtual Machine) is an abstract machine. It
is a specification that provides a runtime environment in
which Java bytecode can be executed.
 JVMs are available for many hardware and software
platforms (i.e. JVM is platform dependent).
JVM Architecture
Classloader: It is a subsystem of JVM that is used to
load class files. Whenever we run the java program, it is
loaded first by the classloader
Class(Method) Area: It is the memory block that stores
the class code, variable code(static variable, runtime
constant), method code, and the constructor of a Java
program.
Heap : It is the memory used to store objects
instantiated by applications running on the JVM. When the
JVM is started, heap memory is created and any objects in
the heap can be shared between threads as long as the
application is running.
Stack: It holds local variables and partial results and
plays a part in method invocation and return. It is used for
execution of threads.
JVM Architecture
 Native Method Stack
It contains all the native methods used in the
application
 Execution Engine

It contains:
o A virtual processor
o Interpreter: Read bytecode stream then execute the
instructions.
o Just-In-Time(JIT) compiler:
• JIT compiles parts of the byte code that have similar
functionality at the same time and hence reduces
the amount of time needed for compilation.
• Here, the term "compiler" refers to a translator from
the instruction set of a Java virtual machine (JVM) to
JVM Architecture
 Java Native Interface
It is a framework that provides an interface to
communicate with another application written in
another language like C, C++, Assembly etc. Java
uses the JNI framework to send output to the
Console or interact with OS libraries.

You might also like