Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to OOP: Evolution of Java, OOP principles, Java Buzzwords, Implementing Java
program, JVM, Data Types, Variables, Type conversions and Casting, Operators, Control
statements, Arrays.
Evolution of Java
What is Java?
1) Standalone Application
2) Web Application
3) Enterprise Application
4) Mobile Application
1. Process-oriented model:
Characteristics of POP:
Emphasis is on doing actions.
Large programs are divided into smaller programs known as functions.
Most of the functions shared global data.
Data move openly around the program from function to function.
Functions transform data from one form to another.
Employs top-down approach in program design.
Organization of OOP:
method
method
method
Characteristics of OOPs:
Emphasis on data.
Programs are divided into what are known as methods.
Data structures are designed such that they characterize the objects.
Methods that operate on the data of an object are tied together.
Data is hidden.
Objects can communicate with each other through methods.
Reusability.
Follows bottom-up approach in program design.
Difference between Procedure Oriented Programming (POP) & Object Oriented
Programming (OOP)
1. Encapsulation
2. Inheritance
3. Polymorphism.
1. Encapsulation
Access to the code and data inside the wrapper is tightly controlled
through a well-defined interface.
For example:
A class defines the structure and behavior (data and code) that will be
shared by a set of objects.
Object: Any entity that has state and behavior is known as an
object.
Each object of a given class contains the structure and behavior defined
by the class, Hence objects are sometimes referred to as instances of a
class.
The mechanisms for hiding the complexity of the implementation inside the
class
Each method or variable in a class may be marked private or public.
The public interface of a class represents everything that external users
of the class need to know, or may know.
The private methods and data can only be accessed by code that is a
member of the class. Therefore, any other code that is not a member of the
class cannot access a private method or variable. i.e. Data hiding is possible
with private access specifier.
****Encapsulation provides Security, But Increases length of the code and time
for execution.
2. Inheritance
When one object acquires all the properties and behaviors of parent
object i.e. known as inheritance.
Example:
3. Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that
allows one interface to be used for a general class of actions. The specific
action is determined by the exact nature of the situation.
Example:
You might have a program that requires three types of stacks. One stack
is used for integer values, one for floating point values, and one for
characters. The algorithm that implements each stack is the same, even
though the data being stored differs. In a non–object-oriented language, you
would be required to create three different sets of stack routines, with each set
using different names.
Another example can be to speak something e.g. cat speaks meaw, dog barks
woof etc.
Evolution of Java
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991. The small team of sun engineers called Green Team.
2) Originally designed for small, embedded systems in electronic appliances like set-
top boxes.
3) Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
7) Why they choosed java name for java language? The team gathered to choose a
new name. The suggested words were "dynamic", "revolutionary", "Silk", "jolt",
"DNA" etc. They wanted something that reflected the essence of the technology:
revolutionary, dynamic, lively, cool, unique, and easy to spell and fun to say.
According to James Gosling "Java was one of the top choices along with Silk". Since
java was so unique, most of the team members preferred java.
8) Java is an island of Indonesia where first coffee was produced (called java
coffee).
There are many java versions that have been released. Current stable release of
Java is Java SE 8.
The initial release of Java was nothing short of revolutionary, but it did not mark
the end of Java’s era of rapid innovation.
Java 1.1 added many new library elements, redefined the way events are
handled, and reconfigured many features of the 1.0 library. Java 1.1 both added to
and subtracted from attributes of its original specification.
The next major release of Java was Java 2, where the “2” indicates “second
generation.” With Java 2, Sun repackaged the Java product as J2SE (Java 2
Platform Standard Edition), and the version numbers began to be applied to that
product.
Java 2 added support for a number of new features, such as Swing and the
Collections Framework, and it enhanced the Java Virtual Machine and various
programming tools. Java 2also contained a few deprecations. The most important
affected the Thread class in whichthe methods suspend( ), resume( ), and stop( )
were deprecated.
J2SE 1.3 was the first major upgrade to the original Java 2 release. For the
most part,it added to existing functionality and “tightened up” the development
environment. In general, programs written for version 1.2 and those written for
version 1.3 are source-code compatible. Although version 1.3 contained a smaller
set of changes than the preceding three major releases, it was nevertheless
important.
The release of J2SE 1.4 further enhanced Java. This release contained
several important upgrades, enhancements, and additions. Like it added the new
keyword assert, chained exceptions, and a channel-based I/O subsystem, made
changes to the Collections Framework and the networking classes.
The next release of Java was J2SE 5, and it was revolutionary, J2SE 5
fundamentally expanded the scope, power, and range of the language. To grasp the
magnitude of the
o Generics
o Annotations
o Auto boxing and auto-unboxing
o Enumerations
o Enhanced, for-each style for loop
o Variable-length arguments (varargs)
o Static import
o Formatted I/O
o Concurrency utilities
“5” in J2SE 5 is called the product version number.
The next release of Java was called Java SE 6. Java Platform, Standard
Edition 6.The Java Developer’s Kit was called JDK 6. It enhanced the API libraries,
added several new packages, and offered improvements to the runtime.
Java SE 7 The newest release of Java is called Java SE 7, with the Java
Developer’s Kit being called JDK 7, and an internal version number of 1.7. Java SE 7
is the first major release of Java since Sun Microsystems was acquired by Oracle (a
process that began in April 2009 and that was completed in January 2010).
Java SE 8 , is the Java Platform , Standard Edition 8 which is the latest version of
java with the following features
COMPILATION:
EXCEUTION:
COMPILATION:
D:\ >javac Simple.java
EXCEUTION:
At compile time, java file is compiled by Java Compiler (It does not interact
with OS) and converts the java code into byte code.
JVMs are available for many hardware and software platforms. JVM, JRE
and JDK are platform dependent because configuration of each OS differs.
But, Java is platform independent.
JVMs are available for many hardware and software platforms (i.e. JVM is
platform dependent).
What is JVM
It is:
What it does
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
1) Classloader
Classloader is a subsystem of JVM that is used to load class files.
2) Class(Method) Area
3) Heap
4) Stack
Java Stack stores frames. It holds local variables and partial results, and
plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed
when its method invocation completes.
7) Execution Engine
It contains:
1) A virtual processor
2) Interpreter: Read byte code stream then execute the instructions.
3) Just-In-Time(JIT) compiler: It is used to improve the performance.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 the instruction set of a specific CPU.
4. Garbage Collector: Automatic freeing of Heap Memory.
JRE
JDK
The key that allows Java to solve both the security and the portability
problems is that the output of a Java compiler is not executable code. Rather,
it is bytecode.
Once the run-time package exists for a given system, any Java program can
run on it. Remember, although the details of the JVM will differ from platform
to platform, all understand the same Java bytecode.
The execution of bytecode by the JVM is the easiest way to create truly
portable programs.
The fact that a Java program is executed by the JVM also helps to make
it secure. Because the JVM is in control, it can contain the program and
prevent it from generating side effects outside of the system.
However, with Java, the differential between the two is not so great.
Because bytecode has been highly optimized, the use of bytecode enables the
JVM to execute programs much faster than you might expect.
1. Simple
2. Object-oriented
Object-oriented means we organize our software as a combination of
different types of objects that incorporates both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies
software development and maintenance by providing some rules.
The concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
3. Platform Independent
The Java platform differs from most other platforms in the sense that it
is a software-based platform that runs on the top of other hardware-based
platforms.
It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be run on multiple platforms e.g. Windows, Linux, Sun
Solaris, Mac/OS etc. Java code is compiled by the compiler and converted
into bytecode. This bytecode is a platform-independent code because it can
be run on multiple platforms i.e. Write Once and Run Anywhere(WORA).
4. Secured
o No explicit pointer
o Java Programs run inside virtual machine sandbox
These securities are provided by java language. Some security can also be
provided by application developer through SSL, JAAS, Cryptography etc.
5. Robust
Robust simply means strong. The ability to create robust programs was
given a high priority in the design of Java. Because the program must
execute reliably in a variety of systems.
b) Exception handling
Java restricts you in a few key areas to force you to find your mistakes
early in program development. Exceptional conditions in traditional
environments often arise in situations such as division by zero or “file
not found,” and they must be managed with clumsy and hard-to-read
constructs. Java helps in this area by providing object-oriented
exception handling to overcome runtime errors.
6. Architecture-neutral
Operating system upgrades, processor upgrades, and changes in
core system resources can all combine to make a program malfunction.
7. Portable
We may carry the java byte code to any platform without any
minimal changes.
8. High-performance
9. Distributed
10. Multi-threaded
11. Interpreted
The byte code produced after compilation is interpreted by JVM and
produces the native code of the system.
12. Dynamic
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
The Java switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement.
The case constant can be integer constant, character constant or string constant.
switch(expression)
{
case value1: //code to be executed;
break; //optional
case value2: //code to be executed;
break; //optional
......
default:
// code to be executed if all cases are not matched;
}
Example:
Output:
20
30
Not in 10, 20 or 30
//switch using String constants
import java.util.*;
public class SwitchDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the two nos");
int a = sc.nextInt();
int b = sc.nextInt();
Syntax:
while(condition)
{
//code to be executed
}
If you pass true in the while loop, it will be infinitive while loop.
while(true)
{
System.out.println("infinitive while loop");
}
Output:
Syntax:
do
{
//code to be executed
}while(condition);
do{
System.out.println("infinitive do while loop");
}while(true);
Output:
The Java for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use
for loop.
Syntax:
for(initialization;condition;incr/decr)
{
//code to be executed
}
Example:
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
Syntax:
Example :
for(;;){
System.out.println("infinitive loop");
}
Output:
infinitive loop
infinitive loop
ctrl+c
for(int i=1;i<=10;i++)
{
if(i==5)
{
break;
}
}
System.out.println(i);
for(int i=1;i<=10;i++)
{
if(i==5){
continue;
}
System.out.print (i);
}
Output: 1 2 3 4 5 6 7 8 9 10
Java Comments
The java comments are statements that are not executed by the
compiler and interpreter. The comments can be used to provide
information or explanation about the variable, method, class or
any statement. It can also be used to hide program code for
specific time.
Syntax:
/*
This
is
multi line
comment
*/
Java Operators
Arithmetic Operators
Operator Result
+ Addition (also unary plus)
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus (Applied to both integer and float data)
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
–– Decrement
X+=1
double y= 25.25 % 10 // y=5.25
Relational Operators
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
int x, y, z;
x = y = z = 100; // set x, y, and z to 100 //Assignment Operator
class Oper1
{
public static void main(String args[])
{
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a); //-11
System.out.println(~b);//9
System.out.println(!c);//false
System.out.println(!d);//true
}
}
Java bitwise Operators Example: << , >>
}
}
class Oper3
{
public static void main(String args[])
{
//For positive number, >> and >>> works same
System.out.println(20>>2);
System.out.println(20>>>2);
class Oper4
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);
//false && true = false
System.out.println(a<b&a<c);
//false & true = false
}
}
class Oper5
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a<b && a++<c);
//false && true = false
System.out.println(a);
//10 because second condition is not checked
System.out.println(a<b&a++<c);
//false && true = false
System.out.println(a);
//11 because second condition is checked
}
}
Java logical Operators Example: | , ||
class Oper6
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);
//true || true = true
System.out.println(a>b|a<c);
//true | true = true
System.out.println(a>b||a++<c);
//true || true = true
System.out.println(a);
//10 because second condition is not checked
System.out.println(a>b|a++<c);
//true | true = true
System.out.println(a);
//11 because second condition is checked
}
}