java程序设计基础篇 复习笔记 第二单元
1
原始数据类型(primitive data type) == 基本类型 (fundamental type)
byte short int long float double char boolean
引用类型 reference type
2
System.in System.out
java.util.Scanner
Scanner input = new Scanner (System.in);
nextByte()
nextShort()
nextInt()
nextLong()
nextFloat()
nextDouble()
next()
nextLine()
3
标识符
标识符必须是字母数字下划线和$组成的字符序列,习惯上,$只用在机器自动产生的源代码中
不能以数字开头,
不能是保留字,
不可以是true,false,null,
可以为任意长度
区分大小写
4
定名常量 named constant
final datatype CONSTANTNAME = value;
必须在同一条语句中声明和赋值
5
overflow: int value = 2147483647 + 1; int value2 = -2147483648 - 1;
java不会报关于上溢的警告或错误
underflow: java近似认为是0
6
%: 只有当被除数是负数时,余数是负数
7
数值直接量 literal
整型直接量
long 加L或l
浮点型直接量
double 加D或d
float F或f
科学计数法 e或E 12.3e+2 == 12.3e2
8
long System.currentTimeMillis() 返回格林尼治时间GMT 1,1,1970 0:00(UNIX时间戳)到当前时间的毫秒数
9
简介赋值运算符
10
数值类型转换
拓宽类型 widening a type
缩窄类型 narrowing a type
11
Math.pow(base,times)
12
encoding
encoding scheme
char 16
整型转换成char型发生截断
byte需显式转换
Unicode
Unicode Consortium
supplementary character
\u0000-\uFFFF,必须提供4个16位进制数位
如果没有安装中文字体看不到中文字符
13
Escape character 转义字符
14
不要在nextByte(),nextint()等后面使用nextLine()
15
java文档注释
/**开始 */结尾
二元运算符两边应各加一个空格
16
块的风格:
次行 next-line
行尾 end-of-line
17
java 不能从编译错误检测到除0
调试工具
18
javax.swing.JOptionPane.showInputDialog(null,title,message,ircon);
19
int intValue=Integer.parseInt(intString);
输入的如果不是数值或者点击了Cancel都会出现runtime error
Integer类和Double类都包含在java.lang包中
20
'a'-'c'=-2
显示int值
21
java -cp exercise8e.zip Exercise2_1
22
java.util.Scanner
Keyword:
algorithm
assignment operator
assignment statement
backslash(\)
byte type
casting
char type
constant
data type
debugger
debugging
declaration
decrement operator
double type
encoding
final
float type
floating-point number
expression
identifier
increment operator
incremental developing and testing
indentation
int type
literal
logic error
long type
narrowing
operator
overflow
pseudocode
primitive data type
runtime error
syntax error
supplementary Unicode
short type
underflow
Unicode
UNIX epoch
variable
widening
whitespace
习题
2.1
合法:applet,Applet,$4,apps,x,y,radius
关键字:class,public,int
2.2
double miles=100;
final double KILOMETERS_PER_MILE=1.09
double kilometers=miles*KILOMETERS_PER_MILE;
System.out.println(kilometers);
109
2.3
增加可读性,防止改变常量值,易于维护
final int SIZE=20;
2.4
5
15
50
1
6.5
-4.5
2.5
2
-2
-1
4
0
1
2.6
thursday
2.7
byte -128 - 127 8
short -32768 - 32767 16
int -2147483648 - 2147483647 32
long -9,223,372,036,854,775,808 - 92233720369547758807
float +-(1.4E-45 - 3.4028235E+38) 32
double +-(4.9E-324 - 1.797 693 134 862 315 7E+308)
2.8
6
25.0/4
2.9
从计算机的角度都正确
2.10
4/3.0/(r+34)-9*(a+bc)+(double)(3+d*(2+a))/(a+b*d)
2.11
(double)(m*r*r)
m*Math.pow(r,2);
2.12
(2)(3)
2.13
12.3,12.3e+2,23.4e-2,-334.4,20,39F,40D
2.14
2:public static void
4:int k = 100;
7:and 改为 +
2.15
int currentMinute = (int)(System.currentMillis()%36000000/60000*60)
2.16
可以发生自动转换时可以,但是float和int,long和double运算时则需要显式转换
2.17
发生截断 truncation
改变
2.18
12.5
12
2.19
49
65
66
97
98
(
;
O
U
Z
@
Z
q
r
z
2.20
'1','\u3fFa','\b'
2.21
\\
\"
2.23
char c = 'A';
i = (int)c;//i=67
float f = 1000.34f;
int i = (int)f;//变量重复声明,1000
double d = 1000.34;
int i = (int)d;//1000
int i = 97;
char c = (char)i;//'c'
2.24
'b'
'c'
-2
2.25
11
11
111
12
111
2.26
1Welcome11
1Welcome2
1Welcome2
1Welcomea1
2.27
常量: MAX_VALUE
类或接口: Test
常量或方法名: read,readInt
2.28
.............
2.29
语法错误: 语法不合规范,编译器报错
运行错误: 运行过程中报出的错误,影响程序正常运行
逻辑错误: 编程内容出现错误,指令不能达到应有效果
2.30
JOptionPane 是 javax.swing包里的,不是默认导入的
Math类则是java.lang包内的,默认导入
2.31
int i = Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"请输入一个整数"));
编程练习:
2.9
public class Exercise2-9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String money = javax.swing.JOptionPane.showInputDialog(null,"请输入钱数");
int ind=money.indexOf('.');
String penny = money.substring(ind+1);
money=money.substring(0, ind);
int i=Integer.parseUnsignedInt(money,10)*100+Integer.parseUnsignedInt(penny);
javax.swing.JOptionPane.showMessageDialog(null,"answer"+i+"penny","result",JOptionPane.INFORMATION_MESSAGE);
}
}
2.18
public class Exercise {
public static void print(String a){
System.out.printf("%-10s",a);
}
public static void main(String[] args){
print("a");print("b");print("pow(a,b)");System.out.println();
for(int i=1;i<6;i++){
String s = ""+i;
print(s);
s = String.valueOf(i+1);
print(s);
s = ""+(int)Math.pow(i, i+1);
print(s);System.out.println();
}
}
}
2.25
import java.util.Scanner;
public class Exercise {
public static void main(String[] args){
System.out.print("Enter the time zone offset to GMT:");
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
long second=System.currentTimeMillis()/1000-3600*i;
int sec = (int)(second%60);
second/=60;
int min = (int)second%60;
second/=60;
int hour = (int)second%24;
System.out.print("The current time is "+hour+":"+min+":"+sec);
}
}
java程序设计基础篇 复习笔记 第二单元的更多相关文章
- java程序设计基础篇 复习笔记 第一单元
java语言程序设计基础篇笔记1. 几种有名的语言COBOL:商业应用FORTRAN:数学运算BASIC:易学易用Visual Basic,Delphi:图形用户界面C:汇编语言的强大功能和易学性,可 ...
- java程序设计基础篇 复习笔记 第七单元&&第八单元
7.1 int[][] triArray{ {1}, {1,2}, {1,2,3}, }; 7.2 array[2].length 8.1 Unified Modeling Language:UML ...
- java程序设计基础篇 复习笔记 第六单元
第六章 一维数组 1 数组初始化语法 array initializer 2 for each loop 3 off-by-one error 通常是在循环中该使用<的地方使用了<= 4 ...
- java程序设计基础篇 复习笔记 第五单元
1. method header: modifier, return value type, method signature(method name, parameter) method body ...
- java程序设计基础篇 复习笔记 第三单元
1 单向if语句 双向if语句 dangling else switch:char,byte,short,int 2 javax.swing.JOptionPane.showConfirmDialog ...
- java程序设计基础篇 复习笔记 第四单元
1 think before coding code incrementally 2 sentinel value sentinel-controlled loop 3 输入输出重定向 > &l ...
- Java程序设计基础作业目录(作业笔记)
持续更新中............. Java程序设计基础笔记 • [目录] 我的大学笔记>>> 第1章 初识Java>>> 1.1.4 学生成绩等级流程图练习 1 ...
- Java程序设计基础笔记 • 【目录】
持续更新中- 我的大学笔记>>> 章节 内容 实践练习 Java程序设计基础作业目录(作业笔记) 第1章 Java程序设计基础笔记 • [第1章 初识Java] 第2章 Java程序 ...
- 20145322第九周JAVA程序设计基础学习总结
20145322第九周JAVA程序设计基础学习总结 JDBC简介 JDBC全名Java DataBase Connectivity,是java联机数据库的标准规范.它定义一组标准类与接口,应用程序需要 ...
随机推荐
- 46. Permutations (全排列)
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- hadoop 一些命令
关闭访问墙 service iptables stop hadoop dfs -mkdir input hadoop dfs -copyFromLocal conf/* input hadoop j ...
- 对于JVM中方法区,永久代,元空间以及字符串常量池的迁移和string.intern方法
在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法信息(如字节码,栈和变量大小),运行时常量池,已确定的符号引用和虚方法表. 在过去(当自定义类加载器使用不普 ...
- 从toString()方法到Object.prototype.toString.call()方法
一.toString方法和Object.prototype.toSting.call()的区别 var arr=[1,2]; 直接对一个数组调用toString()方法, console.log(ar ...
- Session管理之超时设置和强制下线
关于Session,在Java Web开发中,为我们提供了很多方便,Session是由浏览器和服务器之间维护的.好吧,闲话不多说,下面让我们一步一步来实现它们. (一)首先来说下Session超时时间 ...
- C++开学第一次作业(5.4)
开学第一次作业(5.4) 代码传送门 题目 Create a program that asks for the radius of a circle and prints the area of t ...
- iOS开发进阶 - 基于PhotoKit的图片选择器
移动端访问不佳,请访问我的个人博客 很早之前就用OC把代码写完了并用在项目中了,一直没时间整理,现在用swift重写一份,并且更加详细的来了解这个Photos框架,下面是我集合苹果官方文档和其他大神的 ...
- Android Device Monitor 文件管理使用的常见问题
本文参照博客:http://blog.csdn.net/aurorayqz/article/details/65705300.以下是我的实际操作. 1.使用Android Device Monitor ...
- [ACM]51nod 贪心专题
目录 A 低买高卖 C 接水问题 D做任务一 E做任务三 51nod一个贪心专题,大多数都是见过的套路,做题找找感觉,有些题解思路懒得写了,直接贴毕姥爷的直播题解了 A 低买高卖 考虑股票市场,一共有 ...
- caffe2+cuda+Ubuntu16.04(u盘安装)
安装caffe2 预先准备.安装gflags及autoconf及GLOG https://github.com/caffe2/caffe2/issues/1810 一.下载源代码通过网盘 https: ...