TR-Java-lecture04-datatype(数据类型).ppt
《TR-Java-lecture04-datatype(数据类型).ppt》由会员分享,可在线阅读,更多相关《TR-Java-lecture04-datatype(数据类型).ppt(44页珍藏版)》请在三一文库上搜索。
1、Java程序设计,Java Programming Spring, 2009,Chapter 4 Java Basic Grammar,2/44,Contents (Chapter 6 in Textbook),Data Types(数据类型) Operators(运算符) Expressions(表达式) Arrays(数组) 包装器类 字符串类 名字含义 控制流语句,Chapter 4 Java Basic Grammar,3/44,Data in Java,Java is a “typed” language: Every data value must have a type The
2、type gives rules for what values are allowed, and what operations can be done with the values. For the computer, values of a certain type usually take a known amount of storage space in memory,Chapter 4 Java Basic Grammar,4/44,Data in Java,Primitive types(基本数据类型) A primitive type represents a single
3、 value, which cannot be decomposed. Reference types(引用数据类型) The reference types are class types, interface types and array types. Variables of these types can refer to objects of the corresponding type.,Chapter 4 Java Basic Grammar,5/44,Primitive Data Type,Java has a number of pre-defined primitive
4、data types. boolean Boolean (logical) values, either true or false char single character byte 8-bit integers (signed) short 16-bit integers (signed) int 32-bit integers (signed) (e.g. 3) long 64-bit integers (signed) float 32-bit floating point double 64-bit floating point (e.g. 3.0),Chapter 4 Java
5、Basic Grammar,6/44,The Boolean Type,A Boolean variable is one which can have only 2 possible values: true or false. Boolean variables are used when you need to do logical tests Example: “Is X greater than 3?” x 3,Chapter 4 Java Basic Grammar,7/44,Type int,A variable of type int may take values from
6、2147483648 to 2147483647. Exceeding the range of legal values results in OVERFLOW, a run-time error. Base of the number: octal(8进制) a leading 0(zero) hexadecimal(16进制) a leading 0x or 0X decimal(10进制),Chapter 4 Java Basic Grammar,8/44,Type long(长整数),A variable of type long may take values from 92233
7、72036854775808L to 9223372036854775807L. To indicate the difference between an int constant and a long constant, the character L/l is appended. Example 2 is of type int 2L is of type long,Chapter 4 Java Basic Grammar,9/44,Type float(长整数),float :4个字节,32位 从1.40129846432481707e-45到 3.40282346638528860e
8、+38 (正值或负值的绝对值) 数字后必须用f指明,否则属于double型。,Chapter 4 Java Basic Grammar,10/44,Type double (Literals,直接量),Type double represents “real” numbers approximately from -1.7 10308 to 1.7 10308 with 15 accurate significant digits(有效数字). Format of large or small values: 12345600000000000.0 = 0.123456 1017 is 0.1
9、23456e17 0.00000123456 = 0.123456 10-5 is 0.123456e-5 If the value of e is more negative than about -308, the result is UNDERFLOW, and the value will be replaced by zero. This happens quietly, and is not a run-time error.,Chapter 4 Java Basic Grammar,11/44,Type char(字符),Characters are individual sym
10、bols, enclosed in single quotes(单括号) Examples: letters of the alphabet (upper and lower case are distinct) A, a punctuation symbol (e.g. comma, period, question mark) single blank space parentheses (,); brackets ,; braces , single digit (0, 1, 9) special characters such as , $, *, and so on.,Chapter
11、 4 Java Basic Grammar,12/44,Unicode,字母:Unicode字符集中的任意一个都是字母。例: 版权 “ 双引号 分式1/2 大写希腊字母delta 斜杠穿过字母o的符号,Chapter 4 Java Basic Grammar,13/44,Special characters(转义字符),Some characters are treated specially because they cannot be typed easily, or have other interpretations in Java. new-line character n tab
12、character t single quote character double quote character “ backslash character The backslash is used as an escape character(转义符): it signifies that the next character is not to have its “usual” meaning.,Chapter 4 Java Basic Grammar,14/44,转义字符:,b (退格)、 f (换页)、 n (回车)、 r (换行)、 t (水平制表符(到下一个tab位置)、 (单
13、引号)、 “ (双引号) (反斜杠),Chapter 4 Java Basic Grammar,15/44,Variables(变量),To store a value, we use a variable a name referring to the location in which the value is stored. Fields Local Variables (局部变量) can be declared anywhere within a block of statements; no default initialization value. Parameter Varia
14、bles(传递参数变量) are declared in methods, constructors, or catch blocks; no explicit(清楚的) initializers. For local variables & parameter variables, the only modifier is final.,Chapter 4 Java Basic Grammar,16/44,Variables,Variables must be declared before they can be used. A variable declaration has three
15、 parts: The type of the variable The name of the variable (Optional) Assign an initial value to the variable. Example: int x = 3;,Chapter 4 Java Basic Grammar,17/44,Assigning Values to Variables,The equals sign = is used to represent assigning a value to a variable. General form: = ; Example: assume
16、 x and y are declared previously x = 3 + y;,Chapter 4 Java Basic Grammar,18/44,final Variable (Java常量),为了修改方便,在程序中常把不允许修改的量用一个标识符表示,该标识符称为符号常量,又叫常量符号。 Java中一般只将类的成员定义为常量,称为类常量,而不在方法中定义常量,定义符号常量的同时必须赋值。具体格式为: final = 常量名一般大写。如: final double PI=3.14; final int N=100;/数组长度。,Chapter 4 Java Basic Grammar
17、,19/44,Operators(操作符),Arithmetic(算术) Operations + addition - subtraction or negation * multiplication / division % remainder,Chapter 4 Java Basic Grammar,20/44,General Operators,Increment & Decrement Operators自增/自减运算符 - Relational & Equality Operators关系运算符 greater than = greater than or equal to les
18、s than = less than or equal to = equal to != not equal to,Chapter 4 Java Basic Grammar,21/44,General Operators,Logical Operators逻辑运算符 ! logical negation & conditional AND | conditional OR,Chapter 4 Java Basic Grammar,22/44,逻辑运算符,三个逻辑运算符: ! (逻辑非),单目运算符, 即只有一个操作数 & (逻辑与) | (条件或) &和|的短路规则 如果从第一个操作数就已经可
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TR Java lecture04 datatype 数据类型
链接地址:https://www.31doc.com/p-2127766.html