- 论坛徽章:
- 0
|
操作符
Almost all operators work only with primitives. The exceptions are ‘=‘, ‘==‘ and ‘!=‘, which
work with all objects (and are a point of confusion for objects). In addition, the String class
supports ‘+’ and ‘+=‘.
几乎所有的操作符都只能操作基本类型,除了=,==,!=之外,另外对于String 类,还支持对+,+=
赋值:. Whenever you manipulate an object, what you’re manipulating is the reference, so when you assign “from one object to another,” you’re actually copying a reference from one place to another. This means that if you say c = d for objects, you end up with both c and d pointing to the object that, originally, only d pointed to
当你在对象上操作时,实际上操作的是对对象的引用,当你将一个对象赋值给另外已给对象时,你实际上是将引用从一个地方拷贝到另外一个地方.这意味着,c=d,这时,c,d都指向原先d所指向的对象,当如果操作的是基础类型,比方说是整形,则是将d的值赋给c,而不是d的地址,以后c和d就没有任何的瓜葛了。如果是这样赋值:t1.level = t2.level;则不会涉及到地址的变化
class Tank {
int level;
}
public class Assignment {
public static void main(String[] args) {
Tank t1 = new Tank();
Tank t2 = new Tank();
t1.level = 9;
t2.level = 47;
print("1: t1.level: " + t1.level +
", t2.level: " + t2.level);
t1 = t2;
print("2: t1.level: " + t1.level +
", t2.level: " + t2.level);
t1.level = 27;
print("3: t1.level: " + t1.level +
", t2.level: " + t2.level);
}
} /* Output:
1: t1.level: 9, t2.level: 47
2: t1.level: 47, t2.level: 47
3: t1.level: 27, t2.level: 27
*///:~
用引用传递参数:
class Letter {
char c;
}
public class PassObject {
static void f(Letter y) {
y.c = ‘z’;
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = ‘a’;
print("1: x.c: " + x.c);
f(x);
print("2: x.c: " + x.c);
}
} /* Output:
1: x.c: a
2: x.c: z
*///:~
In many programming languages, the method f( ) would appear to be making a copy of its
argument Letter y inside the scope of the method. But once again a reference is being
passed, so the line y.c = ‘z’;
is actually changing the object outside of f( ).
由于Java中传递的是引用,所以对象的成员会改变
关系操作符
通过下面两个例子来说明对象的等价性
public class Equivalence {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
System.out.println(n1 != n2);//比较的是引用,而不是引用的内容
System.out.println(n1.equals(n2));
//equals方法比较的是引用的内容
}
}
//out put
false
true
true
对于自己定义的类,则如果你没覆盖equals方法,equals默认方式时比较引用,而不是引用的内容
静态导入是JDK1.5中的新特性。一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....ClassName.*;这里的多了个static,还有就是类名ClassName后面多了个 .* ,意思是导入这个类里的静态方法。当然,也可以只导入某个静态方法,只要把 .* 换成静态方法名就行了。然后在这个类中,就可以直接用方法名调用静态方法,而不必用ClassName.方法名 的方式来调用。
这种方法的好处就是可以简化一些操作,例如打印操作System.out.println(...);就可以将其写入一个静态方法print(...),在使用时直接print(...)就可以了。
//: operators/Bool.java
// Relational and logical operators.
import java.util.*;
import static net.mindview.util.Print.*;
public class Bool {
public static void main(String[] args) {
Random rand = new Random(47);
int i = rand.nextInt(100);
int j = rand.nextInt(100);
print("i = " + i);
print("j = " + j);
print("i > j is " + (i > j));
print("i
print("i >= j is " + (i >= j));
print("i
print("i == j is " + (i == j));
print("i != j is " + (i != j));
// Treating an int as a boolean is not legal Java:
//! print("i && j is " + (i && j));
//! print("i || j is " + (i || j));
//! print("!i is " + !i);
print("(i
+ ((i
print("(i
+ ((i
}
} /* Output:
i = 58
j = 55
i > j is true
i
i >= j is true
i
i == j is false
i != j is true
(i
(i
*///:~
需要注意的是与C++不同,逻辑操作符两端只能是bool类型的而不能是其他类型的
同样和C++一样,在Java中也会发生断路现象
指数符号e
long n3 = 200;
这里200l,200L,200没什么区别
there’s no ambiguity, so an L after the 200 would be superfluous. However, with
float f4 = 1e-43f;对于这个,则f一定要加,因为以e为指数,默认类型是double
类型转换CAST
public class Casting {
public static void main(String[] args) {
int i = 200;
long lng = (long)i;
lng = i; // "Widening," so cast not really required
long lng2 = (long)200;
lng2 = 200;
// A "narrowing conversion":
i = (int)lng2; // Cast required
}
} ///:~
转换范围扩展转换和窄化转化,窄化转换时危险的,可能会丢失信息,必须进行强制转换
Java allows you to cast any primitive type to any other primitive type, except for boolean,
which doesn’t allow any casting at all. Class types do not allow casting. To convert one to the
other, there must be special methods. (You’ll find out later in this book that objects can be
cast within a family of types; an Oak can be cast to a Tree and vice versa, but not to a
foreign type such as a Rock.)
除了bool类型外,基础类型之间可以相互转化。
类之间不可进行cast,如果需要转化的话,要采用特定的方法
截短和圆整
如果进行窄化cast很多情况下都是进行截短,看下面的例子
double above = 1.7, below = 1.4;
float fabove = 0.7f, fbelow = 0.4f;
System.out.println("(int)above: " + (int)above);
System.out.println("(int)below: " + (int)below);
System.out.println("(int)fabove: " + (int)fabove);
System.out.println("(int)fbelow: " + (int)fbelow);
看看输出吧:
(int)above: 1
(int)below: 1
(int)fabove: 0
(int)fbelow: 0
如果想要圆整使用round( )
System.out.println("Math.round(above): " + Math.round(above));
System.out.println("Math.round(below): " + Math.round(below));
System.out.println("Math.round(fabove): " + Math.round(fabove));
System.out.println("Math.round(fbelow): " + Math.round(fbelow));
}
Math.round(above): 2
Math.round(below): 1
Math.round(fabove): 1
Math.round(fbelow): 0
Java没有sizeof
由于Java是建立在虚拟机上的语言,所以不要考虑硬件差异,也就无需sizeof
注意:
Note that boolean is quite limited. You can assign to it the values true and false, and you can test it for truth or falsehood, but you cannot add booleans or perform any other type of operation on them.
Boolean在java中的限制很多,不能进行任何算术运算
char, byte, and short在举行运算时会进行提升变为int型,运算结果也是int型
如果进行赋值,应该进行窄化转化
In char, byte, and short, you can see the effect of promotion with the arithmetic operators. Each arithmetic operation on any of those types produces an int result, which must be explicitly cast back to the original type (a narrowing conversion that might lose information) to assign back to that type. With int values, however, you do not need to cast, because everything is already an int.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/69999/showart_2038290.html |
|