版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第 2 章 基本数据类型和操作,JDK、JRE与JVM的作用与关系?,JDK: Java Development Kit Java开发工具包, 其中包含Java编译器(javac.exe)、Java运行时环境 JRE:Java Runtime Environment Java运行时环境(Java 虚拟机)+支持类库 JVM:Java Virtual Machine 负责将Java字节码翻译为本地机器可以执行二进制机器码,问题,问题,JDK、JRE与JVM的作用与关系?,学习目标,编写简单的Java程序 (2.2). 使用标识符命名变量、常量、方法和类 (2.3). Java数据类型 使用Jav
2、a运算符书写表达式 (2.7 2.10). 使用 JOptionPane 输入对话框输入 (2.14). 熟悉Java的文档管理、编程风格和命名习惯 (2.18).,编写一个简单的程序,计算的圆的面积.,ComputeArea,Run,跟踪程序执行过程,public class ComputeArea /* Main method */ public static void main(String args) double radius; double area; / Assign a radius radius = 20; / Compute area area = radius * rad
3、ius * 3.14159; / Display results System.out.println(The area for the circle of radius + radius + is + area); ,no value,radius,为 radius分配内存单元,跟踪程序执行过程,public class ComputeArea /* Main method */ public static void main(String args) double radius; double area; / Assign a radius radius = 20; / Compute a
4、rea area = radius * radius * 3.14159; / Display results System.out.println(The area for the circle of radius + radius + is + area); ,no value,radius,memory,no value,area,为 area分配内存单元,跟踪程序执行过程,public class ComputeArea /* Main method */ public static void main(String args) double radius; double area;
5、/ Assign a radius radius = 20; / Compute area area = radius * radius * 3.14159; / Display results System.out.println(The area for the circle of radius + radius + is + area); ,20,radius,no value,area,assign 20 to radius,跟踪程序执行过程,public class ComputeArea /* Main method */ public static void main(Strin
6、g args) double radius; double area; / Assign a radius radius = 20; / Compute area area = radius * radius * 3.14159; / Display results System.out.println(The area for the circle of radius + radius + is + area); ,20,radius,memory,1256.636,area,compute area and assign it to variable area,跟踪程序执行过程,publi
7、c class ComputeArea /* Main method */ public static void main(String args) double radius; double area; / Assign a radius radius = 20; / Compute area area = radius * radius * 3.14159; / Display results System.out.println(The area for the circle of radius + radius + is + area); ,20,radius,memory,1256.
8、636,area,控制台输出信息,数据类型,I)值类型(Value Type)、基本类型、原生类型 8种,II)引用类型(Reference Type)、类类型 很多,值类型,引用类型(Reference Type),java.lang.Byte java.lang.Short java.lang.Integer: int值类型的包装类 java.lang.Long:long值类型的包装类 java.lang.Float java.lang.Double java.lang.Character java.lang.Boolean java.lang.String,变量,值类型名 变量名 = 值
9、; 引用类型名 变量名 = new 构造方法();,变量名可以包含中文吗?,标识符,标识符是一个由字母、数字、下划线(_)和美元符号($)构成的字符转. 标识符必须由字母、下划线(_)或美元符号开始,不能用数字开头。 标识符不能是保留字(参见附录A)。 标识符不能是 true, false和null. 标识符可以有任何长度.,变量,/ Compute the first area radius = 1.0; area = radius * radius * 3.14159; System.out.println(The area is “ + area + for radius +radius
10、); / Compute the second area radius = 2.0; area = radius * radius * 3.14159; System.out.println(The area is “ + area + for radius +radius);,变量说明,int x; / Declare x to be an / integer variable; double radius; / Declare radius to / be a double variable; char a; / Declare a to be a / character variable
11、;,变量名使用小写字母,如果一个名字有多个词组成,每个词的第一个字母大写,赋值语句,x = 1; / Assign 1 to x; radius = 1.0; / Assign 1.0 to radius; a = A; / Assign A to a;,在第一步中说明和初始化变量,int x = 1; double d = 1.4; float f = 1.4;,Is this statement correct?,float f = 1.4f,常量,final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final i
12、nt SIZE = 3;,常量用大写字母命名,例如PI,算术运算符,+, -, *, /, and % 5 / 2结果为整型2. 5.0 / 2结果为double类型2.5 5 % 2 结果为 1 (求余数),January 1, 2005 是 Saturday, 可推算 February 1, 2005 是 Tuesday,注意,浮点型数不一定是精确存储. 例如, System.out.println(1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1); 显示 0.5000000000000001, 而不是 0.5, System.out.println(1.0 - 0.9);
13、 显示 0.09999999999999998, 而不是 0.1. 整型数据是精确存储.,数值直接量,直接量是在程序中直接出现的常量值。 int i = 34; 整型直接量在 -231 (-2147483648) 到 2311 (2147483647) 之间.long类型在其后加L. 浮点型默认为 double 类型. float类型在其后加f 或F, double 类型加 d or D. 例如: float :100.2f or 100.2F, double :100.2d or 100.2D. 科学计数法可表示为: 1.23456e+2, 或者 1.23456e2, 等价于 123.456
14、, and 1.23456e-2 等价于 0.0123456.,算术表达式,翻译成 (3+4*x)/5 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y),简捷赋值运算符,OperatorExample等价于 +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8,增量和减量运算符,Operator Name Description +var前置增量运算符 表达式执行前变量var先加1. var+后置增量运算符 表达式执行后变量var加1. -var前置减量运算符 表达式执行前
15、变量var先减1. var-后置减量运算符 表达式执行后变量var减1,赋值表达式和赋值语句,以下表达式可作语句: variable op= expression; / op 可选 +, -, *, /, or % +variable; variable+; -variable; variable-;,数值类型转换,考虑以下语句: byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2;,Byte, short, int, long, float, double,范围增加,扩宽可自动转换,缩窄需明确指明,类型转换,隐式转换 do
16、uble d = 3; (类型拓宽) 显式转换 int i = (int)3.0; (类型所在) int i = (int)3.9; (小数部分被截去) What is wrong?int x = 5 / 2.0;,字符型数据类型,char letter = A; (ASCII) char numChar = 4; (ASCII) char letter = u0041; (Unicode) char numChar = u0034; (Unicode),4位十六进制组成.,注意:字符型可作自加、自减运算 char ch = a; System.out.println(+ch);,Unicod
17、e 统一码,Unicode 占2个字节, 以 u开头的4个16进制数,范围从u0000 到 uFFFF. 共65536个字符。,Unicode u03b1 u03b2 u03b3 for three Greek letters,特殊字符的转义字符序列,Description Escape Sequence Unicode Backspace bu0008 Tab tu0009 换行 nu000A 回车 ru000D 斜杠 u005C 单引号 u0027 双引号 u0022,字符型和数值型之间的转换,int i = a; / Same as int i = (int)a; char c = 97
18、; / Same as char c = (char)97;,比较运算符,Operator Name greater than =greater than or equal to =equal to !=not equal to,布尔运算符,Operator Name !not 或者能被400整除, (Equality) int x= (a+)+ a; int a = 0; int x = +a + a;,X=1,X=2,表达式计算的规则,规则1:可能的情况下,从左向右依次计算所有的表达式; 规则2:根据运算符的优先级进行运算; 规则3:对优先级相同的相邻运算符,根据结合方向进行运算。,表达式
19、计算的规则, 根据规则,表达式 3 + 4 * 4 5 * (4 + 3) - 1 的计算如下:,String 类型,String实际上Java定义的一个类。 例如:String message = Welcome to Java;,字符串可以用“+”链接起来 / Three strings are concatenated String message = Welcome + to + Java; / String Chapter is concatenated with number 2 String s = Chapter + 2; / s becomes Chapter2 / Stri
20、ng Supplement is concatenated with character B String s1 = Supplement + B; / s becomes SupplementB,字符串转为数值,int intValue = Integer.parseInt(intString); intString 是一个数值型字符串,例如 “123”. double doubleValue =Double.parseDouble(doubleString); doubleString是一个数值型字符串,例如 “123.45”.,String s = String.valueOf( val
21、ue); 其中 value 为任意一种数字类型。,从输入对话框获取输入,String string = JOptionPane.showInputDialog( null, “Prompting Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);,两种调用方法,第一种: String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); 第一个参数总是null, x 表示显示内容的字符串, y 表示对话框的标题,第四个参数是对话
22、框显示的图标. 第二种: JOptionPane.showMessageDialog(x); x 表示显示内容的字符串,Example 2.3 计算贷款支付额,ComputeLoan,Run,输入年利率、年数和贷款总额 显示月支付额和总支付额,Example 2.4 整钱兑零,输入总钱数,然后兑换成 dollars(1元), quarters(2角5分), dimes(1角), nickels(5分), and pennies(1分).,ComputeChange,Run,程序跟踪,int remainingAmount = (int)(amount * 100); / Find the nu
23、mber of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / Find the number of dimes in the remaining amount i
24、nt numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = remaining
25、Amount;,1156,remainingAmount,remainingAmount initialized,Suppose amount is 11.56,程序跟踪,int remainingAmount = (int)(amount * 100); / Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarters in the remaining am
26、ount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount int numberOfNickels = re
27、mainingAmount / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;,1156,remainingAmount,Suppose amount is 11.56,11,numberOfOneDollars,numberOfOneDollars assigned,程序跟踪,int remainingAmount = (int)(amount * 100); / Find
28、the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / Find the number of dimes in the remaining am
29、ount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = rem
30、ainingAmount;,56,remainingAmount,Suppose amount is 11.56,11,numberOfOneDollars,remainingAmount updated,程序跟踪,int remainingAmount = (int)(amount * 100); / Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarte
31、rs in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount in
32、t numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;,56,remainingAmount,Suppose amount is 11.56,11,numberOfOneDollars,2,numberOfOneQuarters,numberOfOneQuarters assigned,程序跟踪,int rem
33、ainingAmount = (int)(amount * 100); / Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / F
34、ind the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;,6,remainingAmount,Suppose amount is 11.56,11,numberOfOneDollars,2,numberOfQuarters,remainingAmount updated,格式化输出,JDK 1.5
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 未来五年甲羟孕酮市场需求变化趋势与商业创新机遇分析研究报告
- 未来五年新形势下桑拿设备行业顺势崛起战略制定与实施分析研究报告
- 未来五年体育项目用网(兜)市场需求变化趋势与商业创新机遇分析研究报告
- 2026广东佛山市顺德区大良外国语学校招聘校医1人备考题库【黄金题型】附答案详解
- 中移动金融科技有限公司2026春季园招聘备考题库及参考答案详解【培优a卷】
- 2026上半年四川事业单位统考遂宁市考试招聘174人备考题库及完整答案详解(必刷)
- 2026云南省房物业管理有限公司招聘12人备考题库附完整答案详解【典优】
- 2026湖北长江产融资本投资有限公司招聘5人备考题库含答案详解(综合题)
- 2026江苏南通市工会社会工作者招聘21人备考题库【有一套】附答案详解
- 2026山东青岛澳西智能科技有限公司招聘2人备考题库有答案详解
- 门球培训教学课件
- YB-T6332-2024《钢铁行业用塑烧板除尘器》
- 平安测评IQ测试题30道及答案
- (完整版)2026年劳动法实施细则全文
- 7.4 长江经济带的协同发展 课件 2025-2026学年湘教版地理八年级下册
- 团县委保密工作制度规范
- 2026 二年级家长会 教学课件
- csco结直肠癌诊疗指南(2025版)
- 血管造影课件
- 水泥混凝土搅拌站维修手册
- 新车发布活动策划方案(3篇)
评论
0/150
提交评论