2015-04-10一些知识点

  今天看到几个有意思的小程序

package com.lk.A;

public class Test1 {
    static{
        int x = 5;//在第一次被载入JVM时运行,但由于是局部变量,x=5不影响后面的值
    }
    static int x,y;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        x--;
        myMethod();
        System.out.println(x + y++ + x);//1+0+1=2
    }
    public static void myMethod(){
        y = x++ + ++x;
        //此处:首先x=-1,x++后x还是-1,但是执行后x变为0,走到++x时,x已经变为0,在进行++运算,得到x=1
        //因此算式应该是y=-1+1=0;
    }
}
package com.lk.A;

public class Test2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 0;
        System.out.println(i+'0');//int+char类型自动向上转型至int型,因此程序输出48
        //即(int)'0'=48   i+48 = 48
    }

}
package com.lk.A;

public class Test3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int j = 0;
        for(int i = 0;i<100;i++){
            j = j++;
        }
        System.out.println(j);//此时程序输出0
        //因为在执行j=j++时,Java在j++使用了中间缓存变量机制。其等效于:
        //int temp = j;
        //j = j+1;
        //j = temp;
        //但是如果用j = ++j时,程序会输出100;但是在j= ++j这句话会出现警告
    }

}

   4、Java中,简单数据类型由低到高:(byte,short,char)-int-long-float-double

        低级类型为char型,向高级类型(整型)转换时,会转换为对应的ASCII码值。

        对于byte,short,char三种类型而言,他们是相同级别的,因此,不能相互自动转换,可以使用强制类型转换。

   5、对于高级类型向低级类型转换时除了用强转,还可以用包装类,但是都会损失精度。同时简单类型的变量转换为相应的包装类,可以利用包装类的构造函数。

package com.lk.A;

public class Test4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double d1 = 100.00;
        Double D1 = new Double(d1);
        int i1 = D1.intValue();
        System.out.println(i1);
        double d2 = 101.00;
        int i2 = (int)d2;
        System.out.println(i2);
    }

}
100
101

    6、字符型变量转换为数值型变量实际上有两种:一种是将其转换成对应的ASCII码;另一种是转换关系,例如,‘1’就是数值1,而不是其ASCII码

        方法:

package com.lk.A;

public class Test5 {
    public static void main(String[] args) {
        Character C = null;
        System.out.println(C.getNumericValue('3'));
        char c = '3';
        System.out.println((int)c);
    }
}
3
51

    7、+=默认不检查数据类型,例如

package com.lk.A;

public class Test5 {
    public static void main(String[] args) {
//        short s = 1;
//        s = s+1;//会报错,int不能不能直接赋值给short
short s1 = 1;
        s1 = (short)(s1+1);//可以通过强转 short s = 1; s += 1;//不报错 System.out.println(s); } }

 

package com.lk.A;

public class Test6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 5;
        System.out.println((a<5)?10.9:9);//Java这是会根据运算符的精度类型进行自动类型转换
        //由于前面有一个10.9,因此后面的9也会自动变成9.0。因此,输出的是9.0
    }

}

 

package com.lk.A;

public class Test6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub    
        char x = 'x';
        int i = 10;
        System.out.println(false ? i : x);
        //int i=10中i是一个变量,因此,第一个输出x被提升为int型了,因为i是int类型,x的int类型值为120,,所以输出120
        System.out.println(false ? 10 : x);
        //Java编程规范中提到,当后两个表达式有一个是常量表达式(这里是10)时,另外一个类型时T(这里是char)时
        //而常量表达式可以被T表示时,输出的结果是T类型。所以,因为10是常量,可以被char表示,所以结果是char型的
    }

}

 

package com.lk.A;

public class Test6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 1,b = 2;
        int c = a&b;
        System.out.println(c);
        //&在这里表示"按位与"及1=0001;2=0010,按位与之后是0000,所以输出0;
    }

}

 

posted @ 2015-04-10 18:01  luankun0214  阅读(140)  评论(0编辑  收藏  举报