Dept.
of Electrical &
Computer Engineering (ECE)
CSE 215 : Programming Language II
Lec 3: Introduction to JAVA
(contd.)
Dr Ziaul Hossain
Type Conversion
• Java does automatic type conversion
– Types are compatible
– Destination type is larger size than source type
– byte -> short -> int -> long -> float -> double
– Also known as Widening Casting
int i = 20; char ch = ‘Q’;
double d = i; long l = ch;
Dept. of Electrical & Computer
Date Slide 2
Engineering (ECE)
Explicit Casting
• Manual or explicit casting
– Converts larger sized data types into smaller ones
– May loose value or precision
– Also known as Narrowing Casting
Output:
Dept. of Electrical & Computer
Date Slide 3
Engineering (ECE)
Auto type-conversion in expression
Arithmetic operators Can not assign an int to byte
convert byte or char
or short into integers
solution
Dept. of Electrical & Computer
Date Slide 4
Engineering (ECE)
Type promotion
• int & char -> int
• int & float -> float
… …
• Example of multiple types together
(int + byte) * (float – double)
int * double
double
Dept. of Electrical & Computer
Date Slide 5
Engineering (ECE)
Arrays
• In C, just declaration was enough
int arr [10];
• In Java, declaration and allocation with new
keyword required
int months[];
int months[] = new int [12];
months = new int [12];
Dept. of Electrical & Computer
Date Slide 6
Engineering (ECE)
Array Example
Trying to access an array with negative index or
index value more than the size, a Java run-time
error will occur
Dept. of Electrical & Computer
Date Slide 7
Engineering (ECE)
2-D arrays
All rows contain same columns Each row contain different columns
Dept. of Electrical & Computer
Date Slide 8
Engineering (ECE)
Different array declaration syntax
Dept. of Electrical & Computer
Date Slide 9
Engineering (ECE)
Arithmetic Operators
Dept. of Electrical & Computer
Date Slide 10
Engineering (ECE)
Relational Operator
Dept. of Electrical & Computer
Date Slide 11
Engineering (ECE)
Boolean Logical Operators
Dept. of Electrical & Computer
Date Slide 12
Engineering (ECE)
Operator Precedence
Dept. of Electrical & Computer
Date Slide 13
Engineering (ECE)
If-else example
int i=10, j=15, k=70;
int a=0, b=30, c=40, d=20;
if(i == d/2) {
if(j < 20) a = b;
if(k > 100) c = d;
else a = c;
}
else a = d;
What is the value of a?
Dept. of Electrical & Computer
Date Slide 14
Engineering (ECE)
Switch-case example
Dept. of Electrical & Computer
Date Slide 15
Engineering (ECE)
Switch-case
another
example
Dept. of Electrical & Computer
Date Slide 16
Engineering (ECE)
While Loop
Another Example
int i=100, j=200;
while ( ++i < --j) ;
System.out.println(“Midpoint: ” + i);
Dept. of Electrical & Computer
Date Slide 17
Engineering (ECE)
An example of for loop and if condition
Dept. of Electrical & Computer
Date Slide 18
Engineering (ECE)
Another for Loop Example - condition
boolean done = false;
int i = 0;
for( ; !done; ) {
System.out.println("i is " + i);
if(i == 10) done = true;
i++;
}
Dept. of Electrical & Computer
Date Slide 19
Engineering (ECE)
A new format in for loop
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int i=0; i < 10; i++) sum += nums[i];
New Format allowed in Java
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x: nums) sum += x;
* You can use the break statement if want to finish the loop early
Dept. of Electrical & Computer
Date Slide 20
Engineering (ECE)
Explanation of for loop new format
public class JavaApplication3 {
public static void main(String[] args) {
int nums[] = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10 }; Output:
for(int x : nums) {
1 10
System.out.print(x + " "); 2 20
x = x * 10; // no effect on nums 3 30
System.out.println(x); 4 40
} 5 50
} 6 60
} 7 70
8 80
9 90
10 100
Dept. of Electrical & Computer
Date Slide 21
Engineering (ECE)
For-each loop in 2D array
public class JavaApplication3 {
public static void main(String[] args) {
int sum = 0;
int nums[][] = new int[3][5];
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);
for(int x[] : nums) {
for(int y : x) {
System.out.println("Value is: " + y);
sum += y;
}
}
System.out.println("Summation: " + sum);
}
}
Dept. of Electrical & Computer
Date Slide 22
Engineering (ECE)
break with Label
Dept. of Electrical & Computer
Date Slide 23
Engineering (ECE)
Input from Keyboard
• Scanner class
import java.util.Scanner;
class InputTest{
public static void main(String args[]){
Scanner scan = new Scanner (System.in);
System.out.println(“Enter an interger:”);
int n = scan.nextInt();
System.out.println(“You entered: ” + n);
}
}
• nextLine(): returns the string from input
• nextDouble(): returns the double value from input
Dept. of Electrical & Computer
Date Slide 24
Engineering (ECE)
Ternary Operator - ? :
if ( a > b )
max = a;
max = (a>b)? a : b;
else
max = b;
max= (a>b)? ( (a>c)?a:c ) : ( (b>c)?b:c ) ;
Dept. of Electrical & Computer
Date Slide 25
Engineering (ECE)
• End of Lecture 3
Dept. of Electrical & Computer
Date Slide 26
Engineering (ECE)