TR-Java-lecture04-datatype(数据类型).ppt

上传人:本田雅阁 文档编号:2127766 上传时间:2019-02-19 格式:PPT 页数:44 大小:445.51KB
返回 下载 相关 举报
TR-Java-lecture04-datatype(数据类型).ppt_第1页
第1页 / 共44页
TR-Java-lecture04-datatype(数据类型).ppt_第2页
第2页 / 共44页
TR-Java-lecture04-datatype(数据类型).ppt_第3页
第3页 / 共44页
亲,该文档总共44页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《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,逻辑运算符,三个逻辑运算符: ! (逻辑非),单目运算符, 即只有一个操作数 & (逻辑与) | (条件或) &和|的短路规则 如果从第一个操作数就已经可

19、以断定出表达式结果,就自动不再计算第二个操作数。,Chapter 4 Java Basic Grammar,23/44,General Operators,Bit Manipulation Operators按位逻辑运算符 binary bitwise operations & bitwise AND | bitwise inclusive OR bitwise exclusive or (XOR),Chapter 4 Java Basic Grammar,24/44,按位逻辑运算符,仅限于整数。先将整数写成二进制形式,然后按位操作,最后产生一个新的数 & 按位与 可以把某些位置0 | 按位或

20、 可以把某些位置1 按位异或 可以把某些位置反(两个二进制位不同时,XOR的结果为1) 按位非 全部取反 带符号右移 不带符号右移,Chapter 4 Java Basic Grammar,25/44,练习: (思路),输出一个整数的二进制表示形式的字符串 Integer.toBinaryString(x); 用实现对一句话的加解密,26,class Secret public static void main(String args) char a=金,木,水,火,土; char secret=z; for(int i=0;ia.length;i+) ai=(char)(aisecret);

21、 System.out.println(“密文:n“); for(int i=0;ia.length;i+) System.out.print(“ “+ai); for(int i=0;ia.length;i+) ai=(char)(aisecret); System.out.println(“n原文:n“); for(int i=0;ia.length;i+) System.out.print(“ “+ai); ,27,Chapter 4 Java Basic Grammar,28/44,对象运算符,instanceof: evaluates if a reference refers to

22、 an object that is an instance of a particular class or interface. Example: String name=“Java”; boolean isString=name instanceof String; boolean isShort=name instanceof Short; boolean isObject=name instanceof Object;,/true,/false,/true,Chapter 4 Java Basic Grammar,29/44,Conditional Operator 条件运算符,Th

23、e Conditional Operator ?: ternary (three-operand,三元) operator value = (userSetIt ? userValue : defaultValue); equivalent to: if (userSetIt) value = usersValue; else value = defaultValue;,int a=5; a3?a+5:a-5;,Chapter 4 Java Basic Grammar,30/44,字符串连接符 ,String Concatenation(连接) Operator You can use + t

24、o concatenate (连接) two strings. Example: String + String (字符串 + 字符串) String boo=“boo”; String cry=boo+“hoo”; String + Other types(字符串 + 其它类型) int no=1001; String stuNo=“cuit”+no;,/ “boohoo”,/ “cuit1001”,31,String Concatenation(连接) Operator,Strings can be CONCATENATED (joined) using the + operator: “

25、My name is“ + “Alan“ gives “My name isAlan“ String values can also be concatenated to values of other types with the + operator. “The speed is “ + 15.5 gives “The speed is 15.5“ Because one of the values for the + operator is a string , the double is temporarily converted to a string value “15.5“ be

26、fore doing the concatenation.,Chapter 4 Java Basic Grammar,32/44,General Operators,内存分配运算符 new instance creation expression; 为变量或对象以及数组动态分配内存单元。,Chapter 4 Java Basic Grammar,33/44,Precedence(优先) of Operators,Operators are evaluated left-to-right, with the following precedence (all operators on the s

27、ame line are treated equally): (Detail see Page 177) () (expression) (array subscript) . (object member) + (to indicate positive or negative values) ! (not) * / % + - (for addition or subtraction of two values, concatenation) = = = != & | = (assignment to variable),Chapter 4 Java Basic Grammar,34/44

28、,Expressions 表达式,An expression consists of operators and their operands(操作数), which are evaluated to yield(产生) a result; The result may be a variable or a value, or even void; Example: x + y + z,Chapter 4 Java Basic Grammar,35/44,Expressions,Operands to operators will be evaluated left-to-right. Exc

29、ept for the operators If evaluation of the left operand of a binary operator causes an exception, no part of the right-hand operand is evaluated. The evaluation stops as soon as an exception is encountered.,Chapter 4 Java Basic Grammar,36/44,Expression Type,Every expression has a data type. Type dec

30、ision rules are as follows: If an arithmetic or bit manipulation operator is applied to integer values, the result of the expression is of type int unless one or both sides are long, in which case the result is long. The smaller byte and short integer are always promoted to int before evaluation. If

31、 either operand of an arithmetic operator is floating point, the operation is performed in floating-point arithmetic;,Chapter 4 Java Basic Grammar,37/44,Expression Type,A + operator is a String concatenation when either operand to + is of type String or if the left-hand side of a += is a String; Whe

32、n used in an expression, a char value is converted to an int by setting the top 16 bits to zero. Example: Unicode character uffff integer 0x0000ffff,Chapter 4 Java Basic Grammar,38/44,Implicit Type Conversions (隐式类型转换),原则:系统自动进行,在计算机中占内存位(bit)数少的类型向占位数多的类型转换。 Widening conversion(扩展转换) any numeric va

33、lue can be assigned to any numeric variable whose type supports a larger range of values;,Chapter 4 Java Basic Grammar,39/44,Explicit Type Casts(显式类型转换),强制类型转换(Cast) 指从计算机中占内存位(bit)数多的类型向占位数少的类型转换。可能造成高位数据的丢失。 凡是数据类型高的转换为数据类型低的都要进行强制类型转换,转换方法如下: () ,Chapter 4 Java Basic Grammar,40/44,Explicit Type C

34、asts(显式类型转换),Example: double d=7.99; long l=(long)d; l is 8,41,数据类型隐式转换简单练习,例1: public class Autotraf public static void main(String arg) char a=d; int i=120; float f=1.2340f; double d=45.6000; long l=7844L; System.out.println(“i的值是“+i); i=a; System.out.println(“i的值是“+i); i=120; System.out.println(“

35、f的值是“+f); f=i; System.out.println(“f的值是“+f); System.out.println(“d的值是“+d); d=i; System.out.println(“d的值是“+d); System.out.println(“l的值是“+l); l=i; System.out.println(“l的值是“+l); ,42,43,数据类型显式转换简单练习,例2: public class Presstraf public static void main(String arg) char ch, chinaword=你; byte a; int b=325; d

36、ouble c=346.73; System.out.println(“汉字你在unicode表中顺序:“+(int)chinaword); System.out.println(“unicode表中325位置上字符为:“+(char)b); a=(byte) b; System.out.println(“intbyte:“+b+“+a); b=(int) c; System.out.println(“doubleint:“+c+“+b); b=(byte) c; System.out.println(“double byte:“+c+“+b); ch=(char) -70; System.out.println(“-70char:“+ch); ,44,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1