package net.mindview.util; public class Print { /** * 不带有回车 * @param s */ public static void print(Object s){ System.out.print(s); } /** * 带有回车 * @param s */ public static void println(Object s){ System.out.println(s); } }
package net.mindview.operators; import java.util.Date; import static net.mindview.util.Print.*; public class HelloData { public static void main(String[] args) { print("hello, it is"); print(new Date()); System.out.println("正常的方式打印"); } }
package net.mindview.operators; class Tank{ int level; } public class Assignment { public static void main(String[] args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 27; t2.level = 41; System.out.println("t1.level:" + t1.level + ", t2.level:" + t2.level); t2 = t1; System.out.println("t1.level:" + t1.level + ", t2.level:" + t2.level); t1.level = 5; System.out.println("t1.level:" + t1.level + ", t2.level:" + t2.level); } }
package net.mindview.operators; class Letter{ char c; } public class PassObject { static void f(Letter y){ y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c="+x.c); //传递的时x所指向的引用 f(x); System.out.println("1: x.c="+x.c); } }
package net.mindview.operators; class Dog{ public String name; public String says; @Override public String toString() { return "名字:"+this.name + ",语言:"+this.says; } } public class DogTest { public static void main(String[] args) { // TODO Auto-generated method stub Dog d1 = new Dog(); Dog d2 = new Dog(); d1.name = "spot"; d1.says = "Ruff!"; d2.name = "scruffy"; d2.says = "Wurf!"; System.out.println(d1); System.out.println(d2); } }
package net.mindview.operators; class Dog{ public String name; public String says; @Override public String toString() { return "名字:"+this.name + ",语言:"+this.says; } } public class DogTest { public static void main(String[] args) { // TODO Auto-generated method stub Dog d1 = new Dog(); Dog d2 = new Dog(); d1.name = "spot"; d1.says = "Ruff!"; d2.name = "scruffy"; d2.says = "Wurf!"; System.out.println(d1); System.out.println(d2); Dog d3 = new Dog(); d3.name = "spot"; System.out.println(d1.name == d3.name); System.out.println(d1.name.equals(d3.name)); } }
输出结果:
名字:spot,语言:Ruff! 名字:scruffy,语言:Wurf! true true
这个结果需要特别说明一下, String是特殊的引用类型, 当他被直接赋值时,就是把这个值对应的引用位置赋值给String变量了, 所以, 两次结果都是true。 如果你用new String()赋值, 结果就不同了.
package net.mindview.operators; import java.util.Random; import static net.mindview.util.Print.*; public class ThrowCron { public static void main(String[] args) { Random num = new Random(); int a = num.nextInt(100); switch (a % 2){ case 0: println("正面"); break; case 1: println("反面");break; } } }