第5章 初始化和清理/5.2 区分重载方法

 

  1. 参数数量
  2. 参数类型

第5章 初始化和清理/5.2 区分重载方法/5.2.1 涉及基本类型的重载

  1. 如果传入的实际基本类型小于方法中声明的形式参数类型,那么实际类型都会被提示
  2. 如果传入的实际基本类型大于方法中声明的形式参数类型,那么需要通过类型转换来执行窄化转换

第5章 初始化和清理/5.2 区分重载方法/5.2.2 以返回值区分重载方法


第5章 初始化和清理/5.3 默认构造器


第5章 初始化和清理/5.4 this关键字

  1. 使用场景
    1. 返回当前对象的引用:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
    2. 将自己作为引用传入其它方法:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.4 this关键字/5.4.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();
    		
    	}
    }
  2. 输出结果:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.4 this关键字/5.4.2 static的含义

  1. 内部调用时只能调用标识有static域或者static方法
  2. 可以直接用类名调用

第5章 初始化和清理/5.5 清理:终结处理和垃圾回收

  1. 垃圾回收站只针对由new申请的内存
  2. 垃圾回收站并不一定会发生
  3. 对象并不一定凡在垃圾回收站中
  4. 垃圾回收站不等同于析构函数

第5章 初始化和清理/5.5 清理:终结处理和垃圾回收/5.5.1 finalize用途所在


第5章 初始化和清理/5.5 清理:终结处理和垃圾回收/5.5.2 你必选实施清理

易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理


第5章 初始化和清理/5.5 清理:终结处理和垃圾回收/5.5.3 终结条件

  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. 输出结果:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.5 清理:终结处理和垃圾回收/5.5.4 垃圾回收站如何工作

  1. 引用计数方法
    1. 原理
      1. 当有引用指向对象时,引用计数加1;当引用离开域时引用计数减去1
      2. 垃圾回收站扫描引用计数列表,当发现引用计数为0时就会马上释放对象空间
    2. 缺点:如果对象存在互相引用,会出现“对象应该被回收,但是引用计数不为0的情况”
    3. 应用:常用来说明垃圾回收站的工作方式,但是实际上不被应用
  2. 基于找到“活”的对象:追踪在堆栈或者静态存储区域中的引用

第5章 初始化和清理/5.6 成员初始化


第5章 初始化和清理/5.6 成员初始化/5.6.1 指定初始化

  1. 基本类型在定义时直接初始化:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  2. 对象类型在定义时直接初始化:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  3. 调用方法提供初始值,但是方法必须先有值:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  4. 调用方法初始化值,但是方法没有值,这种是不行的:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.7 构造器初始化/5.7.1 初始化顺序

  1. 举例:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.7 构造器初始化/5.7.2 静态数据的初始化

  1. 在堆栈上检查该类是已经存在,如果不存在
    1. 初始化static数据
    2. 初始化非static数据
  2. 如果已经存在则直接返回

第5章 初始化和清理/5.7 构造器初始化/5.7.3 显式静态初始化

  1. 在static后面用花括号括起来的对静态成员初始化,
  2. 格式:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  3. 特点:也是被初始化一次
  4. 举例:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.7 构造器初始化/5.7.4 非静态实例初始化

  1. 概念:类似于“静态子句”,也是在对象声明后面进行初始化
  2. 格式:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  3. 举例:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  4. 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();
      
           }
      
      }

       

    2. StaticTest初始化,i=400

      StaticTest初始化,i=500

      StaticTest初始化,i=100

      StaticTest初始化,i=200

      i=0

      StaticTest初始化,i=300

      i=0

      StaticTests初始化

      StaticTest初始化,i=200

      i=0

      StaticTest初始化,i=300

      i=0

      StaticTests初始化


第5章 初始化和清理/5.8 数组初始化

  1. 概念:指的是数据类型相同的基本类型序列或者对象序列
  2. 表示方法:
    1. int a1;:JAVA中采用本方法
    2. int  a1[ ];:C或者C++的写法
  3. 数组初始化的方法
    1. 花括号方法:int [ ]a1 = {1,2,3,4,5};
    2. 数组引用:
      1. 格式:int [ ]a2 ; a2 = a1;
      2. 例子:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
    3. new后面直接初始化,适应于无固定参数:
      1. Integer [ ]b = new Integer[ ] {new Integer(1),new Integer(2),3};
      2. 举例:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  4. 所有的数组(包括基本类型数组)都有一个固定成员:length
  5. 数组下标也是从0开始的,最大到length-1
  6. 举例:
    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. 输出结果:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.8 数组初始化/5.8.1 可变参数列表

  1. 源代码:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
    //: 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. 结果输出:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  • JAVA SE5后新可变参数方法:
  • 源代码:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
    //: 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");
    	}
    }
  • 结果输出:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  • 可变参数的类可以为任何类型,包括基本类型
    1. 比如:String类型易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
    2. 比如:Int类型:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  • 可变参数的重载
  1. 可变参数重载举例(参数只有可变参数):易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  2. 可变参数重载举例(参数有可变参数和不变参数):易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

第5章 初始化和清理/5.9 枚举类型

  1. 关键字:enum
  2. 自动添加的方法
    1. toString( ):显示实例名称
    2. ordinal( ):显示声明顺序
    3. values( ):显示常量对应值 
  3. 举例:
    1. 源代码:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
    2. 输出结果:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理
  4. enum与switch的结合使用:易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第5章 初始化和清理

相关文章:

  • 2021-10-14
  • 2021-09-19
  • 2021-09-29
  • 2022-01-18
  • 2022-12-23
  • 2021-05-05
  • 2021-10-14
  • 2021-12-16
猜你喜欢
  • 2021-07-02
  • 2021-10-18
  • 2022-01-10
  • 2021-07-21
  • 2021-08-15
  • 2021-08-31
  • 2021-11-26
相关资源
相似解决方案