2006年6月22日

public   class  AutoBoxing  {
    
    
/**  Creates a new instance of AutoBoxing  */
    
public  AutoBoxing()  {
    }

    
public   void  boxingDemo() {
        
// auto boxing
        Integer i  =   0 ;
        
float  f  =   1.66f ;
        Float F 
=  f;
        
// auto unboxing
        Integer I  =   new  Integer( 1 );
        
int  i2  =  I;
        
// null value test, it will case NullPointerException
        Integer I2  =   null ;
        
int  i3  =  I2;
    }

    
public   void  testOperator() {
        Integer i 
=   1 ;
        
while ( true ) {
            i
++ ;
            System.out.println(
" Counter: " + i);
            
if (i > 5 break ;
        }

    }

    
public   void  testCompare() {
        
// it's equal because -127~127 are immutable objects
        Integer i  =   1 ;
        Integer i2 
=   1 ;
        
if (i  ==  i2) System.out.println( " 1:Equal " );
        
else  System.out.println( " 1:Not Equal " );
        
// it's not equal because j and j2 are different objects
        Integer j  =   200 ;
        Integer j2 
= 200 ;
        
if (j  ==  j2) System.out.println( " 200:Equal " );
        
else  System.out.println( " 200:Not Equal " );
    }

    
public   void  testControl() {
        Boolean flag 
=   true ;
        Integer i 
=   20 ;
        Integer j 
=   30 ;
        
if (flag) {
            System.out.println(
" Boolean affects " );
        }

        
if (i < j)
            System.out.println(
" Integer affects " );
    }

    
public   void  testMethod( double  arg) {
        System.out.println(
" public void testMethod(double arg) is invoked " );
    }

    
public   void  testMethod(Integer arg) {
        System.out.println(
" public void testMethod2(Integer arg) is invoked " );
    }

    
public   static   void  main(String args[]) {
        AutoBoxing auto 
=   new  AutoBoxing();
        auto.testCompare();
        auto.testOperator();
        auto.testControl();
        
int  i  =   1 ;
        
//  public void testMethod(Integer arg) wouldn't be invoked
        
// because  public void testMethod(double arg) will be invoked in JDK1.4
        
// Java tiger consider the backward capability
        auto.testMethod(i);
        auto.boxingDemo();
    }

    
}

posted @ 2006-06-22 18:34 BlueO2 阅读(184) | 评论 (0)编辑 收藏

public   enum  User  {
    Admin,User,Guest,Unknown   
}

public   class  Login  {
    
    
private  User user;
    EnumMap
< User,String >  userName  =   new  EnumMap < User, String > (User. class );
    
/**  Creates a new instance of Login  */
    
public  Login()  {
        userName.put(User.Admin, 
" Administrator " );
        userName.put(User.User, 
" David " );
        userName.put(User.Guest, 
" Steve " );
    }

    
public   boolean  isAdmin(User user) {
        
if (user.equals(User.Admin)) {
            
return   true ;
        }

        
return   false ;
    }

    
public   void  printUserRole() {
        User[] users 
=  user.values();
        
for (User u : user.values()) {
            System.out.println(u.toString());
        }

    }

    
public   void  isRole(User user) {
        
switch (user) {
            
case  Admin:
                System.out.println(
" admin " );
                
break ;
            
case  User:
                System.out.println(
" User " );
                
break ;
            
case  Guest:
                System.out.println(
" Guest " );
                
break ;
            
default :
                System.out.println(
" unknow " );
        }

    }

    
public   static   void  main(String[] args) {
        Login login 
=   new  Login();
        System.out.println(login.isAdmin(User.Admin));
        login.printUserRole();
        login.isRole(User.User);
    }

    
}

posted @ 2006-06-22 18:22 BlueO2 阅读(136) | 评论 (0)编辑 收藏


posts - 29, comments - 3, trackbacks - 0, articles - 0

Copyright © BlueO2