Initialization
初始化
* All classlevel (member) variables are initialized before they can be used
All local variables are not initialized until it is done explicitly
* 所有的主成员在他们使用之前被初始化所有的局部变量必须通过显式的赋值来初始化
* An array object (as distinct from reference) is always initialized(with zeroes or nulls)
* 数组对象总是能够初始化(零或者null)
* Member initialization with the declaration has exception problems:
cannot call methods that throw a checked exception
cannot do error recovery from runtime exceptions
If you need to deal with errors you can put the initialization code along with try/catch statements in either a ctor (for instance fields) or in a static initialization block for static fields You can also have instance (nonstatic) initialization blocks but ctors are more recognizable
* 需要处理异常的成员初始化
不能调用会抛出异常的方法
不能对基本异常做任何处理
如果你需要处理错误将初始化的代码放到构造器或者静态初始化块的 try/catch块中当然你也可以放到非静态的代码块中但是构造器似乎更为通用
Strings
字符串
* The String class
Because string is an immutable class its instance methods that look like they would transform the object they are invoked upon do not alter the object and instead return new String objects
String has methods concat(String)trim()replace(charchar)
String has static valueOf methods for a whole bunch of primitives and for Object too (equivalent to ObjecttoString())
in substring(intint) the second arg is exclusive
indexOf methods returns for not found
* 类String
类String是不可变的即使他的某些方法看起来会改变字符串的内容但实际上他们返回的是一个新的字符串而不是改变原来的字符串
类String的方法cancat(String)trim()replace(charchar)
类String的静态方法valueOf能处理所有的基本类型和对象(调用对象的toString()方法)
在substring(intint)方法中第二个参数是不包括的(译者注第一个参数是包括的例如substring()将会返回字符串从第二个字符开始(包括第二个字符)到第五个字符结束(不包括第五个字符)的子字符串)
如果没有找到indexOf方法将返回
* String Pool:
A JVM has a string pool where it keeps at most one object of any String String literals always refer to an object in the string pool String objects created with the new operator do not refer to objects in the string pool but can be made to using Strings intern() method Two String references to equal strings in the string pool will be ==
* 字符串池
虚拟机有一个字符串池保存着几乎所有的字符串对象字符串表达式总是指向字符串池中的一个对象使用new操作创建的字符串对象不指向字符串池中的对象但是可以使用intern方法使其指向字符串池中的对象(译者注如果池中已经有相同的字符串使用equals方法确定则直接返回池中的字符串否则先将字符串添加到池中再返回)池中两个相等的字符串如果使用==来比较将返回真
* StringBuffer doesnt override equals
* 类StringBuffer没有覆盖equals方法
Arrays
数组
* Arrays are objects the following create a reference for an int array
int[] ii;
int ii[];
* 数组是一个对象 下面的代码创建一个整型数组的引用
int[] ii;
int ii[];
* You can create an array object with new or an explicit initializer:
ii = new int[];
ii = new int[] { };
int[] ii = { ); // only when you declare the reference
* 你可以通过new操作或者显式的初始化创建一个数组对象
ii = new int[];
ii = new int[] { };
int[] ii = { }; // 只有声明的时候
* CAREFUL: You cant create an array object with:
int iA[];
* 小心你不能象下面这样创建一个数组对象
int iA[];
* If you dont provides values the elements of obj arrays are always initialized to null and those of primitive arrays are always initialized to
* 如果你不提供初始值对象数组的元素总是初始化成null基本类型数组的元素总是初始化成零
Primitive Types
基本类型
* Primitive types:
short and char are both bytes
int and float are both bytes
long and double are both bytes
char is the only unsigned primitive type
* 基本类型
short和char的长度是两个字节
int和float的长度都是四个字节
long和double的长度都是八个字节
char是唯一的无符号基本类型
* Literals:
You can have boolean char int long float double and String literals
You cannot have byte or short literals
char literals: d \uc (the c must be a digit hex number)
int literals: xc is hex is octal(for )
You can initialize byte short and char variables with int literals(or const int expressions) provided the int is in the appropriate range
* 表达式
只有booleancharintlongfloatdouble和字符串的表达式没有byte和short的表达式
字符(char)表达式d\uc(c必须是四位的十六进制数字)
整型(int)表达式xc是十六进制形式是八进制形式
可是使用合法范围内的整型表达式对byteshort和char变量初始化
* CAREFUL: cant assign a double literal to a float float fff = ;
* 小心不能将一个double表达式赋给一个float变量 float fff = ;
* The only bit operators allowed for booleans are &^| (cant do ~ or shift ops)
* 位运算只有&^|(不能使用~或者移位操作)
* Primitive wrapper classes
are immutable
override equals
the static valueOf(String) methods in primitive wrapper classes return wrapper objects rather than a primitives
* 基本类型的包装类
不可变的
覆盖equals方法
静态方法valueOf(String)返回的是包装类而不是基本类型
Conversions and Promotions
类型转换
* boolean>anything but boolean or string is not allowed
* All other primitive conversions are allowed with an explicit cast
* char/byte/short/int/long to float/double is a widening conversion even if some precision is lost (the overall magnitude is always preserved)
* Narrowing conversions require an explicit cast
integral narrowing conversions simply discard highorder bits
anything to char is a narrowing conversion (inc byte) because its signed to unsigned and negative numbers get messed up
* boolean不能跟其它的任何类型相互转换但是boolean>String是允许的
* 所有的基本类型之间可以通过显式的类型转换而转变成其它类型
* char/byte/short/int/long到float/double的转换是宽转换即使有可能丢掉部分信息
* 窄转换需要显式的转换
整型的窄转换只简单的去掉高位比特
所有到char的转换都是窄转换(包括byte)因为转换是从有符号数到无符号数
的转换负数将会得到一个混乱的结果
* Widening primitive and reference conversions are allowed for assignment and in matching the arguments to a method (or ctor) call
* 对象和基本类型的宽转换允许在赋值和匹配的方法调用中(非显式的)使用
* For assignment (but not method invocation) representable constant int expressions can be converted to byte char or shorts (eg char c = )
* 赋值时合法的整型表达式能被自动转换成bytechar或者short(例如char c = )
* Unary numeric promotions: byte/short/char to int