210 likes | 226 Views
Explore the default types of floating-point number literals in Java, learn about type casting, issues with real numbers, and math operations using the Math class. Self-study examples included.
E N D
Introduction to Computing Using Java More about Primitive Types and Operators Michael Fung, CS&E, The Chinese University of HK
Default Type of Floating-point Number Literals • Floating-point Number literals are considered to be of type double by default. double d1 = 3.14159; // ok double d2 = 3e8; // ok double d3 = -0.27e-5; // ok float f1 = 3.14159; // not ok • Adding a suffix F/ f to the number changes this default: float f2 = 3.14159F; // ok float f3 = -0.27e-5f; // ok Michael Fung, CS&E, The Chinese University of HK
Type Casting • We can also apply type casting. float f3 = (float) 3.14159; // ok float f4 = (float) d1; // ok • In case of real to integer type casts, any fractional part is discarded, and results in the integral part (truncation): (int) 6.3 gives 6 (int) 6.8 gives 6 • Syntax of type casting (explicit type conversion): (<type_name>)some_value Michael Fung, CS&E, The Chinese University of HK
Type Casting Examples double UV_measurement = 6.754; int UV_level = (int) UV_measurement; short avg_height; avg_height = (short)( (178.2 + 192.7 + 180.1) / 3 ); // type-casting has higher precedence than division! Benz peter = new Benz(); Car michael = (Car) peter; // type-casting also works on class type objects // peter keeps a Benz object // we can "down-convert" it and consider it a Car Advanced example Michael Fung, CS&E, The Chinese University of HK
Type Casting Issues • Integer-to-integer type casts • byte short int long Always ok • long int short byte May not… • Overflow may occur, causing errors. • Real-to-real type casts • float double • double float • Note that real numbers are often inexact. • Always check the accuracy you needed. ? Michael Fung, CS&E, The Chinese University of HK
Issue Concerning Real Numbers • There are infinite real numbers in the world. • Real numbers do not have absolute friends • There are always 第三者in between • 1.4 1.43 1.442 1.445 1.5 • Moreover, computers use 0 and 1 (binary digits) to represent numbers and there is always a limitation in representing decimal real numbers. • You will find that 0.7 * 0.7 0.49 and(double) 3.123F gives 3.122999906539917 Michael Fung, CS&E, The Chinese University of HK
Self-study: More About Type class DataType { /* testing of primitive data types */ public static void main (String [ ] args) { byte int8 = -128; System.out.println(int8); int8 = (byte) 137; /* type casting with an overflow */ System.out.println(int8); boolean ok = (3 > 7); /* boolean expression */ System.out.println(ok); System.out.println("Hello \"World\" !!!"); double GPA = 3; System.out.println("GPA = " + GPA); System.out.println("(int)(5.23) = " + (int)(5.23)); System.out.println("Math.floor(5.23) = " + Math.floor(5.23)); System.out.println("Math.ceil(5.23) = " + Math.ceil(5.23)); System.out.println("Math.round(5.23) = " + Math.round(5.23)); System.out.println("(int)(-5.23) = " + (int)(-5.23)); System.out.println("Math.floor(-5.23) = " + Math.floor(-5.23)); System.out.println("Math.ceil(-5.23) = " + Math.ceil(-5.23)); System.out.println("Math.round(-5.23) = " + Math.round(-5.23)); } } Michael Fung, CS&E, The Chinese University of HK
Self-study: More About Type -128 -119 false Hello "World" !!! GPA = 3.0 (int)(5.23) = 5 Math.floor(5.23) = 5.0 Math.ceil(5.23) = 6.0 Math.round(5.23) = 5 (int)(-5.23) = -5 Math.floor(-5.23) = -6.0 Math.ceil(-5.23) = -5.0 Math.round(-5.23) = -5 Look up Java API class Math check these Math methods: floor( ) round( ) ceil( ) What are their functions? Do they act like a mathematical function? Michael Fung, CS&E, The Chinese University of HK
The Math Class • The class Math is provided with Java, offering various methods for simple mathematical operations. • To use such methods, we have to send message to the Math class: double answer1; answer1 = Math.sqrt(49); • A message usually generates a result (depending on the design and nature/type of the method): long answer2; answer2 = Math.round(Math.pow(2+3, 2)); Michael Fung, CS&E, The Chinese University of HK
Math PI 3.14159… sqrt pow sin round … Methods in the Math Class • PI is a class constant field pre-stored with many digits of . • Notice that sin, cos, tan, etc. uses radian instead of degree. • Don’t miss the pair of parenthesis in sending messages to the Math class! • General form of a such a message: class_name.method_name(input); Math.function(input); Michael Fung, CS&E, The Chinese University of HK
Operators • Category of operators: • Arithmetic : + – * / % -num • Relational : < > <= >= == != • Logical : ! & | && || ^ • Conditional : bool_expression ? true_case : false_case • Short-hand : ++ -- += -= *= … • Assignment : = (to store a value) Michael Fung, CS&E, The Chinese University of HK
Arithmetic Operators • Addition/ Sum : a + b • Subtraction/ Difference : a – b • Multiplication/ Product : a * b • Division/ Quotient : a / b • Remainder (of integer division) : a % b • Negation/ Minus : –number Michael Fung, CS&E, The Chinese University of HK
Real and Integer Division • System.out.println(1.0 / 2.0); 0.5 • System.out.println(1 / 2); 0 • System.out.println(1.0 / 2); 0.5 Michael Fung, CS&E, The Chinese University of HK
Real and Integer Division • System.out.println(1.0 / 2.0); 0.5 • System.out.println(1 / 2); 0 • System.out.println(1.0 / 2); 0.5 double / double Michael Fung, CS&E, The Chinese University of HK
Real and Integer Division • System.out.println(1.0 / 2.0); 0.5 • System.out.println(1 / 2); 0 • System.out.println(1.0 / 2); 0.5 double / double int / int int Michael Fung, CS&E, The Chinese University of HK
Real and Integer Division • System.out.println(1.0 / 2.0); 0.5 • System.out.println(1 / 2); 0 • System.out.println(1.0 / 2); 0.5 double / double int / int int double / int Michael Fung, CS&E, The Chinese University of HK
Real and Integer Division • System.out.println(1.0 / 2.0); 0.5 • System.out.println(1 / 2); 0 • System.out.println(1.0 / 2); 0.5 double / double int / int int double / double Implicit Coercion Michael Fung, CS&E, The Chinese University of HK
Relational and Logical Operators(Examples will be covered in tutorial) • Relational operators • to make comparisons • result is a boolean value (true or false) • Logical/ Boolean operators • and, or, negate, exclusive-or, … • operate on boolean values • result is a boolean value Michael Fung, CS&E, The Chinese University of HK
Short-hand Operators • Increment operator (basic understanding) a++ ++a a = a + 1 • Decrement operator (basic understanding) a–– --a a = a – 1 • Short-hand assignment operators (lazy) a +=b a = a + b a -= b a = a – b a *= b a = a * b ... Michael Fung, CS&E, The Chinese University of HK
Summary • Type casting usage and examples • Floating-point number issues • Java API Math class and methods • Operators usage and examples • Conditional operator will be discussed later Michael Fung, CS&E, The Chinese University of HK
End Note • Readings and References • Section 2.1 – 2.5 • Exercise • 1.3, 1.4, 1.16, 1.17, 1.18, • 2.2, 2.3, 2.4, 2.5, 2.6, 2.7 • Programming Projects • 2.1 Michael Fung, CS&E, The Chinese University of HK