??xml version="1.0" encoding="utf-8" standalone="yes"?>国产在线导航,青青草视频免费在线观看,欧美精品在线一区二区三区http://www.aygfsteel.com/aoneany/category/42806.htmlƲ上天堂Q先下地?/description>zh-cnMon, 30 Nov 2009 03:53:39 GMTMon, 30 Nov 2009 03:53:39 GMT60cd信息-Thinking in javaMW记http://www.aygfsteel.com/aoneany/articles/304119.html沙漠中的?/dc:creator>沙漠中的?/author>Sun, 29 Nov 2009 11:23:00 GMThttp://www.aygfsteel.com/aoneany/articles/304119.htmlhttp://www.aygfsteel.com/aoneany/comments/304119.htmlhttp://www.aygfsteel.com/aoneany/articles/304119.html#Feedback0http://www.aygfsteel.com/aoneany/comments/commentRss/304119.htmlhttp://www.aygfsteel.com/aoneany/services/trackbacks/304119.htmlClass.forName("Gum");

forName是取得Class引用的一U方法,q回一个Class对象的引用?/p>

如果Gumc还没有被加载就加蝲它,在加载过E中QGum的静态子句被执行?/p>

可能产生的异常:ClassNotFoundException

Class.getInterfaces("Gum")

q回对象是ClasscdQ表C类Gum包含的接?/p>

如类Gum implements interface1,

则会获取到interface1的类对象

Class.newInstance()

实现“虚拟构造器”的一U途径

 

cd面常?/strong>

obj.Class生成对Class对象的引用,它比forName更加安全Q因为它在编译时接受检查?/p>

注意:当?Class来创建Class对象的引用时Q不会自动初始化Class对象?/p>

class Initable{
    
static final int staticFinal=47;
    
static final int staticFinal2=ClassInitialization.rand.nextInt(1000);
    
static{
        System.out.println(
"Initialzing Initable");
    }

}

class Initable2{
    
static int staticNonFinal=147;
    
static{
        System.out.println(
"Initialzing Initable2");
    }

}

class Initable3{
    
static int staticNonFinal=74;
    
static{
        System.out.println(
"Initialzing Initable3");
    }

}

public class ClassInitialization {
    
public static Random rand=new Random(47);
    
public static void main(String[] args){
        Class initable
=Initable.class;
        System.out.println(
"After creating Initable ref");
        System.out.println(Initable.staticFinal);
        System.out.println(Initable.staticFinal2);
        System.out.println(Initable2.staticNonFinal);
        Class initable3
=Class.forName("Initable3");
        System.out.println(
"After creating Initable3 ref");
        System.out.println(Initable3.staticNonFinal);
    }

}

 

输出l果为:

Initialzing Initable
After creating Initable ref
47
258
Initialzing Initable2
147
Initialzing Initable3
After creating Initable3 ref
74

l论Q?/strong>如果一个static final值是"~译期常?Q就象Initable.staticFinal那样Q那么这个g需要对Initablecd使化可以读取,但是如果只是一个域讄为static和final的,如对Initable.staticFinal2的访问将q行强制的初使化Q因为它不是一个编译型帔R?/p>

如果一个static而不是final的,那么在它讉KӞL要求q行链接Qؓq个域分配存储空_和初始化Q初始化该存储空_Q就像对Initable2.staticNonFinal那样?/p>

泛化的Class引用

Class<Integer> iniClass=int.class

新的转型语法

case()

InstanceOf

反射

getMethods()q回Method对象的数l?/p>

getConstructors()q回Contructor对象的数l?/p>

动态代?/strong>

实现InvocationHandler接口

public Object invoke(Objct proxy,Method method,Object[] args)throws Throwable{}Ҏ

通过Proxy.newProxyInstance(ClassLoader,Class[],InvocationHandler)创徏动态代?/p>

具体CZ参见:

http://www.aygfsteel.com/aoneany/articles/271019.html



]]>
字符?Thinking in JavaMW记http://www.aygfsteel.com/aoneany/articles/304020.html沙漠中的?/dc:creator>沙漠中的?/author>Sat, 28 Nov 2009 05:54:00 GMThttp://www.aygfsteel.com/aoneany/articles/304020.htmlhttp://www.aygfsteel.com/aoneany/comments/304020.htmlhttp://www.aygfsteel.com/aoneany/articles/304020.html#Feedback0http://www.aygfsteel.com/aoneany/comments/commentRss/304020.htmlhttp://www.aygfsteel.com/aoneany/services/trackbacks/304020.htmlString对象是不可变?/p>

Scanner扫描字符串对象?/p>

备注Q?/p>

在java~译好的class文g?有个区域UCؓConstant Pool,他是一个由数组l成的表,cd
为cp_info constant_pool[],用来存储E序中用的各种帔R,包括Class/String/Integer{各
U基本Java数据cd,详情参见The Java Virtual Machine Specification
4.4章节.

对于Constant Pool,表的基本通用l构?
cp_info {
        u1 tag;
        u1 info[];
}

tag是一个数?用来表示存储的常量的cd,例如8表示Stringcd,5表示Longcd,info[]Ҏ
cd码tag的不同会发生相应变化.
对于Stringcd,表的l构?
CONSTANT_String_info {
        u1 tag;
        u2 string_index;
}
tag固定?,string_index是字W串内容信息,cd?
CONSTANT_Utf8_info {
        u1 tag;
        u2 length;
        u1 bytes[length];
}
tag固定?,length为字W串的长?bytes[length]为字W串的内?

代码样例
(以下代码在jdk6中编?
Z详细理解Constant Pool的结?我们参看一些代?
    String s1
= "sss111";
    String s2
= "sss222";
    System.out.println(s1
+ " " + s2);
׃
"sss111"?/span>"sss222"都是字符串常?在编译期已l创建好了存储在class文g?
在编译后的class文g中会存在q?个常量的对应表示:
08 00 11 01 00 06 73 73 73 31 31 31 08 00 13 01 ; ......sss111....
00 06 73 73 73 32 32 32                         ; ..sss222

Ҏ上面说的String帔Rl构,我们分析一?br /> 开始的08为CONSTANT_String_infol构中的tag,?1应该是它的相对引?01?br /> CONSTANT_Utf8_info的tag,06为对应字W串的长?
73 73 73 31 31 31为字W串?br /> 应的~码,接着分析,会发现后面的是对?/span>"sss222"的存储结?

l过上面分析,我们知道?1?3是两个字W串的相对引?可以修改class文g
来修Ҏ印的内容,把class文g中的
00 6E 00 04 00 03 00 00 00 24 12 10 4C 12 12 4D
Ҏ
00 6E 00 04 00 03 00 00 00 24 12 10 4C 12 10 4D
E序׃输出sss111 sss111,而不是和原程序一栯出sss111 sss222,因ؓ?br /> 们把?/span>"sss222"的相对引?2Ҏ了对"sss111"的相对引?0.

------------分割U?br /> public class Test {
   
public static void main(String[] args) {
        String s1
= "sss111";
        String s2
= "sss111";
    }
}

在上面程序中存在2个相同的帔R
"sss111",对于n个值相同的String帔R,在Constant Pool?br /> 只会创徏一?所以在~译好的class文g?我们只能扑ֈ一个对"sss111"的表C?
000000abh:
08 00 11 01 00 06 73 73 73 31 31 31             ; ......sss111

在程序执行的时?Constant Pool会储存在Method Area,而不是heap?

另外,对于
""内容为空的字W串帔R,会创Z个长度ؓ0,内容为空的字W串攑ֈConstant Pool?
而且Constant Pool在运行期是可以动态扩展的.

关于Stringcȝ说明

1.String使用private final char value[]来实现字W串的存?也就是说String对象创徏之后,׃?br /> 再修Ҏ对象中存储的字符串内?是因ؓ如此,才说Stringcd是不可变?immutable).

2.StringcL一个特D的创徏Ҏ,是使用""双引h创徏.例如new String("i am")实际创徏??br /> String对象,一个是"i am"通过""双引号创建的,另一个是通过new创徏?只不q他们创建的时期不同,
一个是~译?一个是q行?/span>!

3.java对Stringcd重蝲?/span>+操作W?可以直接使用+对两个字W串q行q接.

4.q行期调用Stringcȝintern()Ҏ可以向String Pool中动态添加对?

String的创建方?/strong>

一般有如下几种
1.直接使用""引号创徏.
2.使用new String()创徏.
3.使用new String("someString")创徏以及其他的一些重载构造函数创?
4.使用重蝲的字W串q接操作W?/span>+创徏.

面试?

String s1 = new String("s1") ;
String s2
= new String("s1") ;
上面创徏了几个String对象
?
{案:3?,~译期Constant Pool中创??q行期heap中创??




]]>
注释-thinking in java(MW记)http://www.aygfsteel.com/aoneany/articles/303405.html沙漠中的?/dc:creator>沙漠中的?/author>Mon, 23 Nov 2009 16:01:00 GMThttp://www.aygfsteel.com/aoneany/articles/303405.htmlhttp://www.aygfsteel.com/aoneany/comments/303405.htmlhttp://www.aygfsteel.com/aoneany/articles/303405.html#Feedback0http://www.aygfsteel.com/aoneany/comments/commentRss/303405.htmlhttp://www.aygfsteel.com/aoneany/services/trackbacks/303405.html注释为特D的interfacecdQ他以interface前面加一个@W号Q他可以定义在Q何地方(c,属性,ҎQ?/p>

@Target(ElementType.TYPE)Q可选)
@interface ClassInfo {
    String created() default "Jan 31 2005";
    String createdBy();
    String lastModified();
    String lastModifiedBy();
    Revision revision();
}
ElementType.TYPE描绘支持的范?br /> ANNOTATION_TYPE, CONSTRUCTOR, METHOD, FIELD, LOCAL_VARIABLE, PARAMETER, PACKAGE, and TYPE
他支持定义默认?br /> 它可以有0个元?/p>

@ClassInfo (
    created = "Jan 31 2005",
    createdBy = "James Gosling",
    lastModified = "Jan 31 2005",
    lastModifiedBy = "James Gosling",
    revision = @Revision
)
public class Foo {
    // ...
}



]]>
初化和清除-thinking in javaMW记http://www.aygfsteel.com/aoneany/articles/303251.html沙漠中的?/dc:creator>沙漠中的?/author>Sun, 22 Nov 2009 11:08:00 GMThttp://www.aygfsteel.com/aoneany/articles/303251.htmlhttp://www.aygfsteel.com/aoneany/comments/303251.htmlhttp://www.aygfsteel.com/aoneany/articles/303251.html#Feedback0http://www.aygfsteel.com/aoneany/comments/commentRss/303251.htmlhttp://www.aygfsteel.com/aoneany/services/trackbacks/303251.htmlfinalize()

工作原理应该是这LQ一旦垃圾收集器准备好释攑֯象占用的存储I间Q它首先调用finalize()Q而且只有在下一ơ垃圾收集过E中Q才会真正回收对象的内存?/p>

Z么要使用finalize()Ҏ

因ؓjava内存回收之能回收自己java自己创徏的对象,而对调用未java的对象,如C,C++创徏的对象,不能自动的清除,可以通过在finalize()ҎQ将q些对象昄的清除?/p>

finalize()最有用处的地方之一是观察垃圾收集的q程?/p>

class Chair {
  
static boolean gcrun = false;
  
static boolean f = false;
  
static int created = 0;
  
static int finalized = 0;
  
int i;
  Chair() 
{
    i 
= ++created;
    
if(created == 47
      System.out.println(
"Created 47");
  }

  
protected void finalize() {
    
if(!gcrun) {
      gcrun 
= true;
      System.out.println(
        
"Beginning to finalize after " +
        created 
+ " Chairs have been created");
    }

    
if(i == 47{
      System.out.println(
        
"Finalizing Chair #47, " +
        
"Setting flag to stop Chair creation");
      f 
= true;
    }

    finalized
++;
    
if(finalized >= created)
      System.out.println(
        
"All " + finalized + " finalized");
  }

}


public class Garbage {
  
public static void main(String[] args) {
    
if(args.length == 0{
      System.err.println(
"Usage: \n" +
        
"java Garbage before\n  or:\n" +
        
"java Garbage after");
      
return;
    }

    
while(!Chair.f) {
      
new Chair();
      
new String("To take up space");
    }

    System.out.println(
      
"After all Chairs have been created:\n" +
      
"total created = " + Chair.created +
      
", total finalized = " + Chair.finalized);
    
if(args[0].equals("before")) {
      System.out.println(
"gc():");
      System.gc();
      System.out.println(
"runFinalization():");
      System.runFinalization();
    }

    System.out.println(
"bye!");
    
if(args[0].equals("after"))
      System.runFinalizersOnExit(
true);
  }

}
 ///:~

输入l果为:

Created 47
Beginning to finalize after 8694 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 9834, total finalized = 108
bye!
成员初?/strong>
可以Ҏ赋值初使化成员
下面q样做是合法的:

class CInit {
int i = f();
int j = g(i);
//...
}

但下面这样做是非法的Q?br />
class CInit {
int j = g(i);
int i = f();
//...
}
构造器初?/strong>
class Counter {
int i;
Counter() { i = 7; }
i首先会初始化成零Q然后变??/pre>
1.初化顺?/strong>
先初使化成员Q然后执行构造器内容
2.静态数据初使化
 
public class Dog {
    
public static Dog sDog=new Dog("static dog");
    
public Dog(String str){
        System.out.println(str);
    }

    
public static void main(String[] args){
        Dog dog
=new Dog("init");
    }

}
输入出结?/pre>
static dog
init

(1) cd为Dog的一个对象首ơ创建时Q或者DogcȝstaticҎQstatic字段首次讉KӞJava解释器必L到Dog.classQ在事先讑֥的类路径里搜索)?br /> (2) 扑ֈDog.class后(它会创徏一个Class对象Q这在后面学到Q,它的所有static初始化模块都会运行。因此,static初始化仅发生一ơ——在Class对象首次载入的时候?br /> (3) 创徏一个new Dog()ӞDog对象的构E首先会在内存堆QHeapQ里Z个Dog对象分配_多的存储I间?br /> (4) q种存储I间会清为零Q将Dog中的所有基本类型设为它们的默认|零用于数字,以及boolean和char的等仯定)?br /> (5) q行字段定义时发生的所有初始化都会执行?br /> (6) 执行构徏器?/p>

3.明确q行的静态初始化

class Spoon {  
   
static int i;  
   
static {    
     i 
= 47;  
   }

}
q段代码仅执行一ơ——首ơ生成那个类的一个对象时Q或者首ơ访问属于那个类的一个static成员?/pre>
4.非静态实例的初始?/strong>
class Mug {
  Mug(
int marker) {
    System.out.println(
"Mug(" + marker + ")");
  }

}


public class Mugs {
  Mug c1;
  
{
    c1 
= new Mug(1);
    System.out.println(
"c1 initialized");
  }

  Mugs() 
{
    System.out.println(
"Mugs()");
  }

  
public static void main(String[] args) {
    System.out.println(
"Inside main()");
    Mugs x 
= new Mugs();
  }

}

输入出结?/pre>
Inside main()
Mug(1)
c1 initialized
Mugs()
 


]]> վ֩ģ壺 ϼ| | | | ʵ| ͸| | | | | | ų| | | ƽ| | ո| | | | ƽ| | | | | | | | | | | | Ƥ| | ƽ| | | | ƽң| ̨| |