Lec 4
Lec 4
I Semester 2008-09
Lecture 4
II Block of code
1
(Primitive) Data types in JAVA
2
Operation defined on Data types for Integer
Arithmetic operators
+ addition Integer
− subtraction “
∗ multiplication “
/ integer division “
% mod “
19/4 : ?
19%4 : ?
3
Operation defined on Data types for Integer
Arithmetic operators
+ addition Integer
− subtraction “
∗ multiplication “
/ integer division “
% mod “
19/4 : 4
19%4 : 3
4
Operation defined on Data types for Integer
Relational operators
== equal “
! = not equal “
2<3:?
3 == 3 : ?
5
Operation defined on Data types for Integer
Relational operators
== equal “
! = not equal “
2 < 3 : true
3 == 3 : true
6
Operation defined on Data types for fractional numbers
19.0/4.0 : 4.75
19.0%4.0 : 3.0
7
Operation defined on Data type for Boolean
Logical operators
! NOT boolean
&, && AND boolean
|, || OR boolean
Relational operators
== Equal boolean
!= Not equal boolean
8
Evaluation of Expressions
2 + 3 ∗ 4 is equal to ??
96/4/2 is equal to ??
9
Evaluation of Expressions
2 + 3 ∗ 4 is equal to 14
96/4/2 is equal to 12
10
Evaluation of Expressions
An important tool :
It is always better to use parentheses in writing any expression.
However, if the expression is not fully parenthesized, then the following rules are
followed
• two consecutive operators have same precedence, they are evaluated from
left to right.(called left associative).
11
II : Block of code
For example
{
Statement1;
Statement2;
.
.
.
Statementk;
}
12
Scope of a variable
13
Example : Scope of vatiable
1.class scope
2.{ public static void main(String args[])
3. {
4. int i;
5. i = 100;
6. System.out.println("value of i here is "+i);
7. {
8. int j;
9. j=55;
10. i = i*j;
11. System.out.println(i);
12. System.out.println(j;)
13. }
14. System.out.println(j);
15. }
16.}
The above code will give copilation error at line 14 because no j exists at this line.
14
Motivation for If statement
class if_example
{
public static void main(String args[])
{
int i,j,max;
.
.
//---write code here so that max
//---stores the bigger of i and j
}
}
15