[关闭]
@Gebitang 2015-09-04T21:32:00.000000Z 字数 4689 阅读 1349

JAVA Basic

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


Via ImportNew

非可变性(Immutability)和对象引用(Object reference)

来源
Q1.下面的代码片段会输出什么?

  1. String s = " Hello ";
  2. s += " World ";
  3. s.trim( );
  4. System.out.println(s);

A1.正确输出是” Hello World “。


equals与==

Q2.下面的代码片段的输出是什么?

  1. Object s1 = new String("Hello");
  2. Object s2 = new String("Hello");
  3. if(s1 == s2) {
  4. System.out.println("s1 and s2 are ==");
  5. }else if (s1.equals(s2)) {
  6. System.out.println("s1 and s2 are equals()");
  7. }

A2.输出结果是:

s1 and s2 are equals()

Q.下面代码片段的输出是什么?

  1. Object s1 = "Hello";
  2. Object s2 = "Hello";
  3. if (s1 == s2) {
  4. System.out.println("s1 and s2 are ==");
  5. } else if (s1.equals(s2)) {
  6. System.out.println("s1 and s2 are equals()");
  7. }

A.答案是:

s1 and s2 are ==


重载(overloading)与重写(overriding)

Q.下面代码片段的输出结果是什么?

  1. public class MethodOverrideVsOverload {
  2. public boolean equals( MethodOverrideVsOverload other ) {
  3. System.out.println("MethodOverrideVsOverload equals method reached" );
  4. return true;
  5. }
  6. public static void main(String[] args) {
  7. Object o1 = new MethodOverrideVsOverload();
  8. Object o2 = new MethodOverrideVsOverload();
  9. MethodOverrideVsOverload o3 = new MethodOverrideVsOverload();
  10. MethodOverrideVsOverload o4 = new MethodOverrideVsOverload();
  11. if(o1.equals(o2)){
  12. System.out.println("objects o1 and o2 are equal");
  13. }
  14. if(o3.equals(o4)){
  15. System.out.println("objects o3 and o4 are equal");
  16. }
  17. }
  18. }

A.输出结果是:

MethodOverrideVsOverload equals method reached
objects o3 and o4 are equal

方法 说明
参数 不可变(注:包括参数类型和个数)
返回类型 不可变,除了协变返回类型或其子类型(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会抛出一个编译错误。

  1. public class MethodOverrideVsOverload {
  2. @Override
  3. public boolean equals( Object other ) {
  4. System.out.println("MethodOverrideVsOverload equals method reached" );
  5. return true;
  6. }
  7. public static void main(String[] args) {
  8. Object o1 = new MethodOverrideVsOverload(); //during compile time o1 is of type Object
  9. //during runtime o1 is of type MethodOverrideVsOverload
  10. Object o2 = new MethodOverrideVsOverload(); //during compile time o2 is of type Object
  11. //during runtime o2 is of type MethodOverrideVsOverload
  12. MethodOverrideVsOverload o3 = new MethodOverrideVsOverload(); //o3 is of type MethodOverrideVsOverload
  13. // during both compile time and runtime
  14. MethodOverrideVsOverload o4 = new MethodOverrideVsOverload(); //o4 is of type MethodOverrideVsOverload
  15. // during both compile time and runtime
  16. if(o1.equals(o2)){
  17. System.out.println("objects o1 and o2 are equal");
  18. }
  19. if(o3.equals(o4)){
  20. System.out.println("objects o3 and o4 are equal");
  21. }
  22. }
  23. }

输出为:

MethodOverrideVsOverload equals method reached
objects o1 and o2 are equal
MethodOverrideVsOverload equals method reached
objects o3 and o4 are equal

上面的代码中,运行时equals方法正确的重写了Object中的相应方法。这是一个比较容易混淆的问题,面试的时候需要很详尽的解释相关的概念。


迭代(iteration)和递归(recursion)

计算给定文本内字符“A”的个数。分别用迭代和递归两种方式。
迭代

  1. public class Iteration {
  2. public int countA(String input) {
  3. if (input == null || input.length( ) == 0) {
  4. return 0;
  5. }
  6. int count = 0;
  7. for (int i = 0; i < input.length( ); i++) {
  8. if(input.substring(i, i+1).equals("A")){
  9. count++;
  10. }
  11. }
  12. return count;
  13. }
  14. public static void main(String[ ] args) {
  15. System.out.println(new Iteration( ).countA("AAA rating")); // Ans.3
  16. }
  17. }

递归

  1. public class RecursiveCall {
  2. public int countA(String input) {
  3. // exit condition – recursive calls must have an exit condition
  4. if (input == null || input.length( ) == 0) {
  5. return 0;
  6. }
  7. int count = 0;
  8. //check first character of the input
  9. if (input.substring(0, 1).equals("A")) {
  10. count = 1;
  11. }
  12. //recursive call to evaluate rest of the input
  13. //(i.e. 2nd character onwards)
  14. return count + countA(input.substring(1));
  15. }
  16. public static void main(String[ ] args) {
  17. System.out.println(new RecursiveCall( ).countA("AAA rating")); // Ans. 3
  18. }
  19. }

编译时与运行时

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注