第一题:两个对象:1,"xyz";2:指向“xyz”的引用对象s

解决方案 »

  1.   

    String s = new String("xyz");创建了几个String Object?
    2个编程写一个Singleton出来。(或者用自然语言讲出你的思路)
    class xx
    {
    static t=null
    private xx(){};
    public xx create()
    {
       if(xx==null)
         t=new xx();
       return xx;
    }
    }
      

  2.   

    当然是一个对象了,new了一次而已.单例实在太简单,用饿汉模式就不会有双重check的问题了.在JBuilder些一下,然后查看UML视图,打印出来我还真记不得这些url,不过不难,连接池就是一个连接的池,其实应该比较简单,和一般的缓冲区差不多
      

  3.   

    关于Singleton:有几种形式:
     如:
    第一种形式:饿汉Singleton
    public class Singleton { 
      private static Singleton x=new Singleton();
      private Singleton(){}
      public static Singleton getInstance(){
         return x;
      }}第二种形式: 懒汉Singleton
    public class Better{
      private static Better x=null;
      private Better(){}
      public static Better getInstance(){
        if(x==null)
          x=new Better();
        return x;
      }//lazy instantiation
    }
    但是请注意,上述两种Singleton不能继承,因为Constructor为private,这样就引出了
    第三种:登记式Singletonpublic class RegSingleton 

    static private HashMap m_registry = new HashMap(); static 

    RegSingleton x = new RegSingleton(); 
    m_registry.put(x.getClass().getName(), x); 
    } protected RegSingleton() 
    { } public static RegSingleton getInstance(String name) 

    if (name == null) 

    name = "RegSingleton"; 
    } if (m_registry.get(name) == null) 

    try 

    m_registry.put(name, Class.forName(name).newInstance()); 

    catch (Exception e) 

    System.out.println("Error happened."); 
    } } return (RegSingleton)(m_registry.get(name)); 
    } public String about() 

    return "Hello, I am RegSingleton."; 

    } RegSingletonChild.java 
    import java.util.HashMap; public class RegSingletonChild extends RegSingleton 

    public RegSingletonChild() 

    } static public RegSingletonChild getInstance() 

    return (RegSingletonChild) 
    RegSingleton.getInstance("RegSingletonChild"); 
    } public String about() 

    return "Hello, I am RegSingletonChild."; 


      

  4.   

    对象应该是两个。
    “xx”为一个,new后又有一个。