Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I...

40
1 Department of Computer Engineerin 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY More on Data Types

Transcript of Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I...

Page 1: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

1

Department of Computer Engineering

2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

More on Data Types

Page 2: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

2 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Objective

Students should:

• Understand how to work with different data types including calculations and conversions.

• Be aware of imprecision in floating point calculations.

• Understand overflow, underflow, Infinity, NaN, and divided-by-zero exception.

• Understand and be able to use increment and decrement operators correctly.

• Be able to use compound assignments correctly.

Page 3: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

3 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Precedence and Associativity

Operator Precedence

Associativity

Grouping Operator () 17 None

Unary Operator +,-,! 13 Right

Multiplicative Operator *,/, %

12 Left

Additive Operator +,- 11 Left

Relational Ordering >,<,>=,<=

10 Left

Relational Equality ==,!= 9 Left

Logical AND && 4 Left

Logical OR || 3 Left

Assignment Operator = 1 Right

Page 4: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

4 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Compound Assignment

For these operators: +,-,*,/,%Assignment Statement of this form :variable = variable operator expressioncan be written as

variable operator= expressionExample:x +=5; x = x+5; x -=5; x = x-5; x *=5; x = x*5; x /=5; x = x/5;x %=5; x = x%5;

Same variable

No Space

Page 5: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

5 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Caution!

Be careful with complex expressions

x /= y + 5; ≠ x = x / y + 5;

x /= y + 5; x = x / (y+5);

Page 6: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

6 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Assignment Operator

Associativity of assignment operator is RIGHT. Which means :

int x, y;x = y= 10; // y 10 x 10x = 5*(y = 6); // y 6 x 30x = 5*y = 6; // ERROR

Page 7: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

7 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Numeric Data Type Specification

• An integer number is an int data type by default.

• An integer number with l or L at the end becomes a long data type.

• A floating point number is an double data type by default.

• A floating point number with f or f at the end becomes a float data type.

• A floating point number with d or D at the end becomes a double data type.

Page 8: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

8 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Examples

10 int

10l or 10L long

10.0 double

10.0f or 10.0F float

10.0d or 10.0D double

Page 9: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

9 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Examples

int i = 1;long l = 1L;long k = 256l;float f = 2.0f;float g = -0.56F;double d1 = 1.0;double d2 = 2.0D;double d3 = 2.0d;

Page 10: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

10 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Data Types Conversion

Data type conversion can be done explicitly by using cast operators, written as the name of the data type, to which you want to convert your data to, enclosed in parentheses. Cast operators are put in front of the data to be converted. Example: int a = (int) 23.85;

Page 11: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

11 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Cast Operator

• Conversion by a cast operator is done before +,-,*,\, and %, but after () and unary operators. (precedence=13)

• Converting floating point numbers to integers will cause the program to discard all of the floating points, no matter how large they are.

• the conversion from a wider data type to a narrower data type is called narrowing, while the opposite is called widening.

Page 12: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

12 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example

public class TypeConversionDemo2 {public static void main(String[] args){

int i,j;double d1 = 0.5, d2 = 0.5, d3,d4;i = (int)d1+(int)8.735f;j = (int)(d1+d2);System.out.println("i = "+i);System.out.println("j = "+j);d3 = (double)i-(double)j;d4 = (double)(i-j);System.out.println("d3= "+d3);System.out.println("d4= "+d4);

} }

Page 13: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

13 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

i = (int)d1+(int)8.735f;

80.5

0

j = (int)(d1+d2);

1.0

0.50.5

1

Page 14: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

14 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Converting Primitive Data Types

from\tobyte short int long float double char

boolean

byte A A A A A C X

short C A A A A C X

int C C A A* A C X

long C C C A* A* C X

float C C C C A C X

double C C C C C C X

char C C A A A A X

boolean X X X X X X X

* indicates that precision lost might occur from the conversion.

A automatic; C casting; X cannot convert

Page 15: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

15 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Caution!

When cast operator is use for narrowing, although it may prevent exception (error), it can produce unexpected result.

short s; // scope of short is 32,7673s= 327674; // errors= (short) 327674; // s -6

narrowing

Page 16: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

16 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Data Types from Expressions

For a binary operator• If either operand is of type String, the

other is converted to String.• Otherwise, if either operand is of type double, the other is converted to double.

• Otherwise, if either operand is of type float, the other is converted to float.

• Otherwise, if either operand is of type long, the other is converted to long.

• Otherwise, both operands are converted to type int.

Page 17: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

17 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Examples

String s =“Hello” + 5+6; // s= “Hello56”String s =“Hello” +(5+6); // s= “Hello11”String s =“Hello” +5*6; // s= “Hello30”String s =“Hello” +(5.0+6); //s= “Hello11.0”double x = 1.0/3; // x=0.3333333333333333double x = 1.0F/3; // x=0.3333333432674408double x = 1.0F/3D; //x=0.3333333333333333float x =8.0/5; // error

Page 18: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

18 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Examples

double x = 3/4*4/3; // x=0.0 double x = 3.0/4*4/3; // x=1.0double x = 3.0/4*(4/3); // x=0.75double x = 3/4*4/3.0; // x=0.0

Page 19: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

19 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Limitation on Floating Point Computation

• There is an imprecision of representing floating point values with binary representation. Thus, some decimal points cannot retain their exact decimal values when represented with binary representation.

• Floating point values should not be compared directly with relational equality operators (== and !=). A good practice for comparing a floating point value A and a floating point value B is to compute d = ||A|-|B||, and see whether d is small enough. if so, you could consider that A equals to B.

Page 20: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

20 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example public class LimitedPrecision1 {

public static void main(String[] args) {double d = 0.0;double increment = 0.1;System.out.println("Original d \t\t="+d);d += increment;System.out.println("d + 1 increments \t="+d);d += increment;System.out.println("d + 2 increments \t="+d);d += increment;System.out.println("d + 3 increments \t="+d);d += increment;System.out.println("d + 4 increments \t="+d);

Page 21: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

21 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

d += increment;System.out.println("d + 5 increments \t="+d);

d += increment;System.out.println("d + 6 increments \t="+d);d += increment;System.out.println("d + 7 increments \t="+d);

d += increment;System.out.println("d + 8 increments \t="+d);d += increment;System.out.println("d + 9 increments \t="+d);d += increment;System.out.print("d + 10 increments \t="+d);

}}

Page 22: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

22 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Page 23: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

23 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

d = ||a|-|b||

……double a,b,d;……d=Math.abs(Math.abs(a)-Math.abs(b));d /= Math.max(Math.abs(a),Math.abs(b))*100;

Make d to be percentage of max. value betweena and b.

Page 24: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

24 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Overflow and Underflow

• If a value is larger than the biggest value that a data type can handle, it is said that an overflow has occurred.

• If a value is smaller than the smallest value that a data type can handle, that value might be rounded to 0. This situation is called underflow.

• Overflow and underflow might cause some arithmetic computations to yield unexpected results.

Page 25: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

25 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example: Integer Overflowpublic class Overflow1 {

public static void main(String[] args) {int veryBigInteger = 2147483647;System.out.println("veryBigInteger = "

+veryBigInteger);System.out.println("veryBigInteger+1 = "

+(veryBigInteger+1));int verySmallInteger = -2147483648;System.out.println("verySmallInteger = "

+verySmallInteger);System.out.println("verySmallInteger-1 = "

+(verySmallInteger-1));System.out.println("veryBigInteger*2 = "

+(veryBigInteger*2)); }}

Biggest Value for long

Smallest Value for long

Page 26: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

26 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Overflow

Page 27: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

27 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example: Floating Point Overflow

public class Overflow2{public static void main(String[] args) {

double d = 1.0e308;double dSquare = d*d;

System.out.println("d = "+d); System.out.println("d^2 = "+dSquare);System.out.println("-d^2 = "+(-

dSquare));System.out.println("sqrt(d^2) = "

+Math.sqrt(dSquare));} }

Page 28: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

28 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Overflow

Page 29: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

29 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example: Floating Underflow

public class Underflow1 {public static void main(String[] args) {

double d = 1.0e-323;double p = d/10*10;double g = 10*d/10;System.out.println("d = "+d);System.out.println("d/10*10 = "+p);System.out.println("10*d/10 = "+g);

}}

Page 30: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

30 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Underflow

No Underflow

Different Results?

Page 31: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

31 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

p = d/10*10;

1.0e-323

1.0e-324

0 *10

0

g = 10*d/10;

1.0e-323

1.0e-322 /10

1.0e-323

Page 32: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

32 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Divide by Zero

• When an integer value is divided by zero, mathematically it will results in an infinite value. In Java, there is no such integer value. The program will give out exception (error).

• When a floating point value, apart from 0.0, is divided by 0, the resulting value is Infinity.

• When 0.0 is divided by 0, the resulting value is NaN ( Not a Number).

Page 33: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

33 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example: Integer Divide by Zero

public class DivBy0DemoInt {public static void main(String[] args) {

int zero = 0, i = 100;System.out.println(i/zero);

} }

Page 34: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

34 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Example : Floating Point Divide by Zero

public class DivBy0DemoDouble {public static void main(String[] args) {

double zero = 0, i = 100;System.out.println("100/0 ="+i/zero);System.out.println("0/0 ="+(0/zero));

} }

Page 35: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

35 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

i/zero

0/zero

Page 36: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

36 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Infinity and Nan

• Infinity and NaN are results of floating point expression.

• Infinity is the biggest floating point number.• -Infinity is the smallest floating point

number.• NaN uses to represent a value such as 0.0/0

or -1.• Expression that have Infinity (or –Infinity)

will be evaluated to Infinity or NaN.• Expression that have NaN will be evaluated

to NaN.

Page 37: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

37 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Increment and Decrement Operator

• The increment operator (++) add 1 to the value of the variable to which it is applied.

• The decrement operator (--) subtract 1 from the value of the variable to which it is applied.

• Each operator effect the value inside variable to which it is applied.

• Precedence=13 (same as unary operator).

++K; K++; K = K+1;--K; K--; K = K-1;

Page 38: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

38 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Prefix and PostFix

• A prefix version is when an increment (or decrement) operator is applied prior to the variable (++k).

• A postfix version is when an increment (or decrement) operator is applied after the variable (k++).

• Both versions increment (or decrement) the values inside the variables by 1.

Page 39: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

39 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

Prefix and Postfix in Expression

• Prefix version: the value inside the variable is incremented (or decremented) and then use to evaluate the expression.

• Postfix version: the value inside the variable is used to evaluate the expression and then the value is incremented (or decremented).

Page 40: Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.

40 2140101 Computer Programming for International EngineersINTERNATIONAL SCHOOL OF ENGINEERING

CHULALONGKORN UNIVERSITY

Department of Computer Engineering

1

Example

int a,b,c=0;a b c

a = c++;b = ++a;

0

10

1 1

1 1 2c++;2 4 3b = ++c+a++;

a = --b+c++; 6 3 4

Can be usedwith floating pointdata type