01-DataTypes Operators
01-DataTypes Operators
JAVA PROGRAMMING
Java Programming
1
12/10/2022
Java Programming
Java Programming
2
12/10/2022
Integers 5
Java Programming
Example 6
/* Compute the number of cubic inches
* in 1 cubic mile. 1 mile = 63360 inches
*/
public class Inches {
public static void main(String[] args) {
long ci;
long im;
im = 63360;
ci = im * im * im;
System.out.println("There are " + ci
+ " cubic inches in cubic mile.");
}
}
Java Programming
3
12/10/2022
Floating-Point Types 7
Java Programming
Example 8
Java Programming
4
12/10/2022
Characters 9
Java Programming
Java Programming
10
5
12/10/2022
Example 11
// Character variables can be handled like integers.
public class CharArithDemo {
public static void main(String[] args) {
char ch;
ch = 'X';
System.out.println("ch contains " + ch);
ch++; // increment ch
System.out.println("ch is now " + ch);
ch = 90; // give ch the value Z
System.out.println("ch is now " + ch);
}
}
Java Programming
11
Boolean type 12
Java Programming
12
6
12/10/2022
13
// Demonstrate boolean values.
public class BoolDemo {
public static void main(String[] args) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
Java Programming
13
Java Programming
14
7
12/10/2022
15
/* Compute the distance to a lightning
* strike whose sound takes 7.2 seconds to reach you
*/
public class Sound {
public static void main(String[] args) {
double dist;
dist = 7.2 * 1100;
System.out.println("The lightning is " + dist +
" feet away.");
}
}
Java Programming
15
Literals 16
Java Programming
16
8
12/10/2022
• Example:
• int hex = 0xFF; // 255 in decimal
• int oct = 011; // 9 in decimal
• int binary = 0b1100; // 12 in decimal
Java Programming
17
Java Programming
18
9
12/10/2022
String Literals 19
Java Programming
19
Variables 20
type varname;
• where type is the data type of the variable, and
varname is its name
• Initializing a Variable:
type var = value;
• Example:
int count = 10; // give count an initial value of 10
char ch = 'X'; // initialize ch with the letter X
float f = 1.2F; // f is initialized with 1.2
int a, b = 8, c = 19, d; // b and c have initializations
Java Programming
20
10
12/10/2022
Dynamic initialization 21
Java Programming
21
22
11
12/10/2022
Example: Scope 23
// Demonstrate block scope
public class ScopeDemo {
public static void main(String[] args) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // know only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y == 100; // Error! y is not known here
// x is still known here.
System.out.println("x is " + x);
}
}
Java Programming
23
Example: Lifetime 24
// Demonstrate lifetime of a variable.
public class VarInitDemo {
public static void main(String[] args) {
int x;
for(x = 0; x < 3; x++) {
int y = -1;
// y is initialized each time block is entered
System.out.println("y is : " + y);
// this always prints -1
y = 100;
System.out.println("y is now: "+ y);
}
}
}
Java Programming
24
12
12/10/2022
Operators 25
Java Programming
25
Arithmetic operators 26
Java Programming
26
13
12/10/2022
Example 27
// Demonstrate the % operator
public class ModDemo {
public static void main(String[] args) {
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0;
System.out.println("Result and remainder of 10 / 3: ” +
iresult + " " + irem);
System.out.println("Result and remainder of 10.0/3.0: ”+
dresult + " " + drem);
}
}
Java Programming
27
Java Programming
28
14
12/10/2022
Java Programming
29
30
Java Programming
30
15
12/10/2022
31
Java Programming
32
16
12/10/2022
Example 33
// Demonstrate the short-circuit operators.
public class SCops {
public static void main(String[] args) {
int n, d, q;
n = 10;
d = 2;
if(d != 0 && (n %d ) == 0)
System.out.println(d + " is a factor of " + n);
d = 0; // now, set d to zero
// Since d is zero, the second operand is not evaluated
if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
/* Now, try same thing without short-circuit operator.
* This will cause a divide-by-zero error.
*/
if(d != 0 & (n % d) == 0)
System.out.println(d + " is a factor of " + n);
}
} Java Programming
33
Assignment operator 34
var = expression;
• The type of var must be compatible with the type of
expression.
• Example:
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
Java Programming
34
17
12/10/2022
Shorthand assignments 35
Java Programming
35
36
18
12/10/2022
Java Programming
37
Example 38
// Demonstrate automatic conversion from long to double
public class LtoD {
public static void main(String[] args) {
long L;
double D;
L = 100123285L;
D = L; // automatic conversion from long to double
System.out.println("L and D: " + L + " " + D);
}
}
Java Programming
38
19
12/10/2022
39
// This program will not compile.
public class DtoL {
Java Programming
39
(target-type) expression
• target-type specifies the desired type to convert the
specified expression to.
• Example:
double x = 3.0, y = 2.0;
// .....
int z = (int)(x / y);
Java Programming
40
20
12/10/2022
// Demonstrate casting.
public class CastDemo {
public static void main(String[] args) { 41
double x, y;
byte b;
int i;
char ch;
x = 10.0;
y = 3.0;
i = (int)( x / y); //cast double to int
System.out.println("Integer outcome of x / y: " + i);
i = 100;
b = (byte) i;
System.out.println("Value of b: " + b);
i = 257;
b = (byte) i;
System.out.println("Value of b: " + b);
b = 88; // ASCII code for X
ch = (char) b;
System.out.println("Value of ch: " + ch);
}
Java Programming
}
41
Operator precedence 42
Java Programming
42
21
12/10/2022
Java Programming
43
Result 44
Java Programming
44
22
12/10/2022
Java Programming
45
Example: Promotion 46
// Demonstrate a promotion
public class PromDemo {
public static void main(String[] args) {
byte b;
int i;
b = 10;
i = b * b; // Ok, no cast needed
b = 10;
// Cast is needed here to assign an int to a byte
b = (byte) (b * b);
System.out.println("i and b: " + i + " " + b);
}
}
Java Programming
46
23
12/10/2022
Java Programming
47
Java Programming
48
24
12/10/2022
Exercises 49
Java Programming
49
50
QUESTION ?
Java Programming
50
25