Java课程作业01

 1. 对象变量也可以使用“==”判断两变量值是否相等吗?

  判断两个对象是否是同一个引用对象则用==,"=="比的是地址.因为如果地址相同,则就是同一个对象
  obj1.equals(Object obj2),用来判断两个对象是否相等
2.请输入并运行以下代码,得到什么结果?

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Foo obj1=new Foo();
Foo obj2=new Foo();
System.out.println(obj1==obj2);
}

}
class Foo{
int value=100;
}

结果为false

3.以下代码为何无法通过编译?哪儿出错了?

public class Test{

  public static void main(String[] args){

  Foo  obj1=new Foo()

}

}

class Foo{

  int value;

  public Foo(int initvalue){

  value=initvalue;

}

}

如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法。

4.Java字段初始化的规律

Java进行初始化的地方有两个:初始化块和构造函数,其中初始化块又分为静态初始化块和实例初始化块。静态初始化块是类中由static修饰的初始化块,实例初始化块为类中没有任何关键字修饰的初始化语句。

如果在主函数中创建对象时没有形参时,如果在类中定义了公共的变量并给与了赋值,那么就会把值赋给主函数中的变量,再调用类中的默认构造函数,如果在主函数中创建对象时有形参,则调用类中对应的构造函数。

5.类字段的初始化顺序

执行类成员定义时指定的默认值或类的初始化块,到底执行哪一个要看哪一个“排在前面”。

执行类的构造函数。

类的初始化块不接收任何的参数,而且只要一创建类的对象,它们就会被执行。因此,适合于封装那些“对象创建时必须执行的代码”。

静态初始化块->初始化块(构造代码块)->构造函数

6.静态初始化块的执行顺序

静态初始化块只执行一次。

创建子类型的对象时,也会导致父类型的静态初始化块的执行。

7.两对整数明明完全一样,为何一个输出true,一个输出false?

public class StrangeIntegerBehavior 
{

    
    public static void main(String[] args)
    {

        
        Integer i1=100;
       
        Integer j1=100;
        
        System.out.println(i1==j1);

        
        Integer i2=129;
        
        Integer j2=129;
        
        System.out.println(i2==j2);
    
    }


}


输出结果表明i1和j1指向的是同一个对象,而i2和j2指向的是不同的对象。

此时只需一看源码便知究竟,下面这段代码是Integer的valueOf方法的具体实现:

public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

而其中IntegerCache类的实现为:

private static class IntegerCache {
        static final int high;
        static final Integer cache[];

        static {
            final int low = -128;

            // high value may be configured by property
            int h = 127;
            if (integerCacheHighPropValue != null) {
                // Use Long.decode here to avoid invoking methods that
                // require Integer's autoboxing cache to be initialized
                int i = Long.decode(integerCacheHighPropValue).intValue();
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - -low);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }


 private static class IntegerCache {
        static final int high;
        static final Integer cache[];

        static {
            final int low = -128;

            // high value may be configured by property
            int h = 127;
            if (integerCacheHighPropValue != null) {
                // Use Long.decode here to avoid invoking methods that
                // require Integer's autoboxing cache to be initialized
                int i = Long.decode(integerCacheHighPropValue).intValue();
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - -low);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

从这2段代码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

上面的代码中i1和j1的数值为100,因此会直接从cache中取已经存在的对象,所以i1和i2指向的是同一个对象,而i2和j2则是分别指向不同的对象。

8.如何在静态方法中访问类的实例成员

public class Example
  {
   int x = 3;//类的实例变量,初始化值为3
   static int  y = 4;//类的静态变量,初始化值为4
   public static void method()//静态方法
    {   
     System.out.println("实例变量x = " + new Example().x);//在静态方法中访问类的实例变量需首先进行类的实例化
     System.out.println("静态变量y = " + y);//在静态方法中可直接访问类的静态变量
    }
 
  public static void main(String[] args) 
     {
          Example.method();
          Example ex = new Example();
         System.out.println("x = " + ex.x);
     }
 }
 
 
原文地址:https://www.cnblogs.com/bangandwolf/p/7688578.html