Java
学习笔记
Java 编程思想
Everything is an object
reference
where storage lives?
registers. inside the processor
the stack. has direct support from the processor via its stack pointer.—RAM
reference exists on the stack. objects are not placed on the stack.
the heap. flexibility,take more time.
constant storage. ROM
Non-RAM storage. lightweight persistence.
primitive types
new objects on the heap. primitive types on the stack.
size invariance
all numeric types are signed, so don’t look for unsigned types.
wrapper classes(包装类) make a non_primitive object on the heap. Java SE5 autoboxing.
high-precision(高精度) numbers: BigInteger BigDecimal
arrays in java
reference isn’t pointing to an object.----null
you never need to destroy an object
confusion over variable lifetimes?
scoping 代码块
the reference vanishes at the end of the scope. However ,the object is still occupying memory.
what keeps them from filling up memory and halting your program?
garbage collector
it release the memory for those objects that there’s no reference.
creating new data types: class
fields and methods.
default values 默认数值 only used as a member of a class.
returnType methodName(/*argument list*/){
/*method body*/
}
signature of the method: name and argument list.
name visibility
clashing
namesapces
domain name----net.mindview.utility.foinles
static key word
using the class name is the preferred way to refer to a static variable.
your first java program
public static void main(String [] args){}
the name of the class name is the same as the file name .
the argument to main() is an array of String objects. the args won’t be used in this program, but the java complier insists that they be there because they hold the arguments from the command line.
compiling and running
javac HelloDate.java
java HelloDate
comments
javadoc
doc tags ’@' embed(嵌入) HTML
/**……*/
for only public and protected members.
@see
{@link package.class#member label}
{@inherit Doc}
@version
@author
@since
@param
@return
@throws
@deprecated
coding style
camel-casing
operators
simpler print statements
the static import
assignment(赋值)
from one object to another. copy a reference from one place to another.
aliasing
aliasing during method calls
mathematic operators
create a Random object with no arguments. java use the current time as a seed for the random number generator.
call the methods nextInt(). set the upper bound. the lower bound is zero.
auto increment and decrement(自增自减)
relational operators
object equivalence. equals(). the default behavior of equals() is to compare references. override
logic operators
&& || !
short-circuiting(惰性)
a&&b&&c
literals
0x L D F
Exponential notation
1.39e-43f
bitwise operators
& | not ^(xor)
shift operators
<<< >>> unsigned. zero extension
<< >> signed sign extension
ternary if-else operator
boolean-exp ? value0 : value1
string operator + and +=
if an expression begins with a string, then all operands that follows must be strings. convert
common pitfalls
while(x=y),in java, the complier won’t convert to a boolean, so a compile-time error.
casting operators(类型转换)
warning:this can be dangerous thing to do-if you want me to do it anyway you must make the cast explicit.
java allows you to cast any primitive type, except for boolean, which doesn’t allow any casting at all.
truncation and rounding
java.lang.Math Math.round()(四舍五入)
一般丢掉小数部分
java has no “sizeof”
all the data types are the same size on all machines.
Controlling Execution
java don’t allow you use a number as a boolean.
if-else
if(boolean-expression)
statement
else
statement
iteration(迭代)
do
statement
while(boolean-expression);
for(initialization ; boolean-expression ; step)
statement
the comma operator
those statements will be evaluated sequentially.
foreach syntax
for use with arrays and containers
you can easily iterate through the characters in a string.
break and continue
break quits the loop without executing the rest of the statements in the loop. continue stops the execution of the current iteration and goes back to begin the next iteration.
the infamous “goto”
label
switch
selection statement. enums are designed to work nicely with switch.
Initialization and cleanup
chapter 1
1.1 Java程序设计平台
多种优势
1.2 Java"白皮书"的关键术语
简单性. 面向对象. 分布式. 健壮性. 安全性. 体系结构中立. 可移植性. 解释型. 高性能. 多线程. 动态性
1.3 Java applet 与 Internet
chapter 2
JDK: Java Development Kit JRE: Java Runtime Environment SE: Standard Edition EE: Enterprise Edition ME: Micro Edition
SDK: Software Development Kit
Chapter 3 Java 的基本程序设计结构
3.1 一个简单的Java应用程序
类名以大写字母开头的名词
源代码的文件名==公共类的类名
3.2 注释
/** ...... **/自动生成文档
3.3 数据类型
整型 浮点类型 char Unicode boolean
3.4 变量
初始化<可与声明同时进行>
int i=10;
常量 final 指示 final double a =3.14;
3.5 运算符 NaN
数值之间的类型转换
double x=9.97;
int nx =(int)x;
位运算符 and & or | not ~ xor ^ << >> <<< >>>
枚举类型
3.6 字符串
子串 substring
String greeting =”Hello”;
String s=greeting.substring(0,3);
“Hel”
不可变字符: 编译器可以让字符串共享
3.6.4 检测字符串是否相等
Hello.equals(greeting);
3.7 输入输出
3.7.1 输入
读取输入----要想通过控制台进行输入,首先需要构造一个Scanner对象,并与标准输入流System.in关联
String name = in .nextLine();
要想读取一个单词用 String firstName=in.next();
整数 int age =in.nextInt(); Double型: nextDouble();
Scanner类定义在java.util包中,不存在java.lang包中
注释: 因为输入是可见的,所以Scanner类不适用于从控制台读取密码. Java SE 6特别引入了 Console类.
Console cons =System.console();
String name =cons.readLine(”Username: ”);
char [] password=cons.readPassword(“Password: ”)
为了安全起见,返回的密码存放在一维数组中,而不是字符串中.在对密码进行处理之后, 应该马上用一个填充值覆盖元素.
3.7.2格式化输出 沿用C的模式
3.7.3 文件输入与输出
读取文件 Scanner in =new Scanner(Path.get(“myfile.txt”), “UTF-8”);
写入文件 PrintWriter ou =new PrintWriter(“myfile.txt”, “UTF-8”); 如果文件不存在,创建.
可以使用print命令
3.8 控制流程
3.8.1 block 块作用域
块确定了变量的作用域,但是,不能在嵌套中的两个块中声明同名变量.
3.8.2
条件语句 jf(condition) statement
循环语句 while(condition) statement
确定循环 for
多重选择 switch (choice) {case 1: }
3.9 大数值


浙公网安备 33010602011771号