-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataTypes.java
83 lines (71 loc) · 2.1 KB
/
DataTypes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//compute the area of circle.
class area{
public static void main(String args[]){
double pi,r,a;
r = 21.6;//radius of circle
pi = 3.1416;//pi, approximate
a = pi*r*r;//compute area
System.out.println("Area of circle is: " + a);
}
}
//demonstrate char data type
class Chardemo{
public static void main(String args[]){
char ch1,ch2;
ch1 = 88;
ch2 = 'Y';
System.out.println("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
//char variables behave like integers
class Chardemo2{
public static void main(String args[]){
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++;
System.out.println("ch1 is now " + ch1);
}
}
//Demonstrate boolean values.
class BoolTest{
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));
}
}
//Demonstrate Dynamic initialization
class DynInit{
public static void main(String args[]){
double a =3.0,b =4.0;
//c is dynamically initialized
double c=Math.sqrt(a*a + b*b);
System.out.println("Hypotennuse is " + c);
}
}
//Demonstrate block scope.
class Scope{
public static void main(String args[]){
int x;//known to all code within main
x =10;
if(x == 10){//started new scope
int y = 20;//known 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 not known here
//x is still known here.
System.out.println("x is " + x);
}
}