Chapter 2 Primitive Data Types and Operations.ppt
《Chapter 2 Primitive Data Types and Operations.ppt》由会员分享,可在线阅读,更多相关《Chapter 2 Primitive Data Types and Operations.ppt(59页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 2 Primitive Data Types and Operations,Introduce Programming with an Example Identifiers, Variables, and Constants Primitive Data Types byte, short, int, long, float, double, char, boolean Expressions Operators, Precedence, Associativity, Operand Evaluation Order: +, -, *, /, %, +=, -=, *=, /
2、=, %=, , &, |, +, -, Getting Input from Input Dialog Boxes Case Studies (Computing Mortgage, and Computing Changes) Style and Documentation Guidelines Syntax Errors, Runtime Errors, and Logic Errors,Introducing Programming with an Example,Example 2.1 Computing the Area of a CircleThis program comput
3、es the area of the circle.,ComputeArea,Run,Identifiers,An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot
4、 be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, or null. An identifier can be of any length.,Variables,/ Compute the first area radius = 1.0; area = radius*radius*3.14159; System.out.println(“The area is “ + area + “ for radi
5、us “+radius);/ Compute the second area radius = 2.0; area = radius*radius*3.14159; System.out.println(“The area is “ + area + “ for radius “+radius);,Declaring Variables,int x; / Declare x to be an/ integer variable; double radius; / Declare radius to/ be a double variable; char a; / Declare a to be
6、 a/ character variable;,Assignment Statements,x = 1; / Assign 1 to x; radius = 1.0; / Assign 1.0 to radius; a = A; / Assign A to a;,Declaring and Initializing in One Step,int x = 1; double d = 1.4; float f = 1.4;Is this statement correct?,Constants,final datatype CONSTANTNAME = VALUE; final double P
7、I = 3.14159; final int SIZE = 3;,Numerical Data Types,byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits,Operators,+, -, *, /, and %5/2 yields an integer 2. 5.0/2 yields a double value 2.55 % 2 yields 1 (the remainder of the division),NOTE,Calculations involving floating
8、-point numbers are approximated because these numbers are not stored with complete accuracy. For example, System.out.println(1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1); displays 0.5000000000000001, not 0.5, and System.out.println(1.0 - 0.9); displays 0.09999999999999998, not 0.1. Integers are stored precisely
9、. Therefore, calculations with integers yield a precise integer result.,Number Literals,A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements:int i = 34; long l = 1000000; double d = 5.0;,Integer Literals,An i
10、nteger literal can be assigned to an integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable
11、 of the byte type. An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) to 2311 (2147483647). To denote an integer literal of the long type, append it with the letter L or l. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one
12、).,Floating-Point Literals,Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. You can make a number a float by appending the letter f or F, and make a numb
13、er a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number.,Scientific Notation,Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as 1.23456e2, is equivalent to 1
14、23.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an exponent and it can be either in lowercase or uppercase.,Arithmetic Expressions,is translated to(3+4*x)/5 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y),Shortcut Assignment Operators,Operator Example Equivalent += i+=8 i = i+8 -= f-=8
15、.0 f = f-8.0 *= i*=8 i = i*8 /= i/=8 i = i/8 %= i%=8 i = i%8,Increment and Decrement Operators,Increment and Decrement Operators, cont.,Increment and Decrement Operators, cont.,Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoi
16、d using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = +i + i.,Assignment Expressions and Assignment Statements,Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expr
17、essions can be statements: variable op= expression; / Where op is +, -, *, /, or % +variable; variable+; -variable; variable-;,Numeric Type Conversion,Consider the following statements: byte i = 100; long k = i*3+4; double d = i*3.1+k/2;int x = k; /(Wrong) long k = x; /(fine,implicit casting),Type C
18、asting,double float long int short byte,Type Casting, cont.,Implicit castingdouble d = 3; (type widening)Explicit castingint i = (int)3.0; (type narrowing)What is wrong? int x = 5/2.0;,Character Data Type,char letter = A; (ASCII) char numChar = 4; (ASCII) char letter = u0041; (Unicode) char numChar
19、= u0034; (Unicode)Special characters char tab = t;,Unicode Format,Description Escape Sequence Unicode Backspace b u0008 Tab t u0009 Linefeed n u000a Carriage return r u000d,Appendix B: ASCII Character Set,ASCII Character Set is a subset of the Unicode from u0000 to u007f,ASCII Character Set, cont.,A
20、SCII Character Set is a subset of the Unicode from u0000 to u007f,Casting between char and Numeric Types,int i = a; / Same as int i = (int)a;char c = 97; / Same as char c = (char)97;,The boolean Type and Operators,boolean lightsOn = true; boolean lightsOn = false; boolean b = (1 2); & (and) (1 x) &
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER2PRIMITIVEDATATYPESANDOPERATIONSPPT
