Unit 1
Unit 1
Unit – 1
Introduction to OOP
TABLE OF
CONTENTS
01 Introduction - Need of OOP
02 Principles of OOP
3. Java Features
4. Installation of JDK
Object Oriented Programming
Object Oriented
Programming
• Object oriented programming is an approach that provides a way
of modularizing programs by creating partitioned memory area of
both data and functions that can be used as templates for creating
copies of such modules on demand.
new modules.
Principles of Object Oriented
Programming
• Object means a real word entity such as pen, chair, table etc.
•Object-Oriented Programming is a methodology or paradigm to design a
program using classes and objects. It simplifies the software development and
maintenance by providing some concepts:
1. Object
2. Class
3. Encapsulation
4. Inheritance
5. Polymorphism
6. Abstraction
Class
• A class is a set of objects with:
– Same interface, same behavior, different identify
• Classes allow modeling of simple classification in the real world, and simplify
design
• An object is an “instance” of a class
Objects
Object Behaviour
– A bank account: withdraw, deposit, calculate
interest, print statement.
Encapsulation
● Binding (or wrapping) code and data together into a single unit is
known as encapsulation. Example: Class
• HotJava
- The first Java-enabled Web browser
• JDK Evolutions
• Object-oriented • Distributed
• Robust • Dynamic
• Multithreaded • Platform
Independent
Simple
•Java was designed to be easy for the professional programmer to learn
and use effectively.
•If we already understand the basic concepts of object-oriented
programming, learning Java will be even easier.
•If we are an experienced C++ programmer, moving to Java will require
very little effort. Because Java inherits the C/C++ syntax and many of
the object-oriented features of C++.
•No Pointers in java.
• Portability is a major aspect of the Internet because there are many different
types of computers and operating systems connected to it.
•If a Java program were to be run on virtually any computer connected to the
Internet, there needed to be some way to enable that program to execute on
different systems.
• Write once, run everywhere
Object Oriented
•Everything in Java is an Object. All program code and data reside within
objects and classes.
•Java comes with an extensive set of classes arranged in Packages.
Robust
• Robust means strong. Java is robust because
• It uses strong memory management system. Automatic garbage collection.
• Java is a strictly typed language, it checks your code at compile time. However,
it also checks your code at run time.Java handles exceptions using Exceptional
handling.
Multithreaded
• Java was designed to meet the real-world requirement of creating interactive,
networked programs.
•To accomplish this, Java supports multithreaded programming, which allows
you to write programs that do many things simultaneously.
Architecture - Neutral
•Operating system upgrades, processor upgrades, and changes in core system resources
can all combine to make a program malfunction.
•The Java designers made several hard decisions in the Java language and the Java Virtual
Machine in an attempt to alter this situation. Their goal was “write once; run anywhere,
any time, forever.”
Distributed
• Java is designed for the distributed environment of the Internet because it handles
TCP/IP protocols.
• Java also supports Remote Method Invocation (RMI). This feature enables a program to
invoke methods across a network.
Interpreted and High Performance
Dynamic
• Java programs carry with them substantial amounts of run-time type information that is
used to verify and resolve accesses to objects at run time.
Cr e a t e/Modify So ur ce Co de
Creating So ur ce Co de
and Co m p ile So ur ce Co de
i.e. jav ac Welcome.java
Compiling If compilation e r r o r s
Programs By t eco de
Run By t eode
i.e. ja v a W elcome
On command line
Result
javac file.java
If runtime e r r o r s o r incorrect result
Executing Application
• Just In Time compiler (JIT) is runs after the program has started
executing, on the fly. It has access to runtime information and makes
optimizations of the code for better performance.
Java Virtual Machine
• public
- visibility could be private
• static
- the main method belongs to the Hello class, and not an
instance (object) of the class
• void
- method does not return a value
Compile
Welcome.java
javac Welcome.java
Output: Welcome.class
Run
java Welcome
Output: Welcome to Java
Installation of
JDK
Step 0: Un-Install Older Version(s) of JDK/JRE
Step 1: Download JDK
Goto Java SE download site @
http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Step 2: Install JDK
Run the downloaded installer
Step 3: Include JDK's "bin" Directory in the PATH
Step 4: Verify the JDK Installation
Step 5: Write a Hello-World Java Program
Step 6: Compile and Run the Hello-World Java Program
JavaProgramming
• Unit – 1 ProgrammingConcepts
TABLE OF
CONTENTS
1. Java Tokens
2. Control Structures
3. Classes and Objects
4. Garbage Collection
Dr. Y. J. Nagendra Kumar - 53
Java Tokens
• A data type is defined by a set of values and the operators you can
perform on them
• The Java language has several predefined types, called primitive data
types
double 64 308 15
Characters
• A char value stores a single character from the Unicode characterset
• A character set is an ordered list of characters
‘A’, ‘B’, ‘C’, … , ‘a’, ‘b’, … ,‘0’, ‘1’, … , ‘$’, …
Boolean
• A boolean value represents a true/false condition.
• It can also be used to represent any two states, such as a light bulb being on or
off
• The reserved words true and false are the only valid values for a boolean type
Dr. Y. J. Nagendra Kumar - 62
Datatypes
Identifiers: Syntax
• Identifiers are the words a programmer uses in a program
• Identifier syntactic rules:
- Can be made up of any lengthof
- letters
- digits
- underscore character (_)
- dollar sign ($)
- Cannot begin with a digit
• Java is case sensitive
-User and user are completely different identifiers
Dr. Y. J. Nagendra Kumar - 64
Naming Conventions for
Identifiers
• Classes
- Names given in lowercase except for first letter of each word in the name
• Variables
- Same as classes, except first letter is lowercase
• Constants
- All caps with _ between words
• Methods
- like variable names but followed by parentheses
Keywords
• We may declare that a variable is a constant and its value may never change.
• Advantages:
- readability
- efficiency
- error detection
14 / 3 equals 4
8 / 12 equals 0
• The remainder operator (%) returns the remainder after dividing the second
operand into the first
14 % 3 equals 2
8 % 12 equals 8
Operator
Precedence
• Operators can be combined into complex expressions
result = total + count / max - offset;
10 - (7 - 1) 4
1+2*3 7
(1 + 2) * 3 9
1-2*3+4*5 15
Arithmetic Operators
• An operator is a mapping that maps one or more values to a single
value:
• Binary Operators:
a + b adds a and b
a - b subtracts b from a
a * b multiplies a and b
a / b divides a by b
a %b the reminder of divining a by b
• Unary Operator:
-a The negation of a
Dr. Y. J. Nagendra Kumar - 74
Simple Arithmetic
public class Example {
public static void main(String[] args) {
int j, k, p, q, r, s, t;
j = 5; > java Example
k = 2; p = 7
q = 3
p = j + k; r = 10
q = j - k; s = 2
r = j * k; t = 1
s = j / k; >
t = j % k;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
}
Shorthand Operators +=, -=, *=, /=, %=
Common Shorthand
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
a = a % b; a %= b;
Shorthand
Operators
public class Example {
public static void main(String[] args) {
int j, p, q, r, s, t;
j = 5; > java Example
p = 1; q = 2; r = 3; s = 4; t = 5; p = 6
q = -3
p += j; r = 15
q -= j; s = 0
r *= j; t = 0
s /= j; >
t %= j;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
}
Shorthand Increment and Decrement ++ and --
Common Shorthand
a = a + 1; a++; or ++a;
a = a - 1; a--; or --a;
Increment and
Decrement
public class Example {
public static void main(String[] args) {
int j, p, q, r, s;
j = 5; > java example
p = ++j; // j = j + 1; p = j; p = 6
System.out.println("p = " + p); q = 6
j = 7
q = j++; // q = j; j = j + 1; r = 6
System.out.println("q = " + q); s = 6
System.out.println("j = " + j); r >
= --j; // j = j -1; r = j;
System.out.println("r = " + r); s
= j--; // s = j; j = j - 1;
System.out.println("s = " + s);
}
}
Dr. Y. J. Nagendra Kumar - 79
Relational
> < >= <= == !=
Operators
Primitives
• Greater Than >
• Less Than <
• Greater Than or Equal >=
• Less Than or Equal <=
• Logical OR |
|
• Logical NOT !
Assignment
Statements
• An assignment statement takes the following form
variable-name = expression;
• AND &
• OR |
• XOR ^
• NOT ~
Logical (bit) Operator
Examples
public class Example {
public static void main(String[] args) {
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12
int and, or, xor, na; > java Example
and 8
and = a & b; // 00001000 = 8 or 14
or = a | b; // 00001110 = 14 xor 6
na -11
xor = a ^ b; // 00000110 = 6 >
na = ~a; // 11110101 = -11
System.out.println("and " + and);
System.out.println("or " + or);
System.out.println("xor " + xor);
System.out.println("na " + na);
}
}
Dr. Y. J. Nagendra Kumar - 85
Shift Operators (Bit
<< >> >>>
Level)
• Shift Left << Fill with Zeros
a 00000000000000000000000000000011 3
a << 2 00000000000000000000000000001100 12
<<
Left b 11111111111111111111111111111100 -4
b << 2 11111111111111111111111111110000 -16
a 00000000000000000000000000000011 3
>> a >> 2 00000000000000000000000000000000 0
Right b 11111111111111111111111111111100 -4
b >> 2 11111111111111111111111111111111 -1
Shift Operator
>>>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4
a 00000000000000000000000000000011 3
>>> a >>> 2 00000000000000000000000000000000 0
Right 0 b 11111111111111111111111111111100 -4
b >>> 2 00111111111111111111111111111111 +big
l = i;
(long)i; Widening
l long l;
int i;
i
int long
32 bit 64 bit
Dr. Y. J. Nagendra Kumar - 43
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
Flow of Control
• Java executes one statement after the other in the order
they are written
• Many Java statements are flow control statements:
Alternation : if, if else, Nested If, Else If, switch
Looping : while, do while, for
Escapes : break, continue, return
Simple If
if ( x < 10 )
x = 10;
If… else
The if … else statement evaluates an
expression and performs one action if
that evaluation is true or a different
action if it is false.
if (x != oldx) {
System.out.print(“x was changed”);
}
else {
System.out.print(“x is unchanged”);
}
Nested if … else
if ( myVal > 100 )
{
if ( remainderOn == true)
{
myVal = mVal % 100;
}
else {
myVal = myVal / 100.0;
}
}
else
{
System.out.print(“myVal is in range”);
}
Else If Ladder
switch ( n ) {
case 1:
// execute code block #1
break;
case 2:
// execute code block #2
break;
default:
// if all previous tests fail then
//execute code block
break;
}
while loop
int i=0;
while(i<10)
{
System.out.println(i);
i++;
}
int i=0;
do
{
System.out.println(i);
i++;
} while(i<10);
The for loop
Loop n times
for ( i = 0; i < n; n++ ) {
// this code body will execute n times
// i from 0 to n-1
}
Nested for:
for ( j = 0; j < 10; j++ ) {
for ( i = 0; i < 20; i++ ){
// this code body will execute 200 times
}
}
For loops
Break
• A break statement causes an exit from the innermost
containing while, do, for or switch statement.
for ( int i = 0; i < 10, i++ ) {
if ( i == 3 )
{ break;
}
System.out.println(i);
}
Dr. Y. J. Nagendra Kumar - 106
Break
- Scanner class