@Gebitang
2015-09-04T21:32:00.000000Z
字数 4689
阅读 1343
java
Java byte数据类型详解
http://perfect5085.iteye.com/blog/1612694
JAVA 常用进制 转换
http://greemranqq.iteye.com/blog/1837881
Java二进制操作指南
http://www.importnew.com/15060.html
java位运算
http://blog.csdn.net/budapest/article/details/6752771
Java 位运算
http://www.blogjava.net/freeman1984/archive/2009/11/02/300694.html
http://blog.csdn.net/yangbobo1992/article/details/8062249
http://www.cnblogs.com/zhengtao/articles/1916751.html
来源
Q1.下面的代码片段会输出什么?
String s = " Hello ";
s += " World ";
s.trim( );
System.out.println(s);
A1.正确输出是” Hello World “。
Q2.下面的代码片段的输出是什么?
Object s1 = new String("Hello");
Object s2 = new String("Hello");
if(s1 == s2) {
System.out.println("s1 and s2 are ==");
}else if (s1.equals(s2)) {
System.out.println("s1 and s2 are equals()");
}
A2.输出结果是:
s1 and s2 are equals()
Q.下面代码片段的输出是什么?
Object s1 = "Hello";
Object s2 = "Hello";
if (s1 == s2) {
System.out.println("s1 and s2 are ==");
} else if (s1.equals(s2)) {
System.out.println("s1 and s2 are equals()");
}
A.答案是:
s1 and s2 are ==
Q.下面代码片段的输出结果是什么?
public class MethodOverrideVsOverload {
public boolean equals( MethodOverrideVsOverload other ) {
System.out.println("MethodOverrideVsOverload equals method reached" );
return true;
}
public static void main(String[] args) {
Object o1 = new MethodOverrideVsOverload();
Object o2 = new MethodOverrideVsOverload();
MethodOverrideVsOverload o3 = new MethodOverrideVsOverload();
MethodOverrideVsOverload o4 = new MethodOverrideVsOverload();
if(o1.equals(o2)){
System.out.println("objects o1 and o2 are equal");
}
if(o3.equals(o4)){
System.out.println("objects o3 and o4 are equal");
}
}
}
A.输出结果是:
MethodOverrideVsOverload equals method reached
objects o3 and o4 are equal
大多数的非final对象类方法都会被子类重写(overridden):
public boolean equals(Object obj); // make note of this method
public int hashCode();
public String toString();
重载方法在编译时起作用(例如,静态绑定),重写方法在运行时起作用(例如,动态绑定)。静态绑定意味着JVM在编译时决定调用的类或方法。而动态绑定时,JVM是在运行时决定调用的类或方法。动态绑定设计是多态的基础。更多了解编译时和运行时.
方法 | 说明 |
---|---|
参数 | 不可变(注:包括参数类型和个数) |
返回类型 | 不可变,除了协变返回类型或其子类型(covariant (subtype) returns) |
异常 | 子类中可以抛出更少的异常,但绝对不能抛出父类中没有定义的已检查异常 |
访问权限 | 比父类中对应方法更宽松 |
调用 | 运行时(也就是动态绑定),根据对象类型来决定调用的具体方法 |
MethodOverrideVsOverload 类中的”equals(MethodOverrideVsOverload other)” 方法并没有重写Object类中的”public boolean equals(Object obj)” 方法。这是因为其违背了参数规则,其中一个是MethodOverrideVsOverload 类型,而另一个是Object类型。因此,这两个方法是重载关系(发生在编译时),而不是重写关系。
因此,当调用o1.equals(o2)时,实际上调用了object类中的public boolean equals(Object obj)方法。这是因为在编译时,o1和o2都是Object类型,而Object类的equals( … )方法是比较内存地址(例如,Object@235f56和Object@653af32)的,因此会返回false。
当调用o3.equals(o4)时,实际上调用了MethodOverrideVsOverload 类中的equals( MethodOverrideVsOverload other )方法。这是因为在编译时,o3和o4都是MethodOverrideVsOverload类型的,因此得到上述结果。
Q.那怎么解决上面的那个问题呢?
A.在Java5中,新增了注解,其中包括很好用的编译时注解(compile time annotations)@override,来保证方法正确的重写了父类方法。如果在上面的代码中添加了注解,那么JVM会抛出一个编译错误。
public class MethodOverrideVsOverload {
@Override
public boolean equals( Object other ) {
System.out.println("MethodOverrideVsOverload equals method reached" );
return true;
}
public static void main(String[] args) {
Object o1 = new MethodOverrideVsOverload(); //during compile time o1 is of type Object
//during runtime o1 is of type MethodOverrideVsOverload
Object o2 = new MethodOverrideVsOverload(); //during compile time o2 is of type Object
//during runtime o2 is of type MethodOverrideVsOverload
MethodOverrideVsOverload o3 = new MethodOverrideVsOverload(); //o3 is of type MethodOverrideVsOverload
// during both compile time and runtime
MethodOverrideVsOverload o4 = new MethodOverrideVsOverload(); //o4 is of type MethodOverrideVsOverload
// during both compile time and runtime
if(o1.equals(o2)){
System.out.println("objects o1 and o2 are equal");
}
if(o3.equals(o4)){
System.out.println("objects o3 and o4 are equal");
}
}
}
输出为:
MethodOverrideVsOverload equals method reached
objects o1 and o2 are equal
MethodOverrideVsOverload equals method reached
objects o3 and o4 are equal
上面的代码中,运行时equals方法正确的重写了Object中的相应方法。这是一个比较容易混淆的问题,面试的时候需要很详尽的解释相关的概念。
计算给定文本内字符“A”的个数。分别用迭代和递归两种方式。
迭代
public class Iteration {
public int countA(String input) {
if (input == null || input.length( ) == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < input.length( ); i++) {
if(input.substring(i, i+1).equals("A")){
count++;
}
}
return count;
}
public static void main(String[ ] args) {
System.out.println(new Iteration( ).countA("AAA rating")); // Ans.3
}
}
递归
public class RecursiveCall {
public int countA(String input) {
// exit condition – recursive calls must have an exit condition
if (input == null || input.length( ) == 0) {
return 0;
}
int count = 0;
//check first character of the input
if (input.substring(0, 1).equals("A")) {
count = 1;
}
//recursive call to evaluate rest of the input
//(i.e. 2nd character onwards)
return count + countA(input.substring(1));
}
public static void main(String[ ] args) {
System.out.println(new RecursiveCall( ).countA("AAA rating")); // Ans. 3
}
}