Java U1
Java U1
2301CS201
Object Oriented
Programming
Syllabus
Syllabus
OOP Java is the easiest, scoring and my favorite subject
Sr. Content T W%
Hrs
1 Introduction to JAVA 10 20
Unit-1
Introduction to
java
Evolution of Java
Features of JAVA,
JDK, JRE, JVM, Byte Code
Installing and configuring JAVA
Basic Structure of Java program
Compiling and Running Java files
Unit-1
OOP Java is the easiest, scoring and my favorite subject
Introduction
Welcome to the world of Programming
What is Language?
A language is a system of communication used by a particular country or
community. OOP Java is the easiest, scoring and my favorite subject
Introduction to
JAVA Installation
Java is a general-purpose computer-programming language
that is open source, platform independent, object- OOP Java is the easiest, scoring and my favorite subject
2
3
4
of C++.
Environment (JRE)
Java Development Kit
Compiler Java
(javac.exe) Packages
(JDK)
Java (math, util, JVM
Application awt etc…)
Launcher
(java.exe), Runtime
AppletViewer Libraries
, etc..)
Development JRE
tools
and
JRE to run the programs.
The tools include
compiler (javac.exe),
Java application launcher
(java.exe),
Appletviewer, etc…
Java application launcher
(java.exe) opens a JRE, loads the
class, and invokes its main
method.
(Program)
Compil
er
JVM
JVM JVM
(Window
(Linux) (Mac)
s)
(Program)
Here, JVM is an interpreter for bytecode.
Compil
Translating a Java program into bytecode helps er
makes it much easier to run a program in a
wide variety of environments. Byteco .class file
i.e “WRITE ONCE RUN ANYWHERE” de
Thus, the interpretation of bytecode is the
easiest way to create truly portable programs.
Interpreted Java program are highly secured,
because the execution of every Java program is
under the control of the JVM.
{ will start
public static void main(String[] args)
{ String must start with capital
letter
System.out.println("Hello World");
} System must start with
} capital letter
Unit-1(part-II)
Data types,
Variables &
Operators
Prof. Swati R Sharma
Computer Engineering
Department
Darshan University, Rajkot
[email protected]
Looping
What we will learn OOP Java is the easiest, scoring and my favorite subject
Unit-1(part-II)
OOP Java is the easiest, scoring and my favorite subject
Tokens
Tokens
The smallest individual unit of a language / program is known as a token.
Tokens are basic building blocks of any language.
OOP Java is the easiest, scoring and my favorite subject
Identifiers
Identifiers
They are used for class names, method names and variable names.
OOP Java is the easiest, scoring and my favorite subject
Java Datatypes
Primitiv Non-
e primitive
Floating-
Integer Charact Boolea
point Class
s ers n
numbers
For example, if you want to put quotes within quotes you must use the
escape sequence, \", on the interior quotes.
System.out.println("Good Morning \"World\"");
Variables in Java
Variables in Java
The variable is the basic unit of storage in a Java program.
OOP Java is the easiest, scoring and my favorite subject
Declaringtype
a Variable
identifier [ = value][, identifier [=
value] ...] ;
It is Java’s atomic
name of
types, or the name
the
of a class or
variable
interface.
Operator
Perform definite operation
Operators
1. Arithmetic Operators
OOP Java is the easiest, scoring and my favorite subject
2. Relational Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Conditional / Ternary Operator
7. Instance of Operator
Exampl
Operator Meaning Description
e
+ Addition a+b Addition of a and b
- Subtraction a–b Subtraction of b from a
* Multiplication a*b Multiplication of a and b
/ Division a/b Division of a by b
Modulo division-
% a%b Modulo of a by b
remainder
They check the relationship between two operands, if the relation is true,
it returns 1; if the relation is false, it returns value 0.
Relational expressions are used in decision statements such as if, for,
while, etc…
Operator Meaning Example Description
< is less than a<b a is less than b
<= is less than or equal to a <= b a is less than or equal to b
> is greater than a>b a is greater than b
>= is greater than or equal a >= b a is greater than or equal to b
to
== is equal to a == b a is equal to b
!= is not equal to a != b a is not equal to b
== Equals (A == B) is not
true.
!= Not Equals (A != B) is true.
Operators Precedence
& Associativity
Priority matters!
Operator Precedence & Associativity
How does java evaluate 1 + 10 * 9 ?
(1 + 10 ) * 9 = 99
OOP Java is the easiest, scoring and my favorite subject
OR 1 + (10 * 9) = 91
To get the correct answer for the given problem Java came up with
Operator precedence.
Multiplication have higher precedence than addition so correct answer will
be 91 in this case.
For Operator, associativity means that when the same operator appears in
a row, then to which direction the expression will be evaluated.
How does java evaluate 1 * 2 + 3 * 4 / 5 ???
2 + 12 / 5
2 + 2.4
4.4
2) All operators
Priori Opera
ty tor
Description with the same precedence have
Associati
vity
Priori
ty same
Operator associativity
Description
Associati
vity
() left-to- is equal to/is not equal left-to-
Parentheses (function call) 7 == != right
[] right to
Brackets (array subscript)
1 . left-to-
Member selection via object name 8 & Bitwise AND
a++ right
Postfix increment/decrement
a—
left-to-
Prefix increment/decrement right-to- 9 ^ Bitwise exclusive OR
++a right
Unary plus/minus left
—a left-to-
Logical negation/bitwise complement 10 | Bitwise OR
+– right
Cast (convert value to temporary
!~
2 value of type) left-to-
(type) 11 && Logical AND
De-reference right
*
Address (of operand)
& left-to-
Determine size in bytes on this 12 || Logical OR
sizeof right
implementation
right-to-
left-to- 13 ?: Ternary conditional
3 * / % Multiplication/division/modulus left
right
= Assignment right-to-
left-to- left
4 + – Addition/subtraction += -= *= /= Shorthand Assignments
right
%= &= Bitwise
<< left-to- 14 ^= |= exclusive/inclusive
5 Bitwise
Prof. Swati shift
R Sharma left, Bitwise shift right
#2301CS201(OOP) Unit 01 – Introduction to java 65
Exercise
Sr. Exercise Answer
OOP Java is the easiest, scoring and my favorite subject
1. int i=1; 4
i=2+2*i++;
2. int a=2,b=7,c=10; 0
c=a==b;
3. int a=2,b=7,c=10; 1
c=a!=b;
4. int a=100 + 200 / 10 - 3 * 10 90
5. int a = 2, b = 6, c = 12, d; 14
d = a * b + c / b;
System.out.println("The value of d ="+d);
int t;
System.out.println(t);
}
}
Unit-1(Part-III)
Array and
Control
Statements
Prof. Swati R Sharma
Computer Engineering
Department
Darshan University, Rajkot
[email protected]
Looping
What we will learn OOP Java is the easiest, scoring and my favorite subject
Unit-1
OOP Java is the easiest, scoring and my favorite subject
Decision Making
Compiler executes program statements sequentially.
OOP Java is the easiest, scoring and my favorite subject
… …
…
Prof. Swati R Sharma #2301CS201(OOP) Unit 01 – Introduction to java 72
Decision Making Statements
Commonly used decision making statements are
One way Decision:
OOP Java is the easiest, scoring and my favorite subject
if
One way Decision
if
if statement is the most simple decision-making statement also known as
simple if. OOP Java is the easiest, scoring and my favorite subject
2.class MyProgram{
3.public static void main (String[] args){
4.int x;
5.Scanner sc = new Scanner(System.in);
6. x = sc.nextInt();
7. if(x > 0){
8. System.out.println("number is a
positive");
9. }
10.}
DIFFERENCE IS NEGATIVE.
2.class MyProgram{
3.public static void main (String[] args){
4.int x;
5.Scanner sc = new Scanner(System.in);
6. x = sc.nextInt();
7. if( x % 2 == 1 ){
8. System.out.println("number is a odd");
9. }
10. if( x % 2 == 0 ){
11. System.out.println("number is a
even");
12. }
13.}
if…else
Two way Decision
If…else
For a simple if, if a condition is true, the compiler executes a block of
OOP Java is the easiest, scoring and my favorite subject
2.class MyProgram{
3.public static void main (String[] args){
4.int x;
5. Scanner sc = new Scanner(System.in);
6. x = sc.nextInt();
7. if (x > 0){
8. System.out.println("Number is
positive");
9. }//if
10. else{
11. System.out.println("Number is
negative");
12. }//else
13. }//main
14.}//class
Prof. Swati R Sharma #2301CS201(OOP) Unit 01 – Introduction to java 81
WAP to print if a number is odd or even
1.import java.util.*;
OOP Java is the easiest, scoring and my favorite subject
2.class MyProgram{
3.public static void main (String[] args){
4. int x;
5. Scanner sc = new Scanner(System.in);
6. x = sc.nextInt();
7. if( x % 2 == 1 ){
8. System.out.println("number is a odd");
9. }
10. else{
11. System.out.println("number is a even");
12. }
13.}
if…else if…else
Multi way Decision
if…else if…else
if…else if…else statement is also known as if- if(condition 1)
else-if ladder which is used for multi way decision {
OOP Java is the easiest, scoring and my favorite subject
making. statement-block1;
}
It is used when there are more than two different else if(condition 2)
conditions. {
It tests conditions in a sequence, from top to statement-block2;
}
bottom.
else if(condition 3)
If first condition is true then the associated block {
with if statement is executed and rest of the statement-block3;
conditions are skipped. }
else if(condition 4)
If condition is false then then the next if condition {
will be tested, if it is true then the associated block statement-block4;
is executed and rest of the conditions are skipped. }
Thus it checks till last condition. else
default-statement;
Condition is tested only and only when all previous
conditions are false.
TheProf.last
Swati R else
Sharma is the default block Unit
#2301CS201(OOP) which will tobe
01 – Introduction java 85
if-else-if ladder
OOP Java is the easiest, scoring and my favorite subject
if(condition 1)
another if statement. {
if(condition 2)
Nested if statements mean an if statement inside {
another if statement. nested-block;
}
The statement connected to the nested if else
statement is only executed when -: {
nested-block;
condition of outer if statement is true, and }
condition of the nested if statement is also true. }//if
else if(condition 3)
Note: There could be an optional else statement {
associated with the outer if statement, which is statement-block3;
}
only executed when the condition of the outer if else(condition 4)
statement is evaluated to be false and in this {
case, the condition of nested if condition won't statement-block4;
}
be checked at all.
if(username==1234){
if(password==987654){
System.out.println("Your Balance is
="+balance);
}
else{
System.out.println("Password is
invalid");
}
}
else{
System.out.println("Username is invalid");
}
Prof. Swati R Sharma #2301CS201(OOP) Unit 01 – Introduction to java 91
Exercise
In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic
OOP Java is the easiest, scoring and my favorite subject
switch…case
n-way Decision
Switch…case
switch…case is a multi-way decision OOP Java is the easiest, scoring and my favorite subject
switch (expression)
making statement. {
case constant 1:
It is similar to if-else-if ladder statement. // Statement-1
break;
It executes one statement from multiple
conditions. case constant 2:
// Statement-2
break;
case constant 3:
// Statement-3
break;
default:
// Statement-default
// if none of the above case
matches then this block would
be executed.
}
Prof. Swati R Sharma #2301CS201(OOP) Unit 01 – Introduction to java 94
Switch…case: WAP to print day based on
number entered
6.switch (d) {
1.public class Demo { 7. case 1:
2. public static void 8.
OOP Java is the easiest, scoring and my favorite subject
System.out.println(“Monday“); break;
main(String[] 9. case 2:
args){ 10. System.out.println(“Tuesday“); break;
3. int d; 11. case 3:
4. Scanner sc= new 12. System.out.println(“Wednesday“); break;
Scanner(System.in); 13. case 4:
5. d = sc.nextInt(); 14. System.out.println(“Thursday“); break;
15. case 5:
16. System.out.println(“Friday“); break;
17. case 6:
18. System.out.println(“Saturday“); break;
19. case 7:
20. System.out.println(“Sunday“); break;
21. default:
22. System.out.println(“Invalid Day“);
23. } //switch
24. }
25.}
Introduction to loop
Repeatedly execute a block of statements
Loop
Sometimes we need to repeat certain actions several times or till the
some criteria is satisfied. OOP Java is the easiest, scoring and my favorite subject
true-block true-block
while
Entry Controlled Loop
while
while is a entry controlled loop.
OOP Java is the easiest, scoring and my favorite subject
2. Write a program that calculates and prints the sum of the even integers
from 1 to 10.
for(;;)
Entry Controlled Loop
for
for is an entry controlled loop
OOP Java is the easiest, scoring and my favorite subject
Statements inside the body of for are repeatedly executed till the
for(i=1; i <= 5; i+
condition is true
for (initialization; condition; increm int i = 1;
+)
ent while (i <= 5) {
{
/decrement)
{ System.out.print("He
System.out.print("H
// statements ll o World!");
ello World!");
} i++;
}
}
The initialization statement is executed only once, at the beginning of
the loop.
Then, the condition is evaluated.
If the condition is true, statements inside the body of for loop are
executed
If the condition is false, the for loop is terminated.
Then, increment / decrement statement is executed
Prof. Swati R Sharma #2301CS201(OOP) Unit 01 – Introduction to java 111
For Loop
for loop is used to iterate a part of the program several times.
OOP Java is the easiest, scoring and my favorite subject
Write a program that calculates and prints the sum of the even integers
from 1 to 10.
do…while
Exit Controlled Loop
do…while
do…while is an exit controlled loop.
OOP Java is the easiest, scoring and my favorite subject
Statements inside the body of do…while are repeatedly executed till the
condition is true.
while
do loop executes zero or more times, do…while loop executes one or
more
{ times. Flowchart of while Flowchart of do…while
// true-
block
conditio False
} true-block
while(condition) n
; True
conditio
true-block True n
False
continue
Skip the statement in the iteration
continue
Sometimes, it is required to skip the remaining statements in the loop and
continue with the next iteration. OOP Java is the easiest, scoring and my favorite subject
break
Early exit from the loop
break
Sometimes, it is required to early exit the loop as soon as some situation
occurs. OOP Java is the easiest, scoring and my favorite subject
1.import java.util.*;
2.class BreakDemo{ OOP Java is the easiest, scoring and my favorite subject
False Label
conditi Statement
on Loop Body
True True conditi
True conditi on
Loop Body got
on o
False
False labe
l
Prof. Swati R Sharma #2301CS201(OOP) Unit 01 – Introduction to java 125
OOP Java is the easiest, scoring and my favorite subject
nested loop
loop within a loop
WAP to print given pattern (nested loop)
* 1.class PatternDemo{
OOP Java is the easiest, scoring and my favorite subject
Introduction to Array
Large Data Handling
Why Array?
Very often we need to deal with relatively large set of
data. OOP Java is the easiest, scoring and my favorite subject
E.g.
Percentage of all the students of the college. (May be in
thousands)
Age of all the citizens of the city. (May be lakhs)
We need to declare thousands or lakhs of the variable
to store the data which is practically not possible.
We need a solution to store more data in a single
variable.
Array is the most appropriate way to handle such
data.
As per English Dictionary, “Array means collection
or group or arrangement in a specific order.”
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
int percentage[10
];
Fixed Size Sequential Same Data type Single Variable
Name
The size of an array All the elements of Data type of all the
is fixed at the time an array are stored elements of an All the elements of
of declaration in a consecutive array is same which an array will be
which cannot be blocks in a memory. is defined at the referred through
changed later on. time of declaration. common name.
10 (0 to 9)
Here array size is Here data type is Here array name
10. int is percentage
35 13 28 106 35
a 42 5 83 97 14
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
2. class ArrayDemo1{
3. public static void main (String[] args){
4. int i, n;
5. int[] a=new int[5];
6. Scanner sc = new Scanner(System.in);
7. System.out.print("enter Array Length:"); Output:
8. n = sc.nextInt(); enter Array
Length:5
9. for(i=0; i<n; i++) {
enter a[0]:1
10. System.out.print("enter a["+i+"]:"); enter a[1]:2
11. a[i] = sc.nextInt(); enter a[2]:4
12. } enter a[3]:5
13. for(i=0; i<n; i++) enter a[4]:6
14. System.out.println(a[i]); 1
15. } 2
16.} 4
5
6
1. import java.util.*;
2. class ArrayDemo1{ OOP Java is the easiest, scoring and my favorite subject
Multidimensional Array
Multidimensional Array
OOP Java is the easiest, scoring and my favorite subject
length field:
If we use length field with multidimensional array, it will return length of first
dimension.
Here, if runPerOver.length is accessed it will return 3
Also if runPerOver[0].length is accessed it will be 6
Thank You