1. 程式人生 > >易學筆記--從0開始學JAVA(個人純手工筆記共享 免費!免費!免費!)--第5章 初始化和清理

易學筆記--從0開始學JAVA(個人純手工筆記共享 免費!免費!免費!)--第5章 初始化和清理

  1. 引數數量
  2. 引數型別
  1. 原始碼:
    //: HelloDate.java
    import java.util.*;
    //import static net.mindview.until.Print.*;
    /** the first Thinking in java example program
      * display a string and today's date
      * @author wengyongsheng 
      * @version 4.0 
    */
    public class HelloDate{
    	/**
    	* @param args array of string arguments
    	*/
      public static void main(String []args) {
      System.out.println("Hello,It's: ");
      System.out.println(new Date());
    //  print("Hello,It's: ");
    }
    }
    
    class Tank{
    	 int level;
    }
    
    class Assignment{
    	public static void main(String []args) {
    		Tank t1 = new Tank();
    		Tank t2 = new Tank();
    		t1.level = 9;
    		t2.level = 47;
    		System.out.println("1:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
        t1 = t2;
        System.out.println("2:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
    		
    		t1.level = 27;
    		System.out.println("3:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);                   		                   
    	}
    }
    
    class Letter{
    	 char c;
    }
    
    class PassObject{
    	static void f(Letter y){
    		y.c = 'z';
    	}
    	public static void main(String []args) {
    		Letter x = new Letter();
    		System.out.println("1:x.c= " + x.c);
    		x.c = 'a';
    		System.out.println("2:x.c= " + x.c);
    		f(x);
    		System.out.println("3:x.c= " + x.c);
    	}
    }
    
    class MathOps{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		int i , j , k;
    		j = rand.nextInt(100) + 1;
    		System.out.println("j = " + j);
    		k = rand.nextInt(100) + 1;
    		System.out.println("k = " + k);
    		i = j + k;
    		System.out.println("i = " + i);
    		
    		float u , v , w;
    		u = rand.nextFloat();
    		System.out.println("u = " + u);
    		v = rand.nextFloat() ;
    		System.out.println("v = " + v);
    		w = u + v;
    		System.out.println("w = " + w);
    	}
    }
    
    class AutoInc{
    	public static void main(String []args) {
    		int i = 1;
    		System.out.println("i = " + i);
    		System.out.println("++i = " + ++i);
    		System.out.println("i++ = " + i++);
    		System.out.println("i2 = " + i);
    		System.out.println("--i = " + --i);
    		System.out.println("i-- = " + i--);
    		System.out.println("i3 = " + i);
     }
    }
    /* output
    i = 1
    ++i = 2
    i++ = 2
    i2=3
    --i = 2
    i-- = 2
    i3=1
    */
    
    class Equaivalence{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1 == n2);
    		System.out.println(n1 != n2);
     }
    }
    
    class EqualsMethod{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1.equals(n2));
     }
    }
    
    class Value{
    	int i ;
    }
    class EqualsMethod2{
    	public static void main(String []args) {
    		Value v1 = new Value();
    		Value v2 = new Value();
    	 System.out.println(v1.equals(v2));
     }
    }
    
    class ShortCircuit{
    	static boolean test1(int val){
    		System.out.println("test1( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 1);
    	}
    	
    	static boolean test2(int val){
    		System.out.println("test2( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 2);
    	}
    	
    	static boolean test3(int val){
    		System.out.println("test3( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 3);
    	}
    	
    	
    	public static void main(String []args) {
    		
    		boolean b = test1(0) && test2(2) && test2(3);
    		System.out.println("expression is : "  + b );
    	}
    }
    
    class CommaOperator{
    	public static void main(String []args) {
    		for(int i = 1, j = i + 10; i < 5;i++,j = i*2){
    			System.out.println("i =  "  +  i + " j = "  + j);
    		}
     }
    }
    
    
    class ForEachFloat{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		float f[] = new float[10];
    		for(int i = 0; i < 10;i++){
    			f[i] = rand.nextFloat();
    		}
    		
    		for(float x:f){
    			System.out.println("x =  " + x);
    		}
    		
    		for(char c:"my name".toCharArray()){
    			System.out.println("c =  " + c);
    		}
     }
    }
    
    class LableedWhile{
    	public static void main(String []args) {
    		int i = 0;
    		outer:
    		while(true){
    			System.out.println("Outer while loop");
    			while(true){
    				i++;
    				System.out.println("i = " + i);
    				if(i == 1){
    					System.out.println("continue");
    					continue;
    				}
    				if(i == 3){
    					System.out.println("continue Outer");
    					continue outer;
    				}
    				if(i == 5){
    					System.out.println("break");
    					break;
    				}
    				if(i == 7){
    					System.out.println("break Outer");
    					break outer;
    				}
    			}
    		}
     }
    }
    
    class VoewlsAndConson{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		for(int i = 0; i < 100;i++){
    			int c = rand.nextInt(26) + 'a';
    			System.out.println("c = " + c);
    			switch(c){
    				case 'a':
    				case 'b':
    				case 'c': System.out.println("vowel");break;
    				case 'y':
    				case 'w':
    				case 'z':System.out.println("consonant");break;
    				default:System.out.println("xxxx");
    			}
    		}
     }
    }
    
    class Flower{
    	int petalCount = 0;
    	String s = "init value";
    	
    	Flower(int peta){
    		petalCount = peta;
    		System.out.println("Flower(int peta)" );
    	}
    	
    	Flower(String ss){
    		s = ss;
    		System.out.println("Flower(String ss) " );
    	}
    	
    	
    	Flower(String s,int patails){
    		this(patails);
    		this.s = s;
    		System.out.println("Flower(String s,int patails) ");
    	}
    	
    	Flower(){
    		this("xxx",1);
    		System.out.println("Flower() ");
    	}
    	public static void main(String []args) {
    		Flower x = new Flower();
    		
    	}
    }
  2. 輸出結果:

  1. 原始碼:
    //: HelloDate.java
    import java.util.*;
    //import static net.mindview.until.Print.*;
    /** the first Thinking in java example program
      * display a string and today's date
      * @author wengyongsheng 
      * @version 4.0 
    */
    public class HelloDate{
    	/**
    	* @param args array of string arguments
    	*/
      public static void main(String []args) {
      System.out.println("Hello,It's: ");
      System.out.println(new Date());
    //  print("Hello,It's: ");
    }
    }
    
    class Tank{
    	 int level;
    }
    
    class Assignment{
    	public static void main(String []args) {
    		Tank t1 = new Tank();
    		Tank t2 = new Tank();
    		t1.level = 9;
    		t2.level = 47;
    		System.out.println("1:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
        t1 = t2;
        System.out.println("2:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
    		
    		t1.level = 27;
    		System.out.println("3:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);                   		                   
    	}
    }
    
    class Letter{
    	 char c;
    }
    
    class PassObject{
    	static void f(Letter y){
    		y.c = 'z';
    	}
    	public static void main(String []args) {
    		Letter x = new Letter();
    		System.out.println("1:x.c= " + x.c);
    		x.c = 'a';
    		System.out.println("2:x.c= " + x.c);
    		f(x);
    		System.out.println("3:x.c= " + x.c);
    	}
    }
    
    class MathOps{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		int i , j , k;
    		j = rand.nextInt(100) + 1;
    		System.out.println("j = " + j);
    		k = rand.nextInt(100) + 1;
    		System.out.println("k = " + k);
    		i = j + k;
    		System.out.println("i = " + i);
    		
    		float u , v , w;
    		u = rand.nextFloat();
    		System.out.println("u = " + u);
    		v = rand.nextFloat() ;
    		System.out.println("v = " + v);
    		w = u + v;
    		System.out.println("w = " + w);
    	}
    }
    
    class AutoInc{
    	public static void main(String []args) {
    		int i = 1;
    		System.out.println("i = " + i);
    		System.out.println("++i = " + ++i);
    		System.out.println("i++ = " + i++);
    		System.out.println("i2 = " + i);
    		System.out.println("--i = " + --i);
    		System.out.println("i-- = " + i--);
    		System.out.println("i3 = " + i);
     }
    }
    /* output
    i = 1
    ++i = 2
    i++ = 2
    i2=3
    --i = 2
    i-- = 2
    i3=1
    */
    
    class Equaivalence{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1 == n2);
    		System.out.println(n1 != n2);
     }
    }
    
    class EqualsMethod{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1.equals(n2));
     }
    }
    
    class Value{
    	int i ;
    }
    class EqualsMethod2{
    	public static void main(String []args) {
    		Value v1 = new Value();
    		Value v2 = new Value();
    	 System.out.println(v1.equals(v2));
     }
    }
    
    class ShortCircuit{
    	static boolean test1(int val){
    		System.out.println("test1( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 1);
    	}
    	
    	static boolean test2(int val){
    		System.out.println("test2( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 2);
    	}
    	
    	static boolean test3(int val){
    		System.out.println("test3( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 3);
    	}
    	
    	
    	public static void main(String []args) {
    		
    		boolean b = test1(0) && test2(2) && test2(3);
    		System.out.println("expression is : "  + b );
    	}
    }
    
    class CommaOperator{
    	public static void main(String []args) {
    		for(int i = 1, j = i + 10; i < 5;i++,j = i*2){
    			System.out.println("i =  "  +  i + " j = "  + j);
    		}
     }
    }
    
    
    class ForEachFloat{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		float f[] = new float[10];
    		for(int i = 0; i < 10;i++){
    			f[i] = rand.nextFloat();
    		}
    		
    		for(float x:f){
    			System.out.println("x =  " + x);
    		}
    		
    		for(char c:"my name".toCharArray()){
    			System.out.println("c =  " + c);
    		}
     }
    }
    
    class LableedWhile{
    	public static void main(String []args) {
    		int i = 0;
    		outer:
    		while(true){
    			System.out.println("Outer while loop");
    			while(true){
    				i++;
    				System.out.println("i = " + i);
    				if(i == 1){
    					System.out.println("continue");
    					continue;
    				}
    				if(i == 3){
    					System.out.println("continue Outer");
    					continue outer;
    				}
    				if(i == 5){
    					System.out.println("break");
    					break;
    				}
    				if(i == 7){
    					System.out.println("break Outer");
    					break outer;
    				}
    			}
    		}
     }
    }
    
    class VoewlsAndConson{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		for(int i = 0; i < 100;i++){
    			int c = rand.nextInt(26) + 'a';
    			System.out.println("c = " + c);
    			switch(c){
    				case 'a':
    				case 'b':
    				case 'c': System.out.println("vowel");break;
    				case 'y':
    				case 'w':
    				case 'z':System.out.println("consonant");break;
    				default:System.out.println("xxxx");
    			}
    		}
     }
    }
    
    class Flower{
    	int petalCount = 0;
    	String s = "init value";
    	
    	Flower(int peta){
    		petalCount = peta;
    		System.out.println("Flower(int peta)" );
    	}
    	
    	Flower(String ss){
    		s = ss;
    		System.out.println("Flower(String ss) " );
    	}
    	
    	
    	Flower(String s,int patails){
    		this(patails);
    		this.s = s;
    		System.out.println("Flower(String s,int patails) ");
    	}
    	
    	Flower(){
    		this("xxx",1);
    		System.out.println("Flower() ");
    	}
    	public static void main(String []args) {
    		Flower x = new Flower();
    		
    	}
    }
    
    
    class Book{
     boolean checkedOut = false;
     Book(boolean checkedOut){
     	this.checkedOut = checkedOut;
     }
     void checkIn(){
     	checkedOut = false;
     }
     protected void finalize(){
     	if(checkedOut){
     		System.out.println(" protected void finalize:error ");
     	}
    }
    }
    
    class Tcondition{
    	public static void main(String []args) {
    		Book novel = new Book(true);
    		novel.checkIn();
    		new Book(true);
    		System.gc(); //進行強制終結動作
    	}
    }
  2. 輸出結果:
  1. 舉例:
  1. 格式:
  2. 舉例:
  3. static宣告的物件,如果是在子句中,還是允許再次初始化
    1. class StaticTests{
      
           public  int i = 0;
      
           
      
           public static StaticTest  staticTest1 = new StaticTest(400);
      
           public static StaticTest  staticTest2 = new StaticTest(500);
      
           {
      
                 staticTest1 = new StaticTest(200);
      
                 System.out.println("i="+i);
      
                 
      
                 staticTest2 = new StaticTest(300);
      
                 System.out.println("i="+i);
      
                 
      
           }
      
           
      
           static StaticTest staticTest = new StaticTest(100);
      
           
      
           StaticTests(){
      
                 System.out.println("StaticTests初始化");
      
           }
      
           
      
           StaticTests(int i ){
      
                 this.i = i;
      
                 System.out.println("StaticTests初始化,i=" + i);
      
           }
      
           
      
           public static void main(String[] args) {
      
                 StaticTests staticTest1 = new StaticTests();
      
                 StaticTests staticTest2 = new StaticTests();
      
           }
      
      }
  1. 舉例:
    1. 原始碼:
      //: HelloDate.java
      import java.util.*;
      //import static net.mindview.until.Print.*;
      /** the first Thinking in java example program
        * display a string and today's date
        * @author wengyongsheng 
        * @version 4.0 
      */
      public class HelloDate{
      	/**
      	* @param args array of string arguments
      	*/
        public static void main(String []args) {
        System.out.println("Hello,It's: ");
        System.out.println(new Date());
      //  print("Hello,It's: ");
      }
      }
      
      class Tank{
      	 int level;
      }
      
      class Assignment{
      	public static void main(String []args) {
      		Tank t1 = new Tank();
      		Tank t2 = new Tank();
      		t1.level = 9;
      		t2.level = 47;
      		System.out.println("1:t1.level = " + t1.level + 
      		                   " t2.level = " + t2.level);
          t1 = t2;
          System.out.println("2:t1.level = " + t1.level + 
      		                   " t2.level = " + t2.level);
      		
      		t1.level = 27;
      		System.out.println("3:t1.level = " + t1.level + 
      		                   " t2.level = " + t2.level);                   		                   
      	}
      }
      
      class Letter{
      	 char c;
      }
      
      class PassObject{
      	static void f(Letter y){
      		y.c = 'z';
      	}
      	public static void main(String []args) {
      		Letter x = new Letter();
      		System.out.println("1:x.c= " + x.c);
      		x.c = 'a';
      		System.out.println("2:x.c= " + x.c);
      		f(x);
      		System.out.println("3:x.c= " + x.c);
      	}
      }
      
      class MathOps{
      	public static void main(String []args) {
      		Random rand = new Random(47);
      		int i , j , k;
      		j = rand.nextInt(100) + 1;
      		System.out.println("j = " + j);
      		k = rand.nextInt(100) + 1;
      		System.out.println("k = " + k);
      		i = j + k;
      		System.out.println("i = " + i);
      		
      		float u , v , w;
      		u = rand.nextFloat();
      		System.out.println("u = " + u);
      		v = rand.nextFloat() ;
      		System.out.println("v = " + v);
      		w = u + v;
      		System.out.println("w = " + w);
      	}
      }
      
      class AutoInc{
      	public static void main(String []args) {
      		int i = 1;
      		System.out.println("i = " + i);
      		System.out.println("++i = " + ++i);
      		System.out.println("i++ = " + i++);
      		System.out.println("i2 = " + i);
      		System.out.println("--i = " + --i);
      		System.out.println("i-- = " + i--);
      		System.out.println("i3 = " + i);
       }
      }
      /* output
      i = 1
      ++i = 2
      i++ = 2
      i2=3
      --i = 2
      i-- = 2
      i3=1
      */
      
      class Equaivalence{
      	public static void main(String []args) {
      		Integer n1 = new Integer(47);
      		Integer n2 = new Integer(47);
      		
      		System.out.println(n1 == n2);
      		System.out.println(n1 != n2);
       }
      }
      
      class EqualsMethod{
      	public static void main(String []args) {
      		Integer n1 = new Integer(47);
      		Integer n2 = new Integer(47);
      		
      		System.out.println(n1.equals(n2));
       }
      }
      
      class Value{
      	int i ;
      }
      class EqualsMethod2{
      	public static void main(String []args) {
      		Value v1 = new Value();
      		Value v2 = new Value();
      	 System.out.println(v1.equals(v2));
       }
      }
      
      class ShortCircuit{
      	static boolean test1(int val){
      		System.out.println("test1( " + val + " )");
      		System.out.println("result: "  + (val <1) );
      		return (val < 1);
      	}
      	
      	static boolean test2(int val){
      		System.out.println("test2( " + val + " )");
      		System.out.println("result: "  + (val <1) );
      		return (val < 2);
      	}
      	
      	static boolean test3(int val){
      		System.out.println("test3( " + val + " )");
      		System.out.println("result: "  + (val <1) );
      		return (val < 3);
      	}
      	
      	
      	public static void main(String []args) {
      		
      		boolean b = test1(0) && test2(2) && test2(3);
      		System.out.println("expression is : "  + b );
      	}
      }
      
      class CommaOperator{
      	public static void main(String []args) {
      		for(int i = 1, j = i + 10; i < 5;i++,j = i*2){
      			System.out.println("i =  "  +  i + " j = "  + j);
      		}
       }
      }
      
      
      class ForEachFloat{
      	public static void main(String []args) {
      		Random rand = new Random(47);
      		float f[] = new float[10];
      		for(int i = 0; i < 10;i++){
      			f[i] = rand.nextFloat();
      		}
      		
      		for(float x:f){
      			System.out.println("x =  " + x);
      		}
      		
      		for(char c:"my name".toCharArray()){
      			System.out.println("c =  " + c);
      		}
       }
      }
      
      class LableedWhile{
      	public static void main(String []args) {
      		int i = 0;
      		outer:
      		while(true){
      			System.out.println("Outer while loop");
      			while(true){
      				i++;
      				System.out.println("i = " + i);
      				if(i == 1){
      					System.out.println("continue");
      					continue;
      				}
      				if(i == 3){
      					System.out.println("continue Outer");
      					continue outer;
      				}
      				if(i == 5){
      					System.out.println("break");
      					break;
      				}
      				if(i == 7){
      					System.out.println("break Outer");
      					break outer;
      				}
      			}
      		}
       }
      }
      
      class VoewlsAndConson{
      	public static void main(String []args) {
      		Random rand = new Random(47);
      		for(int i = 0; i < 100;i++){
      			int c = rand.nextInt(26) + 'a';
      			System.out.println("c = " + c);
      			switch(c){
      				case 'a':
      				case 'b':
      				case 'c': System.out.println("vowel");break;
      				case 'y':
      				case 'w':
      				case 'z':System.out.println("consonant");break;
      				default:System.out.println("xxxx");
      			}
      		}
       }
      }
      
      class Flower{
      	int petalCount = 0;
      	String s = "init value";
      	
      	Flower(int peta){
      		petalCount = peta;
      		System.out.println("Flower(int peta)" );
      	}
      	
      	Flower(String ss){
      		s = ss;
      		System.out.println("Flower(String ss) " );
      	}
      	
      	
      	Flower(String s,int patails){
      		this(patails);
      		this.s = s;
      		System.out.println("Flower(String s,int patails) ");
      	}
      	
      	Flower(){
      		this("xxx",1);
      		System.out.println("Flower() ");
      	}
      	public static void main(String []args) {
      		Flower x = new Flower();
      		
      	}
      }
      
      
      class Book{
       boolean checkedOut = false;
       Book(boolean checkedOut){
       	this.checkedOut = checkedOut;
       }
       void checkIn(){
       	checkedOut = false;
       }
       protected void finalize(){
       	if(checkedOut){
       		System.out.println(" protected void finalize:error ");
       	}
      }
      }
      
      class Tcondition{
      	public static void main(String []args) {
      		Book novel = new Book(true);
      		novel.checkIn();
      		new Book(true);
      		System.gc(); //進行強制終結動作
      	}
      }
      
      
      class ArrayClassObj{
      	public static void main(String []args) {
      		Random rand = new Random (46);
      		Integer []a = new Integer[rand.nextInt(20)];
      		System.out.println("a.length = " + a.length);
      		for(int i= 0; i < a.length;i++){
      			a[i] = rand.nextInt(500);
      		}
      		System.out.println(Arrays.toString(a));
      		
      	}
      }
    2. 輸出結果:
  1. 原始碼:
    //: HelloDate.java
    import java.util.*;
    //import static net.mindview.until.Print.*;
    /** the first Thinking in java example program
      * display a string and today's date
      * @author wengyongsheng 
      * @version 4.0 
    */
    public class HelloDate{
    	/**
    	* @param args array of string arguments
    	*/
      public static void main(String []args) {
      System.out.println("Hello,It's: ");
      System.out.println(new Date());
    //  print("Hello,It's: ");
    }
    }
    
    class Tank{
    	 int level;
    }
    
    class Assignment{
    	public static void main(String []args) {
    		Tank t1 = new Tank();
    		Tank t2 = new Tank();
    		t1.level = 9;
    		t2.level = 47;
    		System.out.println("1:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
        t1 = t2;
        System.out.println("2:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
    		
    		t1.level = 27;
    		System.out.println("3:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);                   		                   
    	}
    }
    
    class Letter{
    	 char c;
    }
    
    class PassObject{
    	static void f(Letter y){
    		y.c = 'z';
    	}
    	public static void main(String []args) {
    		Letter x = new Letter();
    		System.out.println("1:x.c= " + x.c);
    		x.c = 'a';
    		System.out.println("2:x.c= " + x.c);
    		f(x);
    		System.out.println("3:x.c= " + x.c);
    	}
    }
    
    class MathOps{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		int i , j , k;
    		j = rand.nextInt(100) + 1;
    		System.out.println("j = " + j);
    		k = rand.nextInt(100) + 1;
    		System.out.println("k = " + k);
    		i = j + k;
    		System.out.println("i = " + i);
    		
    		float u , v , w;
    		u = rand.nextFloat();
    		System.out.println("u = " + u);
    		v = rand.nextFloat() ;
    		System.out.println("v = " + v);
    		w = u + v;
    		System.out.println("w = " + w);
    	}
    }
    
    class AutoInc{
    	public static void main(String []args) {
    		int i = 1;
    		System.out.println("i = " + i);
    		System.out.println("++i = " + ++i);
    		System.out.println("i++ = " + i++);
    		System.out.println("i2 = " + i);
    		System.out.println("--i = " + --i);
    		System.out.println("i-- = " + i--);
    		System.out.println("i3 = " + i);
     }
    }
    /* output
    i = 1
    ++i = 2
    i++ = 2
    i2=3
    --i = 2
    i-- = 2
    i3=1
    */
    
    class Equaivalence{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1 == n2);
    		System.out.println(n1 != n2);
     }
    }
    
    class EqualsMethod{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1.equals(n2));
     }
    }
    
    class Value{
    	int i ;
    }
    class EqualsMethod2{
    	public static void main(String []args) {
    		Value v1 = new Value();
    		Value v2 = new Value();
    	 System.out.println(v1.equals(v2));
     }
    }
    
    class ShortCircuit{
    	static boolean test1(int val){
    		System.out.println("test1( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 1);
    	}
    	
    	static boolean test2(int val){
    		System.out.println("test2( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 2);
    	}
    	
    	static boolean test3(int val){
    		System.out.println("test3( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 3);
    	}
    	
    	
    	public static void main(String []args) {
    		
    		boolean b = test1(0) && test2(2) && test2(3);
    		System.out.println("expression is : "  + b );
    	}
    }
    
    class CommaOperator{
    	public static void main(String []args) {
    		for(int i = 1, j = i + 10; i < 5;i++,j = i*2){
    			System.out.println("i =  "  +  i + " j = "  + j);
    		}
     }
    }
    
    
    class ForEachFloat{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		float f[] = new float[10];
    		for(int i = 0; i < 10;i++){
    			f[i] = rand.nextFloat();
    		}
    		
    		for(float x:f){
    			System.out.println("x =  " + x);
    		}
    		
    		for(char c:"my name".toCharArray()){
    			System.out.println("c =  " + c);
    		}
     }
    }
    
    class LableedWhile{
    	public static void main(String []args) {
    		int i = 0;
    		outer:
    		while(true){
    			System.out.println("Outer while loop");
    			while(true){
    				i++;
    				System.out.println("i = " + i);
    				if(i == 1){
    					System.out.println("continue");
    					continue;
    				}
    				if(i == 3){
    					System.out.println("continue Outer");
    					continue outer;
    				}
    				if(i == 5){
    					System.out.println("break");
    					break;
    				}
    				if(i == 7){
    					System.out.println("break Outer");
    					break outer;
    				}
    			}
    		}
     }
    }
    
    class VoewlsAndConson{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		for(int i = 0; i < 100;i++){
    			int c = rand.nextInt(26) + 'a';
    			System.out.println("c = " + c);
    			switch(c){
    				case 'a':
    				case 'b':
    				case 'c': System.out.println("vowel");break;
    				case 'y':
    				case 'w':
    				case 'z':System.out.println("consonant");break;
    				default:System.out.println("xxxx");
    			}
    		}
     }
    }
    
    class Flower{
    	int petalCount = 0;
    	String s = "init value";
    	
    	Flower(int peta){
    		petalCount = peta;
    		System.out.println("Flower(int peta)" );
    	}
    	
    	Flower(String ss){
    		s = ss;
    		System.out.println("Flower(String ss) " );
    	}
    	
    	
    	Flower(String s,int patails){
    		this(patails);
    		this.s = s;
    		System.out.println("Flower(String s,int patails) ");
    	}
    	
    	Flower(){
    		this("xxx",1);
    		System.out.println("Flower() ");
    	}
    	public static void main(String []args) {
    		Flower x = new Flower();
    		
    	}
    }
    
    
    class Book{
     boolean checkedOut = false;
     Book(boolean checkedOut){
     	this.checkedOut = checkedOut;
     }
     void checkIn(){
     	checkedOut = false;
     }
     protected void finalize(){
     	if(checkedOut){
     		System.out.println(" protected void finalize:error ");
     	}
    }
    }
    
    class Tcondition{
    	public static void main(String []args) {
    		Book novel = new Book(true);
    		novel.checkIn();
    		new Book(true);
    		System.gc(); //進行強制終結動作
    	}
    }
    
    
    class ArrayClassObj{
    	public static void main(String []args) {
    		Random rand = new Random (46);
    		Integer []a = new Integer[rand.nextInt(20)];
    		System.out.println("a.length = " + a.length);
    		for(int i= 0; i < a.length;i++){
    			a[i] = rand.nextInt(500);
    		}
    		System.out.println(Arrays.toString(a));
    		
    	}
    }
    
    class VarArgs{
    	
    	static void printArray(Object [] args) {
    		for(Object obj : args){
    			System.out.println("obj = " + obj);
    		}
    	}
    	
    	public static void main(String []args) {
    		printArray(new Object[]
    		           {new Integer(10),
    		           	new Integer(20),
    		           	new Integer(30)});
    
        printArray(new Object[]
    		           {"one",
    		           	"two",
    		            "thress"});
    	}
    }
  2. 結果輸出:
  • JAVA SE5後新可變引數方法:
  • 原始碼:
    //: HelloDate.java
    import java.util.*;
    //import static net.mindview.until.Print.*;
    /** the first Thinking in java example program
      * display a string and today's date
      * @author wengyongsheng 
      * @version 4.0 
    */
    public class HelloDate{
    	/**
    	* @param args array of string arguments
    	*/
      public static void main(String []args) {
      System.out.println("Hello,It's: ");
      System.out.println(new Date());
    //  print("Hello,It's: ");
    }
    }
    
    class Tank{
    	 int level;
    }
    
    class Assignment{
    	public static void main(String []args) {
    		Tank t1 = new Tank();
    		Tank t2 = new Tank();
    		t1.level = 9;
    		t2.level = 47;
    		System.out.println("1:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
        t1 = t2;
        System.out.println("2:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);
    		
    		t1.level = 27;
    		System.out.println("3:t1.level = " + t1.level + 
    		                   " t2.level = " + t2.level);                   		                   
    	}
    }
    
    class Letter{
    	 char c;
    }
    
    class PassObject{
    	static void f(Letter y){
    		y.c = 'z';
    	}
    	public static void main(String []args) {
    		Letter x = new Letter();
    		System.out.println("1:x.c= " + x.c);
    		x.c = 'a';
    		System.out.println("2:x.c= " + x.c);
    		f(x);
    		System.out.println("3:x.c= " + x.c);
    	}
    }
    
    class MathOps{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		int i , j , k;
    		j = rand.nextInt(100) + 1;
    		System.out.println("j = " + j);
    		k = rand.nextInt(100) + 1;
    		System.out.println("k = " + k);
    		i = j + k;
    		System.out.println("i = " + i);
    		
    		float u , v , w;
    		u = rand.nextFloat();
    		System.out.println("u = " + u);
    		v = rand.nextFloat() ;
    		System.out.println("v = " + v);
    		w = u + v;
    		System.out.println("w = " + w);
    	}
    }
    
    class AutoInc{
    	public static void main(String []args) {
    		int i = 1;
    		System.out.println("i = " + i);
    		System.out.println("++i = " + ++i);
    		System.out.println("i++ = " + i++);
    		System.out.println("i2 = " + i);
    		System.out.println("--i = " + --i);
    		System.out.println("i-- = " + i--);
    		System.out.println("i3 = " + i);
     }
    }
    /* output
    i = 1
    ++i = 2
    i++ = 2
    i2=3
    --i = 2
    i-- = 2
    i3=1
    */
    
    class Equaivalence{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1 == n2);
    		System.out.println(n1 != n2);
     }
    }
    
    class EqualsMethod{
    	public static void main(String []args) {
    		Integer n1 = new Integer(47);
    		Integer n2 = new Integer(47);
    		
    		System.out.println(n1.equals(n2));
     }
    }
    
    class Value{
    	int i ;
    }
    class EqualsMethod2{
    	public static void main(String []args) {
    		Value v1 = new Value();
    		Value v2 = new Value();
    	 System.out.println(v1.equals(v2));
     }
    }
    
    class ShortCircuit{
    	static boolean test1(int val){
    		System.out.println("test1( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 1);
    	}
    	
    	static boolean test2(int val){
    		System.out.println("test2( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 2);
    	}
    	
    	static boolean test3(int val){
    		System.out.println("test3( " + val + " )");
    		System.out.println("result: "  + (val <1) );
    		return (val < 3);
    	}
    	
    	
    	public static void main(String []args) {
    		
    		boolean b = test1(0) && test2(2) && test2(3);
    		System.out.println("expression is : "  + b );
    	}
    }
    
    class CommaOperator{
    	public static void main(String []args) {
    		for(int i = 1, j = i + 10; i < 5;i++,j = i*2){
    			System.out.println("i =  "  +  i + " j = "  + j);
    		}
     }
    }
    
    
    class ForEachFloat{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		float f[] = new float[10];
    		for(int i = 0; i < 10;i++){
    			f[i] = rand.nextFloat();
    		}
    		
    		for(float x:f){
    			System.out.println("x =  " + x);
    		}
    		
    		for(char c:"my name".toCharArray()){
    			System.out.println("c =  " + c);
    		}
     }
    }
    
    class LableedWhile{
    	public static void main(String []args) {
    		int i = 0;
    		outer:
    		while(true){
    			System.out.println("Outer while loop");
    			while(true){
    				i++;
    				System.out.println("i = " + i);
    				if(i == 1){
    					System.out.println("continue");
    					continue;
    				}
    				if(i == 3){
    					System.out.println("continue Outer");
    					continue outer;
    				}
    				if(i == 5){
    					System.out.println("break");
    					break;
    				}
    				if(i == 7){
    					System.out.println("break Outer");
    					break outer;
    				}
    			}
    		}
     }
    }
    
    class VoewlsAndConson{
    	public static void main(String []args) {
    		Random rand = new Random(47);
    		for(int i = 0; i < 100;i++){
    			int c = rand.nextInt(26) + 'a';
    			System.out.println("c = " + c);
    			switch(c){
    				case 'a':
    				case 'b':
    				case 'c': System.out.println("vowel");break;
    				case 'y':
    				case 'w':
    				case 'z':System.out.println("consonant");break;
    				default:System.out.println("xxxx");
    			}
    		}
     }
    }
    
    class Flower{
    	int petalCount = 0;
    	String s = "init value";
    	
    	Flower(int peta){
    		petalCount = peta;
    		System.out.println("Flower(int peta)" );
    	}
    	
    	Flower(String ss){
    		s = ss;
    		System.out.println("Flower(String ss) " );
    	}
    	
    	
    	Flower(String s,int patails){
    		this(patails);
    		this.s = s;
    		System.out.println("Flower(String s,int patails) ");
    	}
    	
    	Flower(){
    		this("xxx",1);
    		System.out.println("Flower() ");
    	}
    	public static void main(String []args) {
    		Flower x = new Flower();
    		
    	}
    }
    
    
    class Book{
     boolean checkedOut = false;
     Book(boolean checkedOut){
     	this.checkedOut = checkedOut;
     }
     void checkIn(){
     	checkedOut = false;
     }
     protected void finalize(){
     	if(checkedOut){
     		System.out.println(" protected void finalize:error ");
     	}
    }
    }
    
    class Tcondition{
    	public static void main(String []args) {
    		Book novel = new Book(true);
    		novel.checkIn();
    		new Book(true);
    		System.gc(); //進行強制終結動作
    	}
    }
    
    
    class ArrayClassObj{
    	public static void main(String []args) {
    		Random rand = new Random (46);
    		Integer []a = new Integer[rand.nextInt(20)];
    		System.out.println("a.length = " + a.length);
    		for(int i= 0; i < a.length;i++){
    			a[i] = rand.nextInt(500);
    		}
    		System.out.println(Arrays.toString(a));
    		
    	}
    }
    
    class VarArgs{
    	
    	static void printArray(Object [] args) {
    		for(Object obj : args){
    			System.out.println("obj = " + obj);
    		}
    	}
    	
    	public static void main(String []args) {
    		printArray(new Object[]
    		           {new Integer(10),
    		           	new Integer(20),
    		           	new Integer(30)});
    
        printArray(new Object[]
    		           {"one",
    		           	"two",
    		            "thress"});
    	}
    }
    
    class NewVarArgs{
    	
    	static void printArray(Object ... args) {
    		for(Object obj : args){
    			System.out.println("obj = " + obj);
    		}
    	}
    	
    	public static void main(String []args) {
    		printArray(
    		            new Integer(1),
    		           	new Integer(2),
    		           	new Integer(3));
    
        printArray("one1",
    		           	"two2",
    		            "thress3");
    	}
    }
  • 結果輸出:
  • 可變引數的類可以為任何型別,包括基本型別
    1. 比如:String型別
    2. 比如:Int型別:
  • 可變引數的過載
  1. 可變引數過載舉例(引數只有可變引數):
  2. 可變引數過載舉例(引數有可變引數和不變引數):