【JAVA学习路-think in java】p77:验证未初始化的String+构造器

package pkg;

class Str23{//1st class
	String s;// you will see in output that String s is point to NULL
	Str23(String st){// self-defined constructor
		s=st;
		System.out.println("Created,i am: "+s);
	}
	Str23(){//default constructor
		System.out.println("Created,i am: "+s);
		//nothing,i want to know what it is contained in s by deafault
	}
	
}

class Rock2{//2nd class
	
	Rock2(int i){// the constructor with a input
		System.out.println("Rock: "+i);	
	} 
}


public class p77 {
	public static void main(String[] args){
		
		Object Rock2;
		Object Str23;
		
		for (int i=0;i<10;i++)
			new Rock2(i);
		
		System.out.println("Now,go to see what contained in Str23");
		Str23 st=new Str23();// this can discover what it is in String when it is initialized
		System.out.println(st);
		
		Str23 sp=new Str23("Maomao");
		System.out.println(sp);
		
	}
	
	
}

OUTPUR:

Rock: 0
Rock: 1
Rock: 2
Rock: 3
Rock: 4
Rock: 5
Rock: 6
Rock: 7
Rock: 8
Rock: 9
Now,go to see what contained in Str23
Created,i am: null
pkg.Str23@15db9742
Created,i am: Maomao
pkg.Str23@6d06d69c
 

发布了29 篇原创文章 · 获赞 5 · 访问量 6043

猜你喜欢

转载自blog.csdn.net/Andrew_Zeng/article/details/103932907