Professional Documents
Culture Documents
Lab2 OOP
Lab2 OOP
Objectives
Learn about data types
Learn about variables
Learn about type casting, promotion, inference
Theory
Java Is a Strongly Typed Language. Every variable has a type, every expression
has a type, and every type is strictly defined. Java defines eight primitive types of
data: byte, short, int, long, char, float, double, and boolean. The primitive types are
also commonly referred to as simple types.
There are two kinds of floating-point types, float and double, which represent
single- and double-precision numbers (32 and 64 bit to store a value), respectively.
Program
//Code listing 2
Department of Computer Systems Engineering
// Compute the area of a circle.
class Area {
public static void main(String[] args) {
double pi, r, a;
In Java, the data type used to store characters is char. A key point to understand is
that Java uses Unicode to represent characters. Thus, in Java char is a 16-bit type.
The range of a char is 0 to 65,535. There are no negative chars.
'A' : ASCII 65 : Unicode '\u0041'
'a' : ASCII 97
'0' : ASCII 48
Program
//Code listing 3
// Demonstrate char data type.
class CharDemo {
public static void main(String[] args) {
char ch1, ch2;
Program
//Code listing 4
// char variables behave like integers.
class CharDemo2 {
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
Java has a primitive type, called boolean, for logical values. It can have only one of
two possible values, true or false. This is the type returned by all relational
operators, as in the case of a < b. boolean is also the type required by the
conditional expressions that govern the control statements such as if and for. The
true literal in Java does not equal 1, nor does the false literal equal 0
Program
//Code listing 5
// 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);
b = false;
if(b) System.out.println("This is not executed.");
Program
//Code
//Run on command windown for correct results
public class Aziz {
public static void main(String[] args) {
System.out.println("65Decimal in Octal is 101 for \101");
System.out.println("65Decimal in Hexadecimal is 41 for \u0041");
System.out.println("Single quote \'");
System.out.println("Double quote \"");
System.out.println("Backslash \\");
System.out.println("Karriage return \rC");
System.out.println("Line1\nMoved to line2");
System.out.println("Line1\nMoved to line2");
System.out.println("\tTab for space of 8 characters");
System.out.println("\sSpace of 1 character");
System.out.println("Backspace\bE");
}
}
Output:
65Decimal in Octal is 101 for A
65Decimal in Hexadecimal is 41 for A
Single quote '
Double quote "
Backslash \
Carriage return
Department of Computer Systems Engineering
Line1
Moved to line2
Line1
Moved to line2
Tab for space of 8 characters
Space of 1 character
BackspacE
Homework: checkout two more escape sequences (i) formfeed \f (ii) continue line \endofline
The variable is the basic unit of storage in a Java program. A variable is defined by
the combination of an identifier, a type, and an optional initializer. The identifier is
the name of the variable.
type identifier [ = value ][, identifier [= value ] …];
Program
//Code listing 6
// 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);
Program
//Code listing 7
// Demonstrate block scope.
class Scope {
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; // known only to this block
Program
//Code listing 8
// Demonstrate lifetime of a variable.
class LifeTime {
public static void main(String[] args) {
int x;
it is always possible to assign an int value to a long variable. For example long
a=10;. But there is no automatic conversion defined from double to byte, numeric
types to char or boolean, char to/from boolean, but it is still possible by using cast.
Type promotion is the automatic type conversion of data type done by Java. Java
adopts widening conversion.
Type casting is conversion of one data type to other and this type of conversion is
defined by user. User mostly adopts narrowing conversion.
Automatic type promotion in an Expression:
(i)
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c; //a*b exceeds byte range, Java automatically promotes each byte, short, or char operand to int
//promotion priority starts with double, float, long, and then int. One value in double promotes all values to double in
an expression.
(ii)
byte b = 50; b = b * 2; // Error! Cannot assign an int to a byte!, because 2 is an int, so expression is solved as int.
Program
//Code listing 10
// Demonstrate casts, defined by user.
class Conversion {
public static void main(String[] args) {
byte b;
int i = 257;
double d = 323.142;
In the first subexpression, f * b, b is promoted to a float and the result of the subexpression is float. Next,
in the subexpression i/c, c is promoted to int, and the result is of type int. Then, in d * s, the value of s is
promoted to double, and the type of the subexpression is double. Finally, these three intermediate values,
float, int, and double, are considered. The outcome of float plus an int is a float. Then the resultant float
minus the last double is promoted to double, which is the type for the final result of the expression.
Program
//Code listing 11
class Promote {
public static void main(String[] args) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
Output:
238.14 + 515 - 126.3616
result = 626.7784146484375
Program
//Code listing 19
// A simple demonstration of local variable type inference.
class VarDemo {
public static void main(String[] args) {
Non-primitive data types such as arrays, strings, classes, and interface will be
discussed in next labs.
Exercise program 1
// Write a java program to compute the area of triangle using a common trigonometric
formula
//Code
Output:
Exercise program 2
// Write a java program that converts centigrade into Fahrenheit
//Code
Output: