Download presentation
Presentation is loading. Please wait.
Published bySteven Garrison Modified over 9 years ago
1
Department of Computer Engineering 1 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY More on Data Types
2
Department of Computer Engineering 2 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
3
Department of Computer Engineering 3 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Precedence and Associativity OperatorPrecedenceAssociativity 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
4
Department of Computer Engineering 4 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Compound Assignment For these operators: +,-,*,/,% Assignment Statement of this form : variable = variable operator expression can be written as variable operator= expression Example: 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
5
Department of Computer Engineering 5 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Caution! Be careful with complex expressions x /= y + 5; ≠ x = x / y + 5; x /= y + 5; x = x / (y+5);
6
Department of Computer Engineering 6 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Assignment Operator Associativity of assignment operator is RIGHT. Which means : int x, y; x = y= 10; // y 10 x 10 x = 5*(y = 6); // y 6 x 30 x = 5*y = 6; // ERROR
7
Department of Computer Engineering 7 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
8
Department of Computer Engineering 8 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Examples 10 int 10 l or 10 L long 10.0 double 10.0 f or 10.0 F float 10.0 d or 10.0 D double
9
Department of Computer Engineering 9 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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;
10
Department of Computer Engineering 10 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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;
11
Department of Computer Engineering 11 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
12
Department of Computer Engineering 12 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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); }
13
Department of Computer Engineering 13 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY i = (int)d1+(int)8.735f; 8 0.5 0 j = (int)(d1+d2); 1.0 0.5 1
14
Department of Computer Engineering 14 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Converting Primitive Data Types from\to byteshortintlongfloatdoublecharboolean byteAAAAACX shortCAAAACX intCCAA*ACX longCCCA* CX floatCCCCACX doubleCCCCCCX charCCAAAAX booleanXXXXXXX * indicates that precision lost might occur from the conversion. A automatic; C casting; X cannot convert
15
Department of Computer Engineering 15 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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,7673 s= 327674; // error s= (short) 327674; // s -6 narrowing
16
Department of Computer Engineering 16 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
17
Department of Computer Engineering 17 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.3333333333333333 double x = 1.0F/3; // x=0.3333333432674408 double x = 1.0F/3D; //x=0.3333333333333333 float x =8.0/5; // error
18
Department of Computer Engineering 18 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Examples double x = 3/4*4/3; // x=0.0 double x = 3.0/4*4/3; // x=1.0 double x = 3.0/4*(4/3); // x=0.75 double x = 3/4*4/3.0; // x=0.0
19
Department of Computer Engineering 19 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
20
Department of Computer Engineering 20 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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);
21
Department of Computer Engineering 21 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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); }}
22
Department of Computer Engineering 22 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY
23
Department of Computer Engineering 23 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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 between a and b.
24
Department of Computer Engineering 24 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
25
Department of Computer Engineering 25 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Integer Overflow public 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
26
Department of Computer Engineering 26 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Overflow
27
Department of Computer Engineering 27 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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)); }
28
Department of Computer Engineering 28 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Overflow
29
Department of Computer Engineering 29 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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); }}
30
Department of Computer Engineering 30 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Underflow No Underflow Different Results?
31
Department of Computer Engineering 31 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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
32
Department of Computer Engineering 32 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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).
33
Department of Computer Engineering 33 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Integer Divide by Zero public class DivBy0DemoInt { public static void main(String[] args) { int zero = 0, i = 100; System.out.println(i/zero); }
34
Department of Computer Engineering 34 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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)); }
35
Department of Computer Engineering 35 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY i/zero 0/zero
36
Department of Computer Engineering 36 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
37
Department of Computer Engineering 37 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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;
38
Department of Computer Engineering 38 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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.
39
Department of Computer Engineering 39 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 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).
40
Department of Computer Engineering 40 2140101 Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY 1 Example int a,b,c=0; abc a = c++; b = ++a; 0 1 0 11 11 2 c++; 243 b = ++c+a++; a = --b+c++; 634 Can be used with floating point data type
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.