Java 基础复习 01

暑假学习了Java,感觉比C++好用,所以想深入学习一下。于是就从图书馆借来一本SCJP培训与认证指南来看,书上n多错误啊,不过还是学到了一些东西,这就算是第一次笔记吧:

1: 有关数据类型:

public class Test{
    public static void main( String[] args ) {
//        float a = 3.14;                  1 错误,可能损失精度. 改为3.14F或f
//        float a = 12345L;             OK, 输出12345.0
//        float a = (-1.1)f;             错误, 去掉括号。
//        float a = 1F-2;                 OK, 输出-1.0
//        double a = 34.1e-2;            OK, 输出0.341
//        int a = -2147483648;        OK, int 为32位,表示了-2^31 到 2^31-1. unsigned int : 错误,不是语句。
//        byte a = 128;                        错误,"可能可能损失精度,找到int,需要byte"。 byte为8位,表示了-2^7 到 2^7-1;
//        long a = -9223372036854775808L;        OK, long 为64位,表示范围为 -2^63 到 2^63-1;  L不能少,否则会"过大的整数"
//        p(Integer.MIN_VALUE);         同样可以查看Double, Float, Integer, Short, Byte.

//            int a =7;            p(a++); p(a); p(++a);        输出为 7, 8, 9
//            int f=2, m=3;            int x = (f*m++); p(m); p(x);        输出为 4, 6; 因为++优先级高。如果为(f*++m),则为4, 8
                //char a = 64; p(a);
//            short a =3;  int b = 4;  a=(short)b;  必须强制类型转换,这与 short a = 3; 赋值操作要区分开。
//            char a = 64;   short b = a;            错误,可能损失精度。必须使用(short)a;
// 通常占用内存少的数据类型可以自动转化为占用内存多的。但 long=float 错误,而float=long 正确。
    }
    public static void p( Object o ) {
        System.out.println(o);
    }
}

 

 

2:有关类的加载与运行顺序:

public class Bus {
    private int maxLoad = prtN(20);
    static { System.out.println("Static 语句块执行"); }
    private static int currentLoad = prtS(1);
    public Bus() {
        System.out.println("执行构造函数的初始化工作");
        currentLoad = 10;
    }
    public void setMaxLoad(int value) {
        int a =0;
        maxLoad = value;
    }
    static { int b = prtSK(10); }
    public static int prtS( int k ) { System.out.println("prtS(" + k + ") 静态变量进行初始化 ");  return k; }
    public static int prtSK( int k) { System.out.println("prtSK(" + k + ") 静态语句块进行初始化"); return k; }
    public static int prtN( int k ) { System.out.println("prtN(" + k + ") 非静态成员变量进行初始化"); return k; }
    public static void main( String [] args ) {
        System.out.println("执行main() 方法");
        Bus s = new Bus();
//        static int ls = 8;     Error : 非法的表达式开始
    }   
    static { int a =7; System.out.println( a );}
}

/* 输出为
Static 语句块执行
prtS(1) 静态变量进行初始化
prtSK(10) 静态语句块进行初始化
执行main() 方法
prtN(20) 非静态成员变量进行初始化
执行构造函数的初始化工作
*/
/*
当执行java Bus 时 :
顺序执行static 变量和语句块。
mai()方法
new Bus() 时 将类的所有的成员变量初始化---》对成员变量赋值---》构造函数 --》对象引用赋值给引用变量

static 语句块中声明的变量属于类的局部变量,不是成员变量。
static 不能修饰局部变量。不能出现在方法中
*/

3:有关变量初始化:

public class A {
    static int ma;
    static String[] ms;
    public static void main( String[] args) {
        int la;
//    p( la );            Error: 可能尚未初始化变量 la
        String[] ls;
        p( ma );
        if( ms==null ) { p("数组: 成员变量自动初始化"); }
//    if( ls==null ) { p("数组: 局部变量自动初始化"); }    Error: 可能尚未初始化变量 ls.
        ls = new String[2];  p( ls[0]==null );
        int[] li = new int[1];  p( li[0]==0 );
    }
    public static void p( Object o ) {
        System.out.println(o);
    }
}
/* 输出为
0
数组: 成员变量自动初始化
true
*/
/*
对于局部变量: 无论是基本数据类型还是引用类型(声明一个数组String[] ls, ls就是一个引用类型)都不能自动进行初始化。
但对于数组变量 String ls = new String[2]; 即使是局部变量也还是用对应类型的默认值初始化,int 为0, 引用类型为 null.
*/

 

4:有关this:

class B {
    static int a, b;
    public static void main( String[] args) {
        B cb = new B(2, 3);
        B.IB cib =  cb.new IB(7);
    }
    B() { a=5; System.out.println( "this is B()." + a); }
    B( int a) { this(); this.a = a; System.out.println( "this is B( int a)."+ a); }
    B( int a, int b) { this( a ); this.b = b; System.out.println( "this is B( int a, int b)." + a); }
    class IB {
        IB( int i) { B.this.a = i;  System.out.println( "this is IB( int i)." + B.this.a); }
    }
}
/*  输出为
this is B().5
this is B( int a).2
this is B( int a, int b).2
this is IB( int i).7
*/
/*
this 代表当前对象的一个引用,所以不能出现在static方法或语句块中。
this( 参数列表 ) 可用于构造函数的重载(overload) 中
内部类访问外部类的当前对象格式为 : Outer.this
*/

 

5:有关Java关键字:

1>   default, goto(目前还没定义), const, strictfp, volatile, native 1.4 新增 assert;  1.5 新增 enum.

2>   strictfp:
就是精确浮点的意思。用来声明一个类,接口,方法。使其声明范围内的浮点数运算严格遵守 IEEE-754规范,使得结果更加准确且不因硬件/平台的不同而不同。 不允许对接口中的方法或类的构造函数声明为strictfp。

3> volatile 与 synchronized :
Java对普通的变量采用“主内存”机制,即使用一个主内存来保存变量当前的值,线程将变量的值copy到自己内存中,效率高。但是有时一个线程修改了一个变量的值但还没有写回主存,此时另一个线程中读到的自己内存中的值就是不对的。所以用 volatile修饰的变量就不采用“主内存”机制,而是在所有线程中同步地获得值。性能中有所消耗。 synchronized则在volatile基础上增加了 “锁”的概念,线程获得或者释放锁。它修饰方法或块。

4> static 语句块:
这是在www.jguru.com  上搜索到的介绍:
The primary use of static initializers blocks are to do various bits of initialization that may not be appropriate inside a constructor such that taken together the constructor and initializers put the newly created object into a state that is completely consistent for use.

In contrast to constructors, for example, static initializers aren't inherited and are only executed once when the class is loaded and initialized by the JRE. In the example above, the class variable foo will have the value 998877 once initialization is complete.

Note also that static initializers are executed in the order in which they appear textually in the source file. Also, there are a number of restrictions on what you can't do inside one of these blocks such as no use of checked exceptions, no use of the return statement or the this and super keywords.

5> native 作用: http://blog.chinaunix.net/u/21790/showart.php?id=285716 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值