Software Evolution: From Binary to OOP
Software Evolution: From Binary to OOP
lang
util
Stack class
Push(), pop()
It is an virtual machine which is used to execute the java program. So it considered heart
of java program execution. After compiling a program(source code converts into byte code) it generates
byte code(.class file).
Java compiler is not a part of JVM. When byte code is generated then JVM will take the
responsibility.
JVM contains class loader sub system. It is software it checks weather the byte code is
generated properly or not. If not it terminates the program and shows error message. If byte code is
generated correctly, It uses 5 different memory location to load the byte code.
1) Method Area:- It is also known as class area. This area contains code of all method/ class.
Indirectly it contains whole code. It contains static variables also.
2) Heap Area:- System allocates memory for all instance variables (which declared inside a class
and outside all methods) and objects in Heap area.
3) Stack Area:- It contains local variables and partial result.
4) PC Register Area(Program Counter):- It contains addresses (Reference) of all methods.
5) Native Method Stack:- System allocates all methods which is derived from C and C++ in Native
Method Stack.
Executing Engine:- this engine can execute the program. It contains 2 programs.
1) Interpreter:- It converts byte code to machine to produce the output.
2) JIT(Just In-Time) Compiler:- It is supporting file used to increase the execute speed.
Native Library:- it contains all the predefined derived from c and c++.
Native Method Interface:- It acts as a mediator between Executive engine and Native Libraries.
First Program in Java:-
//To display a message
class Welcome
{
public static void main(String[] args)
{
[Link]("Welcome To Java");
}
}
Description:-
1) Line-1 Comment Line:- It allows the programmer to write description about the program such as
program message, author, date etc. Comment line is optional.
Comment line is divided into 3 types:- 1)Single line comment (above program) 2)Multiple line
Comment 3)Documentation Comment
/*------- /**----------
----------*/ -------------*/
2) Line-2 class Defining:- Java is Pure Object Oriented Programming Language, so every statement
should inside the class only. Every method should be in pair of curly braces ({}). It starts with
class keyword followed by classname.
3) Line-4 main() defining:- Usually every program execution starts with main(). It is treated as
starting point of program execution.
Public:- It is a access specifier, it allows the programmer from anywhere
Static :- it allows to call the method without creating an object.
Void:- It shows that method is not returning any value.
Every statement within a pair of curly braces.
Line-6:- println() :- It is an output method used to display a statement. System is a classname and out is
an static object, through this the programmer should call println().
Tokens:-
The smallest unit of a program is called Tokens. In java tokens are divided into 5 types. They are:
1)Keyword 2)Constant 3)Identifier 4)Operator 5)Separator.
1) Keyword:- Keyword is a predefined word which performs a specific task.
Ex1:- int is a keyword used to integer values.
Ex2:- if is a keyword used to test the condition.
Core Java contains 50 keywords. They are:
1)byte 2)short 3)int 4)long 5)float 6)double 7)char 8)Boolean 9)void 10)enum 11)class
12)interface 13)static 14)abstract 15)final 16)private 17)public 18)protected 19)volatile
20)native 21)transient 22)synchronized 23)strictfp 24)if 25)else 26)switch 27)case 28)default
29)break 30)continue 31)do 32)while 33)for 34)import 35)package 36)extends 37)implements
38)this 39)super 40)instanceof 41)new 42)goto 43)const 44)return 45)assert 46)try 47)catch
48)throw 49)throws 50)finally.
2) Constant :- Constant is a quantity which does not changes during program execution.
a)Integer Constant:- Ex: 45,54, etc
b)float constant:- ex:- 12.5, 56.7 etc
c)char Constant:- ex:- ‘A’, 9, ‘$’, ‘ ‘.
d)String Constant:- Ex:- “Raghava”, “[Link]:-9-136, champapet”.
e)Boolean:- it contains either true or false. Ex:- Boolean r=true;
3) Identifiers:- it may be class name, interface name, methodname, variable name, label name etc
Rules for Identifier:-
1) It should not start with number
Ex:- class 1stname
{
----------
-----------
}
2) Space is not valid in between identifier.
Ex: class First name
3) Special characters is not allowed except $ and _ symbol
Ex:- class First-name
4) Keywords are not allowed
Ex:- int do=10;
5) It may be of any length.
Operator:- It is a symbol which performs operation on operands.
Ex:- c=a+b; Here a,b,c are operands and +,= are oprator.
Operator Rank Direction
(), ., post increment/decrement 1 L to R
Unary -/+, pre increment/decrement 2 R to L
New, typecast() 3 R to L
/, *, % 4 L to R
+, - 5 L to R
>>, <<, >>> (Right shift zero fill operator) 6 L to R
>, <, >=, <=, instanceof 7 L to R
==, != 8 L to R
& 9 L to R
^ (XOR) 10 L to R
| 11 L to R
&& 12 L to R
|| 13 L to R
?: 14 R to L
=, +=, -=,*=,etc 15 R to L
All C operator is valid in java except pointer related operator.
Additional Operators (not in C):-
new:- It is used to allocate the memory just like malloc() in C but new operator implementation is very
easy.
Syntax:- datatype array[]=new datatype[size];
Ex:- int a[]=new int[3];
Ex:- int a[5]; //this is valid in C, C++ not in java
Datatype
Integer Float
type type
char boolean
O/P: 11
2) toUpperCase():-
String s1="Shiva";
String s2=[Link]();
[Link](s2);
O/P: SHIVA
3) toLowerCase():-
String s1="SHIVA”;
String s2=[Link]();
[Link](s2);
O/P: shiva
4) replace():- It is used replace a character. Syntax:- replace(‘old character’, ’new character’)
String s1="abcabc";
String s2=[Link]('b','k');
[Link](s2);
O/P: akcakc
5) concat():- It is used to join 2 strings.
String s1="Raghu";
String s2="Ram";
String s3=[Link](s2);
[Link](s3);
O/P: RaghuRam
6) charAt():- It returns character at given position.
String s1="Raghu";
char x=[Link](3);
[Link](x);
O/P: h
7) indexOf():- It returns the index of given character(first occurance).
String s1="abcabc";
int x=[Link]('b');
[Link](x);
O/P: 1
8) indexOf(start index, char):-
String s1="abcabc";
int x=[Link]('b',2);
[Link](x);
O/P: 4
9) indexOf(substring):-
String s1="Ram InfoTech, Ram Nagar";
int x=[Link]("Ram");
[Link](x);
O/P: 0
10) indexOf(substring, start index):-
String s1="Ram InfoTech, Ram Nagar";
int x=[Link]("Ram",4);
[Link](x);
O/P: 14
11) trim():- It is used to remove the space. (not from middle)
String s1=" ABC DEF ";
[Link](s1+"PQR");
String s2=[Link]();
[Link](s2+"PQR");
O/P:
ABC DEF PQR
ABC DEFPQR
12) isEmpty():- It checks weather the given string is empty or not. If not it returns false.
String s1="Shiva";
String s2="";
boolean x=[Link]();
boolean y=[Link]();
[Link](x);
[Link](y);
O/P:
false
true
13) equals():- It compares 2 strings. If they are same it returns true otherwise it returns false
String s1="ABC";
String s2="abc";
boolean x=[Link](s2);
[Link](x);
O/P: false
14) equalsIgnoreCase():- It is same as above method but it ignore case(no difference in upper case
and lowercase). In above problem result becomes true.
15) compareTo():- It compares 2 strings based on ASCII value. If string1>string2 it returns positive
value. If string1<string2 it returns negative value.
class SDemo
{
public static void main(String[] args)
{
[Link]("abc".compareTo("ABC")); //32
[Link]("ABC".compareTo("abc")); //-32
[Link]("ABC".compareTo("ABC")); //0
[Link]("ABCDE".compareTo("ABC")); //2
[Link]("abc".compareTo("abcde")); //-2
}
}
16) compareToIgnoreCase():- It is same as above method but it ignore the difference between
upper case & lowercase. The result of above program is :- 0 0 0 2 -2
17) substring():- It is used to retrieve the substrig from given position.
String s1="Ram Infotech";
String s2=[Link](4);
[Link](s2);
O/P: Infotech
18) substring(start index, end index(n-1)):-
String s1="Ram Infotech";
String s2=[Link](4,8);
[Link](s2);
O/P: Info
19) startsWith():- It checks weather string1 starts with string2 or not. If not it returns false or
otherwise it returns true.
String s1="Ram Infotech";
String s2="Ram";
boolean x=[Link](s2);
[Link](x);
O/P: true
20) endsWith():- It checks weather string1 ends with string2 or not. If not it returns false or
otherwise it returns true.
String s1="Ram Infotech";
String s2="Info";
boolean x=[Link](s2);
[Link](x);
O/P: false
21) split():- It returns the individual words from given string based on delimiter.
class SDemo
{
public static void main(String[] args)
{
String x="Ram,Ravi,Latha,Geetha";
String y[]=[Link](",");
for(String k:y)
[Link](k);
}
}
O/P:
Ram
Ravi
Latha
Geetha
Program 1:-
class Person
{
String name="Shiva";
int age=21;
void talk()
{
[Link]("Hai My Name is:"+name);
[Link]("My Age is:"+age);
}
public static void main(String[] args)
{
Person p1=new Person();
[Link]();
}
}
O/P:-
Hai My Name is:Shiva
My Age is:21
Program 2:-
class Person
{
String name;
int age;
void talk()
{
[Link]("Hai My Name is:"+name);
[Link]("My Age is:"+age);
}
public static void main(String[] args)
{
Person p1=new Person();
[Link]="Shiva";
[Link]=21;
[Link]();
}
}
Program 3:-
class Person
{
String name;
int age;
void get()
{
name="Shiva";
age=21;
}
void talk()
{
[Link]("Hai My Name is:"+name);
[Link]("My Age is:"+age);
}
public static void main(String[] args)
{
Person p1=new Person();
[Link]();
[Link]();
}
}
Program 4:-
class Person
{
String name;
int age;
void get(String n,int a)
{
name=n;
age=a;
}
void talk()
{
[Link]("Hai My Name is:"+name);
[Link]("My Age is:"+age);
}
public static void main(String[] args)
{
Person p1,p2;
p1=new Person();
[Link]("Shiva",21);
[Link]();
p2=new Person();
[Link]("Latha",20);
[Link]();
}
}
O/P:-
Hai My Name is:Shiva
My Age is:21
Hai My Name is:Latha
My Age is:20
Reactange Program:-
class Rect
{
int l,b,a,p;
void get(int x,int y)
{
l=x;
b=y;
}
void calc()
{
a=l*b;
p=2*(l+b);
}
void put()
{
[Link]("Area="+a);
[Link]("Perimeter="+p);
}
public static void main(String[] args)
{
Rect r1=new Rect();
[Link](5,6);
[Link]();
[Link]();
Rect r2=new Rect();
[Link](7,3);
[Link]();
[Link]();
}
}
O/P:-
Area=30
Perimeter=22
Area=21
Perimeter=20
Method Overloading
A single method can be used for multiple operation is called Method overloading. In this technic
one method can be used for multiple operations. For example user wants to interchange 2 integer
values, 2 float values, 2 string values. Usually a programmer can use 3 different method names. Instead
of this by using method overloading which allows single method name for 3 operations which increases
program understandability.
Program:-
class Arith
{
void add(int x,int y)
{
int z=x+y;
[Link]("Add="+z);
}
void add(double x,double y)
{
double z=x+y;
[Link]("Add="+z);
}
void add(String x,String y)
{
String z=x+y;
[Link]("Concatenation="+z);
}
public static void main(String[] args)
{
Arith a=new Arith();
[Link]("Shiva","kumar");
[Link](10,20);
[Link](12.5,23.7);
}
}
O/P:
Concatenation=Shivakumar
Add=30
Add=36.2
At the time of execution the JVM can search for same argument match between calling and
called method. If not it applies promotion technic(wider datatype).
Program:-
class Arith
{
void add(double x,double y)
{
double z=x+y;
[Link]("Add="+z);
}
public static void main(String[] args)
{
Arith a=new Arith();
[Link](10,20);
}
}
O/P:- Add=30.0
In Method Overloading return type does not play any role.
Program:-
class Arith
{
void add(int x,int y)
{
int z=x+y;
[Link]("Add="+z);
}
int add(int x,int y)
{
int z=x+y;
return z;
}
public static void main(String[] args)
{
Arith a=new Arith();
[Link](10,20);
int k=[Link](20,30);
[Link]("Add="+k);
}
}
In above program it show error message because it confuses which method is preferable.
Programmer can overload main method also. Programmer can design more than one main
method but JVM will execute main method with String array as argument.
Program:-
class Demo
{
void main(int x)
{
[Link]("Single Argument main method");
}
void main(int x,int y)
{
[Link]("Two Argument main method");
}
public static void main(String[] args)
{
Demo d=new Demo();
[Link](10,20);
[Link](30);
}
}
O/P:-
Two Argument main method
Single Argument main method
Program interchange 2 values:-
class Demo
{
void swap(int x,int y)
{
int z;
[Link]("Values before swap:x="+x+" y="+y);
z=x;
x=y;
y=z;
[Link]("Values after swap:x="+x+" y="+y);
}
void swap(double x,double y)
{
double z;
[Link]("Values before swap:x="+x+" y="+y);
z=x;
x=y;
y=z;
[Link]("Values after swap:x="+x+" y="+y);
}
void swap(String x,String y)
{
String z;
[Link]("Values before swap:x="+x+" y="+y);
z=x;
x=y;
y=z;
[Link]("Values after swap:x="+x+" y="+y);
}
public static void main(String[] args)
{
Demo d=new Demo();
[Link]("Raghu","Venu");
[Link](10,20);
[Link](10.5,20.7);
}
}
O/P:-
Values before swap:x=Raghu y=Venu
Values after swap:x=Venu y=Raghu
Values before swap:x=10 y=20
Values after swap:x=20 y=10
Values before swap:x=10.5 y=20.7
Values after swap:x=20.7 y=10.5
Constructor
Object cannot initialized like normal variables (primitive).
Ex:- int x= 10;
Person p1={111,”Raghu”}; [x]
But in real time application object can also initialized like normal variable. To overcome this they
developed Constructor. Constructor is a method which method name and class name should be same
and it should not contain return type. No need to write any statement to call constructor separately.
When JVM read object declaration then it calls constructor automatically (implicit invoking) .
Syntax:-
Class ClassName
{
-----
ClassName([args]) //constructor
{
----
-----
}
---
}
Constructors are divided into 2 types: 1)default Constructor. 2)Parametrized constructor
1) Default Constructor:- In this method we can’t send any parameters from calling to called
method.
Example Program:-
class Person
{
String name;
int age;
Person()
{
name="Raju";
age=21;
}
void talk()
{
[Link]("Hai My Name is:"+name);
[Link]("My age is:"+age);
}
public static void main(String[] args)
{
new Person().talk(); //ananamous object when we have single normal method
}
}
O/P:
Hai My Name is:Raju
My age is:21
2) Parameterized Constructor:- It allows the Programmer to send Parameters from calling to called
values. So we can different parameters.
Example program:-
class Person
{
String name;
int age;
Person(String n,int a)
{
name=n;
age=a;
}
void talk()
{
[Link]("Hai My Name is:"+name);
[Link]("My age is:"+age);
}
public static void main(String[] args)
{
new Person("Raju",21).talk();
new Person("Rani",20).talk();
}
}
O/P:-
Hai My Name is:Raju
My age is:21
Hai My Name is:Rani
My age is:20
[Link](111,"Venu",71,81,91);
[Link]();
if(k.m>34 && k.p>34 && k.c>34)
res="Pass";
else
res="Fail";
[Link]("Result="+res);
}
public static void main(String[] args)
{
Result r1=new Result();
[Link]();
}
}
Inheritance is divided into 5 types. They are: 1) Single Inheritance 2) Multi-level Inheritance 3)
Hierarchical Inheritance 4) Multiple Inheritance 5) Hybrid Inheritance
1) Single Inheritance:- One super class method can be used in only one sub class is called
inheritance. The programmer should declare object for sub class then he/she can access the
properties from both the classes.
Program:-
class Marks
{
int adno,m,p,c,tot;
String name;
double avg;
void getMarks(int no,String n,int s1,int s2,int s3)
{
adno=no;
name=n;
m=s1;
p=s2;
c=s3;
tot=m+p+c;
avg=tot/3.0;
}
void putMarks()
{
[Link](adno+" "+name+" "+m+" "+p+" "+c+" "+tot+" "+avg);
}
}
class Result extends Marks
{
String res;
void putResult()
{
if(m>34 && p>34 && c>34)
res="Pass";
else
res="Fail";
[Link]("Result="+res);
}
public static void main(String[] args)
{
Result r1=new Result();
[Link](111,"Venu",71,81,91);
[Link]();
[Link]();
}
}
O/P:-
111 Venu 71 81 91 243 81.0
Program:-
class Marks
{
int adno,m,p,c,tot;
double avg;
void getMarks(int no,int s1,int s2,int s3)
{
adno=no;
m=s1;
p=s2;
c=s3;
}
void putMarks()
{
tot=m+p+c;
avg=tot/3.0;
[Link]("M="+m+" P="+p+" C="+c);
[Link]("Total="+tot+" Average="+avg);
}
}
class Result extends Marks
{
String res;
void putResult()
{
if(m>=35 && p>=35 && c>=35)
res="Pass";
else
res="Fail";
[Link]("Result="+res);
}
}
class Division extends Result
{
void putDivision()
{
String div;
if(avg>=60 && res=="Pass")
div="First";
else if(avg>=50 && res=="Pass")
div="Second";
else if(avg>=35 && res=="Pass")
div="Third";
else
div="Nill";
[Link]("Division="+div);
}
public static void main(String[] args)
{
Division d=new Division();
[Link](111,81,91,72);
[Link]();
[Link]();
[Link]();
}
}
O/P:-
M=81 P=91 C=72
Total=244 Average=81.33333333333333
Result=Pass
Division=First
3) Hierarchy Inheritence:- common properties can be designed once and used multiple times in
multiple sub classes without redefining is called Hierarchy Inheritance.
Program:-
class Student
{
int adno;
String name,dept;
void get(int no,String s1,String s2)
{
adno=no;
name=s1;
dept=s2;
}
void put()
{
[Link]("Adno="+adno+" Name="+name+" Department="+dept);
}
}
class Science extends Student
{
String m,p,c;
void putScience(String s1,String s2,String s3)
{
m=s1;
p=s2;
c=s3;
[Link]("Subjects in Science department:");
[Link](m+" "+p+" "+c);
}
}
class Arts extends Student
{
String ac,co;
void putArts(String s1,String s2)
{
ac=s1;
co=s2;
[Link]("Subjects in Arts department:");
[Link](ac+" "+co);
}
public static void main(String[] args)
{
Science sc=new Science();
[Link](111,"Raghu","Science");
[Link]();
[Link]("Maths","Physics","Chemistry");
Arts a=new Arts();
[Link](222,"Ravi","Arts");
[Link]();
[Link]("Accounts","Commerce");
}
}
O/P:-
Adno=111 Name=Raghu Department=Science
Subjects in Science department:
Maths Physics Chemistry
Adno=222 Name=Ravi Department=Arts
Subjects in Arts department:
Accounts Commerce
4) Multiple Inheritance:- Acquiring properties from multiple classes they can be used in single sub
class is called multiple inheritance.
Syntax:- class subclass(super_class1, super_class2,…)
Method Overriding:- It allows the programmer to access new properties only. When subclass and
superclass contains same method name with similar arguments then subclass method overrides
superclass method.
Rules of Method overriding:-
1) The classes should contain is-a-relationship.
2) Subclass and superclass method names should be same with similar arguments.
Program:-
class Parent
{
void property()
{
[Link]("Cash/Gold/Land");
}
void marry()
{
[Link]("Pentamma");
}
}
class Child extends Parent
{
void marry()
{
[Link]("3sha/4me/9tara");
}
public static void main(String[] args)
{
Child x=new Child();
[Link]();
[Link]();
}
}
O/P:-
Cash/Gold/Land
3sha/4me/9tara
Super keyword:- It is used to access:
1) Super class instance variables
2) Super class method
3) Super class constructor
1) Super class instance variable:- When programmer declared same variable name in super class
and sub class then super keyword is used to access super class variable.
Program:-
class A
{
int x=10;
}
class B extends A
{
int x=20;
B()
{
[Link]("Super class value="+super.x);
[Link]("Sub class value="+x);
}
public static void main(String[] args)
{
new B();
}
}
O/P:-
Super class value=10
Sub class value=20
2) Access super class:- By using super programmer can execute super method also without
overriding.
Program:-
class A
{
int a,b;
void get(int x,int y)
{
a=x;
b=y;
}
void put()
{
[Link](a+" "+b);
}
}
class B extends A
{
int add,mul;
void put()
{
add=a+b;
mul=a*b;
[Link]();
[Link]("Add="+add+" Mul="+mul);
}
public static void main(String[] args)
{
B k=new B();
[Link](10,20);
[Link]();
}
}
O/P:-
10 20
Add=30 Mul=200
3) Super class constructor:- super keyword allows the programmer to access super class
constructor from sub class constructor.
Program:-
class A
{
int a,b;
A(int x,int y)
{
a=x;
b=y;
}
void put()
{
[Link](a+" "+b);
}
}
class B extends A
{
int c,d;
B(int m,int n,int p,int q)
{
super(m,n);
c=p;
d=q;
}
void put()
{
[Link]();
[Link](c+" "+d);
}
public static void main(String[] args)
{
new B(10,20,30,40).put();
}
}
O/P:-
10 20
30 40
final Keyword:-
It is used to:
1) Declare final variable
2) Define final method
3) Define final class
1) Declare final variable:- It is used to design symbolic constant (name to constant) . they are not
change during program execution.
Syntax:- final datatype name=value;
Ex:- final int MAX=100;
Final int PI=3.1415;
Blank final variable:- The variables which are not initialized at the time of declaration is called
blank final variables. They should be initialized at constructor.
Program:-
class Cust
{
final int PAN_NO;
String name;
Cust(int x,String n)
{
PAN_NO=x;
name=n;
}
void put()
{
[Link]("Pan number="+PAN_NO+" Name="+name);
}
public static void main(String[] args)
{
new Cust(123,"Raju").put();
}
}
O/P:-
Pan number=123 Name=Raju
Static blank final variable:- The programmer can also initialize a final variable in static block
also.
Ex:-
Class A
{
final int MAX;
-----
Static
{
------
MAX=100;
------
}
------
}
Abstraction
It is process to hide implementation details. It shows functionality only.
Ex:- In sending emails the user knowns how to send email but he does not what is the internal process.
Java supports for 2 types of abstraction:-
1) Abstract class(0 to 100% abstraction)
2) Interface(100% abstraction)
Abstract class:-
A class which contains abstract keyword is called abstract class. It should contains atleast one
abstract method. This class contain abstract method declaration only (unimplemented) it does not
contain body.
Syntax:-
Abstract class ClassName
{
--------
-------
Abstract returntype methodname([args]);
-------
------
}
Abstract method should be implemented in sub class. We can’t create object for abstract class.
Program:-
abstract class Bank
{
abstract int interestRate();
}
class SBI extends Bank
{
int interestRate()
{
return 7;
}
}
class SBH extends Bank
{
int interestRate()
{
return 8;
}
public static void main(String[] args)
{
Bank b;
b=new SBI();
[Link]("Interest Rate of SBI="+[Link]()+"%");
b=new SBH();
[Link]("Interest Rate of SBH="+[Link]()+"%");
}
}
O/P:-
Interest Rate of SBI=7%
Interest Rate of SBH=8%