Java Tips
Last updated
Was this helpful?
Last updated
Was this helpful?
常见的一些误区或小算法优化,可以用AS的查看class文件看编译字节码的命令序,看写法的优化或算法的优化。
有符号数的负数表示:按位取反,再加1. 如5=00000101
; -5=11111011
;
Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.这里是Java编程语言的关键字列表。你不能在你的程序中使用以下任何一个作为你的标识符。const和goto是保留关键字,即使它们当前未被使用。true、false和null似乎也是关键词,但是它们是实际上是文字;你不能将它们用作程序中的标识符。
Reserved words
abstract
continue
for
new
switch 开关 切换
assert *
default
goto *
package
synchronized 同步
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum **
instanceof
return
transient 短暂
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp **
volatile 易失
const *
float
native 本地
super
while
friendly,sizeof不是java的关键字,并且java关键字都是小写的
Java保留字是指现有Java版本尚未使用但以后版本可能会作为关键字使用。
byValue, cast, false, future, generic, inner, operator, outer, rest, true, var, goto, const, null
The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as you've already seen:Java编程语言是静态类型的,这意味着所有变量必须先声明才能使用。这包含说明变量的类型与名称,如您所见的
int gear = 1;
Doing so tells your program that a field named "gear" exists, holds numerical data, and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed on it. In addition to int, the Java programming language supports seven other primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:这告诉你的程序,一个名为『gear』的字段的存在,保存数字数据,初始值为1。数量的数据类型确定其包含的值,以及可能对其执行的操作。此外int,java编程语言支持七种其它原始数据类型。原始类型由语言预定义,并由保留关键字命名。原始值不与其它原始值共享状态。Java编程语言支持的八种原始数据类型。
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.字节:byte数据类型是一个8位有符号二进制补码整数。它的最小值为-128,最大值为(包含)127。它可以用于在大型阵列中节省内存,实际上内存节省很重要。它们也能用于替代int,其限制有助于澄清您的代码。变量范围有限的事实可以作为一种文档形式。
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.短整型:它是16位有符号二进制整数。这的值域为( -32768<=short <= 32767)。与byte一样,相同的指导原则适用:你能使用short在大数组中节省内存,实际上内存节省是非常重要的。
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.整型:默认情况下,int型是32位有符号二进制补码整数,它的值域(-2^31 -> 2^31 - 1)。在JSE8及更高版本中,你能使用int型去表示无符号32位整数,它的值域为(0 -> 2^32 -1)。使用Integer类将int数据类型作为无符号整数。看Number了解更多详情。静态方法像 compareUnsigned、divideUnsigned等已被添加到Integer类,以支持无符号整数算术运算。
long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.长整型:它是64位二进制补码整数。值域(-2^63 -> 2^63 -1)。在JSE8及更高版本中,你可以使用long型来表示无符号的64位长整数,值域(0 -> 2^64 - 1)。当你需要的范围超过int所提供的范围,请使用此数据类型。Long类也qnwy包含了compareUnsigned、divideUnsigned等的支持无符号长整型数学运算。
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.浮点型:该型是单精度32位IEEE 754浮点数。其范围超出本讨论范围,但java语言规范的浮点型、格式、值中指定。正如所byte和short建议的,使用float(代替double)当你需要节省内存存储浮点数的大型阵列。不应将此数据类型用于精确值,例如货币。为此,你可能需要使用java.math.BigDecimal类来代替。Numbers和Strings覆盖BigDecimal和Java平台提供的其它有用的类。
double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.双精度:它是64位双精度IEEE 754的浮点数。它的值域超出此讨论范围。但在Java语言规范的浮点类型、格式、值的部分有指定。对于十进制值,该数据类型通常是默认选择。如上所述,此类型不能使用在精确的值。如货币。
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.布尔型: 该型仅有两个可能的值:true和false。将此数据类型用于跟踪真假条件的简单标志。该型表示一位信息,但它的『大小』不是精确定义的。
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).`字符: char数据类型是单16位Unicode字符串。它的值范围(\u0000 - \uffff (0 <= value <= 65535))。
tips: char的有意思的地方
In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object; for example, String s = "this is a string";. String objects are immutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such. You'll learn more about the String class in Simple Data Objects.除了以上八种原始数据类型外,Java编程语言还提供了特别的字符串支持通过java.lang.String类。将你的字符串包含在闭合的双引号内将自动的创建一个新String对象;例如:[String s = "this is a string";]。String对象是不可变的,这意味着一量创建,值就不可变。该String类不是技术上原始数据类型,但考虑由语言所赋予的特殊支持,你可能会倾向于认为它是这样的。你将了解更多多信息关于String类在Sample Data Objects中。
优先级由高到低
运算符分类
结合顺序
运算符
分隔符
左结合
.
[]
( )
;
,
一元运算符
右结合
!
++
--
-
~
算术运算符 移位运算符
左结合
*
/
%
+
-
<<
>>
>>>
关系运算符
左结合
<
>
<=
>=
instanceof(Java 特有)
==
!=
逻辑运算符
左结合
!
&&
|| ~
&
| ^
三目运算符
右结合
布尔表达式?表达式1:表达式2
赋值运算符
右结合
=
*= /=
%=
+=
-=
<<=
>>=
>>>=
&=
|=
运算符
含义
例子
-
改变数值的符号,取反
-x(-1*x)
~
逐位取反,属于位运算符
~x
++
自加1
x++
--
自减1
x--
运算符
含义
例子
+
加法运算
x+y
-
减法运算
x-y
*
乘法运算
x*y
/
除法运算
x/y; 5/3=1
%
取模运算(求余运算)
x%y; 5%3=2
运算符
含义
例子
<<
左移运算符,将运算符左边的对象向左移动运算符右边指定的位数(在 低位补0 )
x<<3
>>
"有符号"右移运算 符,将运算符左边的对象向右移动运算符右边指定的位数。使用符号扩展机制,也就是说,如果 值为正,则在高位补0,如果 值为负,则在高位补1.
x>>3
>>>
"无符号"右移运算 符,将运算符左边的对象向右移动运算符右边指定的位数。采用0扩展机制,也就是说,无论值的正负,都在 高位补0.
x>>>3
运算符
含义
例子
<
小于
x<y
>
大于
x>y
<=
小于等于
x<=y
>=
大于等于
x>=y
==
等于
x==y
!=
不等于
x!=y
A
!A
true
false
false
true
A
B
A && B
false
false
false
true
false
false
false
true
false
true
true
true
A
B
A || B
false
false
false
true
false
true
false
true
true
true
true
true
& | ~ ^ 位运算符
同位同为1结果1 如:129 & 128 = 128
A
B
A & B
1
1
1
1
0
0
0
1
0
0
0
0
同位只要有一个为1,结果为1; 或说:同位上的同为0 结果为0,否则为1
如: 129 | 128 = 129
A
B
A | B
1
1
1
1
0
1
0
1
1
0
0
0
一元运算符, int a=2; System.out.println("a 非的结果是:"+(~a));//-3
A
~A
1
0
0
1
同位上相同返回 0,不同返回 1;
A
B
A ^ B
1
1
0
1
0
1
0
1
1
0
0
0
运算符
例子
含义
+=
x += y
x = x + y
-=
x -= y
x = x - y
*=
x *= y
x = x * y
/=
x /= y
x = x / y
%=
x %= y
x = x % y
>>=
x >>= y
x = x >> y 带符号右移
>>>=
a >>>= y
x = x >>> y 无符号右移
<<=
a <<= y
x = x << y 带符号左移
&=
x &= y
x = x & y
|=
x |= y
x = x | y
^=
x ^= y
x = x ^ y
字符串 +: 连接不同的字符串。
转型运算符 ():可将一种类型的数据或对象,强制转换为另一种类型。如果类型不兼容,会抛异常出来。
== 比较的是内存地址 equals 比较的是类型与值
Integer中 -128至127会有Integer.IntegerCache的小值缓存对象,不会重复创建对象的,使用时需要注意
前代码被javac编译为class字节码后,它的命令结构如下
System.out.println(" try "+ ++i);
会被编译成
但System.out.println(" try " + i);
编译后还是System.out.println(" try " + i);
预编译后
所说java还是只识别for循环,写法上只是多了一个便利方式而已。
此三类的存储的都是char[] value
.
String是不可变的,对内容进行变更时,都会重新创建新对象。看它的源码可看出。
StringBuffer
和StringBuilder
都是继承自AbstractStringBuilder
存在它里的值都是
char[] value;
,是可变长的。 StringBuffer是线程安全的,可以看其源码,大部分方法都加了synchronize modifier。且它还多个了成员private transient char[] toStringCache;
详情看AbstractStringBuilder.ensureCapacity().
为什么要使用嵌套类? 使用嵌套类的令人信服的原因包括:
这是一种仅在一个地方使用的逻辑分组类的方法:如果类仅对另一个类有用,则将其嵌入该类并将其保持在一起是合乎逻辑的。嵌套这样的“助手课程”使他们的包更加精简。
它增加封装:考虑两个顶级类A和B,其中B需要访问否则将被声明的A成员private。通过在类A中隐藏B类,A的成员可以被声明为private,B可以访问它们。另外,B本身也可以从外界隐藏起来。
它可以导致更可读和可维护的代码:将顶级类中的小类嵌套将代码更接近使用的位置。
强烈不推荐序列化(内部类、本地类、匿名类)。在反序列化时,可能有兼容问题。 Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
内部类是依赖于外部类而存活,不能独立创建,可共享相关权限的属性或方法。
内部类的成员或变量不能声明为静态。
内部类可访问外部类的所有成员或方法。
与类方法和变量一样,静态嵌套类与其外部类相关联。和静态类方法一样,静态嵌套类不能直接引用它的封闭类中定义的实例变量或方法:它只能通过对象引用来使用它们。
注意: 静态嵌套类与其外层类(和其他类)的实例成员一样,与任何其他顶级类一样。实际上,静态嵌套类是行为上的顶级类,已经嵌套在另一个顶级类中以便于打包。
静态嵌套类 nested static classes 不能在类方法内声明,它是与外部类相关联的。此种类只能声明为本地类。