1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79
Example Given a radius 10, determine the area of the circle. Recall that a program has data and algorithms. How to store the data? Variable declaration, primitive date type, assignment operator How to compute the area? Arithmetic operators How to show the result? System.out.println() Zheng-Liang Lu Java Programming 46 / 79
Given a radius 10, determine the area of the circle. 1 public class ComputeArea { 2 public static void main (String[] args) { 3 int r = 10; 4 double area = r r 3.1415926; 5 System.out.println("The area for the circle of radius " 6 + r + " is " + area + "."); 7 } 8 } int and double are two of primitive data types. r and area are two variables. The string, which is double-quoted, can be concatenated by simply adding a plus (+) sign. 1 1 String class is very useful in Java because of its fruitful methods. Zheng-Liang Lu Java Programming 47 / 79
Variable as Container Zheng-Liang Lu Java Programming 48 / 79
Variable Declaration Variable declaration tells the compiler to allocate appropriate memory space for the variable based on its data type. Assign a memory address to hold the value Give an identifier (variable name) Refer 2 to the memory address when being fetched Determine the size, which is measured in bytes, to store the value depending on the date type For example, myvar is declared as integer type. 1... 2 int myvar; // int refers to integer type 3... 2 In compilation stage. Zheng-Liang Lu Java Programming 49 / 79
Variable in Memory 1... 2 int x; // x is declared as an int type variale 3... 0x000abc26 stands for a memory address in hexadecimal 3. x itself refers to 0x000abc26 in the program after compilation. 3 0-9, A-F. Zheng-Liang Lu Java Programming 50 / 79
Values in 2 s Complement Java uses an encoding known as 2 s complement 4, which means that negative numbers are represented by inverting 5 all of the bits in a value, then adding 1 to the result. For example, 42 10 = 00101010 2. Then 42 10 = 11010110 2 can be derived from inverting 42 10 to 11010101 2 plus 1. 4 In 1 s complement, simply inverting all of the bits creates 11111111, which creates negative zero. The trouble is that negative zero is invalid in integer math. 5 Changing 1 s to 0 s and vice versa. Zheng-Liang Lu Java Programming 51 / 79
Identifiers Identifiers are the names that identify the elements such as variables, methods, and classes in a program. All identifiers must obey the following rules: a sequence of characters that consists of letters, digits, and underscores ( ) start with a letter or an underscore ( ) can be of any length cannot start with a digit cannot be a reserved word cannot be true, false, or null cannot contain +,,, and % Note that Java is case sensitive 6. 6 A and a are different. Zheng-Liang Lu Java Programming 52 / 79
Reserved Words 7 7 See Appendix A in YDL, p. 1253. Zheng-Liang Lu Java Programming 53 / 79
When To Declare Variables: Two Before Rules Every variable has a scope. The scope of a variable is the part of the program where the variable can be referenced. A variable must be declared before it can be assigned a value. A declared variable must be assigned a value before it can be used. 8 The detail of variable scope is introduced later. 8 In symbolic programming, such as Mathematica and Maple, a variable can be manipulated without assigning a value. Zheng-Liang Lu Java Programming 54 / 79
Data Types Java is a strongly typed programming language. Every variable has a type. Every expression has a type. Every type is strictly defined. All assignments are checked for type compatibility. 9 There are two categories of date types: primitive data types, referential data types. 9 By Java compiler. Zheng-Liang Lu Java Programming 55 / 79
Primitive Data Types 10 10 See Figure 3-4 in Sharan, p. 67. Zheng-Liang Lu Java Programming 56 / 79
Integers The most commonly used integer type is int. long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. Note that when byte and short values are used in an expression, they are promoted to int when the expression is evaluated. 11 11 Due to the first bullet. Zheng-Liang Lu Java Programming 57 / 79
Floating Points Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. All transcendental math functions, such as sin(), cos(), and sqrt(), return double values. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. Be aware that floating-point arithmetic can only approximate real arithmetic. (Why?) Zheng-Liang Lu Java Programming 58 / 79
Example: 0.5-0.1-0.1-0.1-0.1-0.1 = 0? 1 public class displaytime { 2 public static void main(string args[]) { 3 System.out.println(0.5 0.1 0.1 0.1 0.1 0.1); 4 } 5 } The result is surprising. Why? Decimal-binary converter Zheng-Liang Lu Java Programming 59 / 79
IEEE Floating-Point Representation 12 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either between 1 and 2 ɛ, or between 0 and 1 ɛ. The exponent E weights the value by a (possibly negative) power of 2. 12 William Kahan (1985) Zheng-Liang Lu Java Programming 60 / 79
Illustration 13 13 See Figure 2-31 in Byrant, p. 104. Zheng-Liang Lu Java Programming 61 / 79
Assginment Statement An assignment statement designates a value for a variable, by the following syntax: 1... 2 myvar = expression; 3... The equal sign (=) is used as the assignment operator. For example, is the expression x = x + 1 correct? From the right-hand side to the left-hand side An expression represents a computation involving values, variables, and operators that evaluates to a value. To assign a value to a variable, you must place the variable name to the left of the assignment operator. 14 For example, 1 = x is wrong. 14 x can be a L-value and R-value, but 1 and other numbers can be only r-value but not l-value. See Value. Zheng-Liang Lu Java Programming 62 / 79
Variable Initialization Initialization once declaration 1... 2 int x = 1, y = 2; // Equivalent to int x, y; x = 1; y = 2; 3... Dynamical initialization 1... 2 double x, y = 3.0; 3 x = y; 4... y is assigned in the compilation stage; x is assigned in the runtime stage. Zheng-Liang Lu Java Programming 63 / 79
Numeric Operators 15 Note that when both operands of a division are integers, the result of the division is an integer and the fractional part is truncated. So, the operator depends on the operands. 15 See Table 2-3 in YDL, p. 46. Zheng-Liang Lu Java Programming 64 / 79
Example 1... 2 int a, b; 3 double c,d; 4 a = c = b = d = 3 + 7 / 2.; 5... a =? b =? c =? d =? Zheng-Liang Lu Java Programming 65 / 79
Type Conversion If the two types are compatible, then Java will perform the conversion automatically. However, not all types are compatible, and thus, not all type conversions are implicitly allowed. The two types are compatible. The destination type is larger than the source type. For example, there is no automatic conversion defined from double to byte. (Why?) To do so, you must use a cast, which performs an explicit conversion between incompatible types. Zheng-Liang Lu Java Programming 66 / 79
Casting 1 public class test { 2 public static void main (String[] args) { 3 double x = 1.234; 4 int y; 5 y = (int) x; // (target type) 6 System.out.println("y = " + y); 7 } 8 } Target-type specifies the desired type to convert the specified value to. In this example, a different type of conversion will occur when a floating-point value is assigned to an integer type, as known as, truncation. Zheng-Liang Lu Java Programming 67 / 79
Example 1 public class promote { 2 public static void main(string[] args) { 3 byte b = 42; 4 char c = a ; // single quote for a letter, a > 97 5 short s = 1024; 6 int i = 50000; 7 float f = 5.67; 8 double d =.1234; 9 double result = (f b) + (i / c) (d s); 10 System.out.println((f b) + " + " + (i / c) + " " + 11 (d s) + " = " + result); 12 } 13 } (i/c) =? How to correct? Zheng-Liang Lu Java Programming 68 / 79
Type Promotion (Concluded) Type promotion rules are as follows: All byte, short, and char values are promoted to int. If one operand is a long, the whole expression is promoted to long. Similarly, if one is float, then promoted to float. Also, if any of the operands are double, then promoted to double. Zheng-Liang Lu Java Programming 69 / 79
Characters A character is stored in a computer as a sequence of 0s and 1s. The char data type is a 16-bit unsigned Java primitive data type. Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. Zheng-Liang Lu Java Programming 70 / 79
ASCII (7-bit version) Zheng-Liang Lu Java Programming 71 / 79
Example Characters can also be used as an integer type on which you can perform arithmetic operations. For example, 1... 2 char x = a ; Cannot be "a" 3 System.out.println(x + 1); 4 System.out.println((char)(x + 1)); 5 System.out.println(x + "1"); 6 System.out.println("This is " + x + 1); 7 System.out.println("This is " + (x + 1)); 8... x =? Note that the plus (+) operator is overloaded. Zheng-Liang Lu Java Programming 72 / 79
Character Escape Sequences 16 16 See Table 3-1 in HS, p. 44. Zheng-Liang Lu Java Programming 73 / 79
Boolean Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false. boolean is also the type required by the conditional expressions that govern the control statements such as if and for. Note that a boolean value cannot be cast into a value of another type, nor can a value of another type be cast into a boolean value. Zheng-Liang Lu Java Programming 74 / 79
Rational Operators 17 They are all binary operators. Rational expressions return Boolean values. Note that the equality comparison operator is two equal signs (==), not a single equal sign (=), which is the assignment operator. 17 See Table 3-1 in YDL, p. 82. Zheng-Liang Lu Java Programming 75 / 79
Example 1... 2 a = 4 > 3; 3 b = 4 < 3; 4 c = 4 == 3; 5 d = 4!= 3; 6 e = 4 > 3 > 2; 7... a =? b =? c =? d =? e =? Be aware that e is logically correct but syntaxically wrong. Zheng-Liang Lu Java Programming 76 / 79
Logical Operators 18 Sometimes, whether a statement is executed is determined by a combination of several conditions. For example, 4 > 3 > 2 should be (4 > 3)&&(3 > 2) in the program, where && refers to the and operator. 18 See Table 3-2 in YDL, p. 102. Zheng-Liang Lu Java Programming 77 / 79
Arithmetic Compound Assignment Operators += ( =) is called the addition (subtraction) assignment operator. The increment (++) and decrement ( ) operators are for incrementing and decrementing a variable by 1. Zheng-Liang Lu Java Programming 78 / 79
Example 1... 2 int a = 1; 3 System.out.println(a+=1); // a = a + 1 4 System.out.println(a++); // a = a + 1 5 System.out.println(++a); // a = a + 1 6 System.out.println( a); // a = a 1 7 System.out.println(a ); // a = a 1 8... What are the results? Zheng-Liang Lu Java Programming 79 / 79