Java-Notes

下列程序的输出结果是:

public class test
{
    public static void main(String args[])
    {
        String a="abc";
        String b="abc";
        String c=new String(b);

        System.out.print(a==b);/* a和b指向同一个对象**/
        System.out.print(' ');
        System.out.print(a==c);
        System.out.print(' ');
        System.out.print(c==b);
        System.out.println();
    }
}

结果是:true false false

下列程序的输出结果是:

public class test
{
    public static int a,c;
    public static void main(String args[])
    {
        a=0;c=0;
        do
        { 
            --c; 
            a=a-1; 
        }
        while(a>0);
        System.out.println(c);
    }
}

结果是:-1

下列程序的输出结果是:

public class test
{
    public static void main(String args[])
    {
        Integer n1 = new Integer(47);   
        Integer n2 = new Integer(47); 
        int r1=47;
        int r2=47;

        System.out.print(n1==n2); 
        System.out.print(","); 
        System.out.print(n1!=n2);
        System.out.print(","); 
        System.out.println(r1!=r2); 
    }
}

结果是:false,true,false

写一个Singleton。Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。

方法1

public class Singleton
{
    private final static Singleton INSTANCE = new Singleton();
    private Singleton(){}

    public static Singleton getInstance() 
    {
        return INSTANCE;
    }
}

方法2

public class Singleton 
{
    private static volatile Singleton INSTANCE = null;

    // Private constructor suppresses 
    // default public constructor
    private Singleton() {}

    //thread safe and performance  promote 
    public static  Singleton getInstance() 
    {
        if(INSTANCE == null)
        {
            synchronized(Singleton.class)
            {
                //when more than two threads run into the first null check same time, to avoid instanced more than one time, it needs to be checked again.
                if(INSTANCE == null)
                { 
                    INSTANCE = new Singleton();
                }
            } 
        }
        return INSTANCE;
    }
}

下列程序的输出结果是:

public class test
{
    public static void changeStr(String str)
    {
        str="welcome";//这里的更改没有起到作用
    }
    public static void main(String[] args) 
    {
        String str="1234";
        changeStr(str);
        System.out.println(str);
    }
}

结果是:1234

下列程序的输出结果是:

public class test
{
    static boolean foo(char c) 
    {
        System.out.print(c);
        return true;
    }
    public static void main(String[] argv) 
    {
        int i = 0;
        for (foo('A'); foo('B') && (i < 2); foo('C')) 
        {
            i++;
            foo('D');
        }
    }
}

结果是:ABDCBDCB

下列程序的输出结果是:(建行武汉软开面试题)

public class ans
{
    public static void main(String args[])
    {
        String a="abc";
        String b="abcde";
        String c=b.substring(3);
        System.out.println(a==c);
    }
}

结果是:false

links

social