java笔试题目

public class XX {
    
    public void add(Byte b)
    {
        b = b++;
    }
    
    public void test()
    {
        Byte a = 127;
        Byte b = 127;
        add(++a);
        System.out.print(a + " ");
        add(b);
        System.out.print(b + "");
    }

    public static void main(String s[]) {
        XX xx = new XX();
        xx.test();
    }
}

-128 127

参数是对象时,java是引用传递。参数是基本类型时,java是值传递

Byte类型数据居然能++a 这么操作? 有点坑

byte 数据范围为-128~127

 

strictfp, 即 strict float point (精确浮点)。

strictfp 关键字可应用于类、接口或方法。当对一个类或接口使用 strictfp 关键字时,该类中的所有代码,包括嵌套类型中的初始设定值和代码,都将严格地进行计算。
严格约束意味着所有表达式的结果都必须是 IEEE 754 算法对操作数预期的结果,以单精度和双精度格式表示。

如果你想让你的浮点运算更加精确,而且不会因为不同的硬件平台所执行的结果不一致的话,可以用关键字strictfp.

public strictfp class XX {

    public static void main(String[] args) {
        float aFloat = 0.6710339f;
        double aDouble = 0.04150553411984792d;
        double sum = aFloat + aDouble;
        float quotient = (float) (aFloat / aDouble);
        System.out.println("float: " + aFloat);
        System.out.println("double: " + aDouble);
        System.out.println("sum: " + sum);
        System.out.println("quotient: " + quotient);
    }
}

float: 0.6710339
double: 0.04150553411984792
sum: 0.7125394529774224
quotient: 16.167336
不使用关键词strictfp也是这样的结果。可能只有在不同的平台上进行测试才能看出结果来吧


自动装箱是Java编译器在基本数据类型和对应的对象包装类型之间做的一个转化。比如:把int转化成Integer,double转化成double,等等。反之就是自动拆箱。


public class foo{

    public static void main (String[] args){

        String s;
        System.out.println("s=" + s);
    }
}
Java中所有定义的基本类型或对象都必须初始化才能输出值
 ---------------------------------------------
public class XX{

    public static void main(String s[]) {
        String a;
        ((XX)null).haha();
    }

    public static void haha(){
        System.out.println("haha");
    }
}

haha

能正常运行,稍微有点坑(无聊的题目)

----------------------------------------

class HelloA {

    public HelloA() {
        System.out.println("HelloA");
    }
    
    { System.out.println("I'm A class"); }
    
    static { System.out.println("static A"); }

}

public class HelloB extends HelloA {
    public HelloB() {
        System.out.println("HelloB");
    }
    
    { System.out.println("I'm B class"); }
    
    static { System.out.println("static B"); }
    
    public static void main(String[] args) {

        System.out.println("-------main start-------");
        new HelloB();
        new HelloB();
        System.out.println("-------main end-------");
    }
}
static A
static B
-------main start-------
I'm A class
HelloA
I'm B class
HelloB
I'm A class
HelloA
I'm B class
HelloB
-------main end-------

对象的初始化顺序:(1)类加载之后,按从上到下(从父类到子类)执行被static修饰的语句;(2)当static语句执行完之后,再执行main方法;(3)如果有语句new了自身的对象,将从上到下执行构造代码块、构造器(两者可以说绑定在一起)。

-------------------------------

        String $s = "sdfjsdlfjl";
        int x = $s.length();

$符号可以作为标识符,#不能

------------------------------------

        String s = Integer.toBinaryString(a);  //2进制
        Integer.toOctalString(n);  //8进制
        Integer.toHexString(n);//16进制

----------------------------

float b = 3.2; //compile error  can't convert from double to float

---------------------

        int a = 1;    
        int b = a << 33;
        System.out.printf(Integer.toBinaryString(b));
        // 1000000000000000000000000000000   30
        //10000000000000000000000000000000   31
        //                               1   32
        //                              10   33

-------------------------

 

    public static void main(String t[]) {
        int a = 0;
        String s = "sdf";
s = a + s;
//if (s == a) {}//compile error:Incompatible operand types String and int boolean xx = false; Boolean yy = true; if (xx == yy){} }

 

int a = Integer.parseInt(“1b”,16);

 

Math.round(11.5)==12;Math.round(-11.5)==-11;round方法返回与参数最接近的长整数,参数加1/2后求其floor

posted @ 2015-02-26 13:02  牧 天  阅读(218)  评论(0)    收藏  举报