Chapter 1
Chapter 1
POP OOP
-Linear Flow of data by functions -Non Linear flow of data by Object
-Structure Oriented -Object Oriented
-Top-down approach -Bottom-up approach.
- e.g.: C - e.g.: C++ & JAVA
Fundamental of Programming
What is Java???
• JAVA is general purpose (high level) programming
language, designed to produce programs that will
run on any computer system.
Or
• JAVA is programming language expressly designed for
use in the distributed environment of the internet
• This language was initially called OAK. Since this name was
registered by some other company, later it change to the name
JAVA.
Hi.class
JAVA JAVA Machine
Byte code
Compiler Interpreter code
Java Java
l e Compiler Interpreter
pi
m
n
Co
Ru
Java
m/c
Java Source Java Bytecode code
<file>.java
Java <file>.class
Dis-assembler
Java software development kit SDK
- Subtraction a-b
* Multiplication a*a
/ Division a/b
% Modulus a%b
JAVA TOKEN
2. Assignment Operators: Assignment operators are used
to assign the value to the variable.
Consider A=5;
Operator Example Output
= A=5 5
+= A+=5
-= A-=3
*= A*=3
/= A/=2
%= A%=3
JAVA TOKEN
3. Increment / Decrement Operators: These operators are
used to increase & decrease the value by 1.
Consider A=5
Operator Meaning Example Output
++ Increment by 1 A++
-- Decrement by 1 A--
|| OR (A>c) ||
(0,0=0 o/w 1) (A<b)
! NOT !(A<=c)
(1=0 / 0=1)
JAVA TOKEN
6. Conditional Operators : also called as Ternary operator,
used to check wheatear the condition is true or false
Syntax
Variable_name=Condition ? Expreesion1 : Expression2;
Condition
Expression 1 true (1)-> Expression 1
Expression 2 False (0)-> Expression 2
int a=10;
int b= (5>3)?(a+10):(a-5); o/p= 20
JAVA TOKEN
7. Special Operators: Java supports two special operators.
That is, instanceof and dot operator (.).
a) Instanceof operator: It returns true if the object on the
left-hand side is an instance of the class given on the right-
hand side.
For example: maharashtra instanceof India
returns true if the object ‘maharashtra’ is the object of class ‘India’.
Otherwise it will return false.
b) Dot Operator (.): It is used to access the instance
variable and methods of the class using objects.
Ex: company.salary(); //reference to method salary
company.employee; //reference to variable employee
JAVA TOKEN
8. Bitwise Operator: In order to manipulate the data at the
bit level, the bitwise operators are provided in Java
Consider: A=12 (1100) & b=5 ( ? )
final int=12; Final float=3.14; final char a=‘A’; final char a=“Hello”;
Constant
Escape Constant
Escape Meaning
\n New Line
\t Horizontal Tab
\b Back space
\’ Single Quote
\” Double Quote
\\ Back slash
To read string & Char.
• Syntax
DataInputStream d=new DataInputStream(System.in);
– String str=d.readLine(); // for string
– char ch= (char) System.in.read(); // for character
• Exmple:
– double A;
– A=Math.ceil(12.60);
Math is class
found in lang
package
Decision making and Branching
• if
• if-else
• nested if-else
• ladder if-else
• Switch case
If Statement
Syntax:
if (Condition)
{
// Statements for true;
}
//Statements for true/false;
If Statement
If-else Statement
Syntax:
if (Condition)
{
// Statements for true;
}
else
{
// Statements for false;
}
// Statements for true/false;
If-else Statement
Nested if-else Statement
Syntax:
if (Condition1)
{
// Statements for true;
if(condition2)
{
// Statements for true;
}
}
else
{
// Statements for false;
if(condition3)
{
// Statements for true;
}
}
// Statements for true/false;
Ladder if-else Statement
Syntax:
if (Condition1)
{
// condition 1 true
}
else
if(condition 2)
{
//condition one false & condition is 2 true
}
else
if(condition 3)
{
// cond. 1, 2 is false & cond. 3 is true
}
:
:
else
{
//all condition false
}
Switch case
• Menu Driven Program base on user choice
• Syntax
Value may be
switch (Value/Expression) Integer / character / String/symbol
{
case value1:
//true statements
break;
case value2:
//true statements
break;
:
:
case value n:
//true statements
break;
default :
//statements
}
Switch case
Decision making and
looping
• while
• do-while
• for
While loop
Initialization;
while(condition)
{
// statement true
Increment/decrement operator;
}
While loop
Do-While loop
Initialization;
do
{
// statement
Increment/decrement operator;
} while(condition);
Do-While loop
For loop
• syntax
1 2 4
continue
break statement
• break : It is used to jump out of the loop when
this statement is encountered inside the loop.
• Generally, the ‘break’ statement is associated
with an ‘if’ statement. That is, when the condition
is satisfied, the jump will occur.
• break statement directly get exited from the
current loop without considering the condition.
• Example:
break statement
OUTPUT: 1 3 5 7 9
END OF CHAPTER 1