API
Object
-
直接输入一个对象,Java其实是会调用对象的tostring()方法的 ,所以以下两行语句的输出结果是一致的
System.out.println(w1); System.out.println(w1.toString);
在Weapon类没有重写toString()方法时,会输出类似于org.forten.api.object.Weapon@2ff4acd0的内容
此处内容的出处时Object类中的toString()方法,通常它对于人们没有太大的意义,所以需要去在具体的类中重写toString()方法
Object类中的toString()方法的实现:getClass().getName() + '@' + Integer.toHexString(hashCode())。getClass.getName()得到对象字节码中类的全限定名称
、hashCode()会返回一个int值,此值是通过对象的内存地址计算得出的,但这个值不是对象的内存地址
-
nteger.toHexString(int x);得到一个字符串,字符串是一个16进制的数字形式,在子类Weapon中重写父类Object的toString()方法后,会输出以下内容:
Weapon [name=倚天剑, grade=35, damage=1200]
package org.forten.api.object; public class ObjectTest001 { public static void main(String[] args) throws CloneNotSupportedException { Weapon w1 = new Weapon("倚天剑", 35, 1200); System.out.println(w1); System.out.println(w1.toString()); Weapon w2 = (Weapon) w1.clone(); System.out.println(w2); Weapon w3 = w1; System.out.println(w3); System.out.println(w1==w2);// false System.out.println(w1==w3);// true System.out.println(w1.equals(w2));// true System.out.println(w1.equals(w3));// true System.out.println(w2.equals(w3));// true } }
public class ObjectTest002 { public static void main(String[] args) { Weapon w1 = null; // java.lang.NullPointerException // System.out.println(w1.toString()); //当对象为null时,返回字符串"null" //输出:"null" System.out.println(Objects.toString(w1)); //但对象为null时,会返回第二个参数,它也是字符串类型的,输出:"当前的武器不存在" System.out.println(Objects.toString(w1,"当前武器不存在")) // java.lang.NullPointerException // System.out.println(w1.hashCode()); //当对象为null时,Objects.hashCode(obj)返回0 System.out.println(Objects.hashCode(w1)); System.out.println("----------------------------------"); w1 = new Weapon("屠龙枪", 10, 1000); Weapon w2 = new Weapon("龙牙", 30, 2000); Weapon w3 = new Weapon("飓风", 36, 2300); //对象的hashCode()方法与Objects.hashCode(obj)方法返回的整数是一致的 //如果对象为null,则Objects.hashCode(obj)方法返回0 System.out.println(w1.hashCode()); System.out.println(Objects.hashCode(w1)); System.out.println(w2.hashCode()); System.out.println(Objects.hashCode(w2)); System.out.println(w3.hashCode()); System.out.println(Objects.hashCode(w3)); System.out.println("----------------------------------"); //Objects.hash(o1,o2,o3);是通过所有参数对象计算出一个综合hash值 //注意:单个对象的obj.hashCode()和objects.hash(obj)返回的数字是不一致的 System.out.println(Objects.hashCode(w1)); System.out.println(Objects.hash(w1)); System.out.println(Objects.hash(w1, w2, w3)); //如果hash方法的参数都为null,则会返回0 System.out.println(Objects.hash(null,null,null)); System.out.println("----------------------------------"); //Objects.equals(a,b) //如果a和b都不是null,则按正常逻辑进行a和b的相等判定 Sysem.out.println(Objects.equals(w1,w2)); //如果a和b都是null,则直接返回true System.out.println(Objects.equals(null,null)); //如果a和b其一是null,则直接返回false. System.out.println(Objects.equals(null, w1)); System.out.println(Objects.equals(w1, null)); System.out.println("----------------------------------"); System.out.println(Objects.nonNull(w1)); System.out.println(Objects.nonNull(null)); System.out.println(Objects.isNull(w1)); System.out.println(Objects.isNull(null)); System.out.println("----------------------------------"); //w1=new Weapon(null,1,10); //static
T requireNonNullElse(T obj,T defaultObj) //如果obj非null,则返回它,否则defaultObj(默认对象) w1 = new Weapon("屠龙枪", 10, 1000); Weapon newW = Objects.requireNonNullElse(w1, new Weapon("板砖", 1, 5)); System.out.println(newW); newW = Objects.requireNonNullElse(null, new Weapon("板砖", 1, 5)); System.out.println(newW); // static T requireNonNullElseGet(T obj, Supplier extends T> supplier) newW = Objects.requireNonNullElseGet(null, ()->new Weapon("板凳", 2, 7)); System.out.println(newW); } } package org.forten.api.object; import java.util.Objects; public class Weapon implements Cloneable { private String name; private int grade; private int damage; public Weapon() { } public Weapon(String name, int grade, int damage) { //如果name的值为null,则会抛出java.lang.NullPointerException //Objects.requireNonNull(name); //如果name的值为null,则会抛出java.lang.NullPointerException //并且以第二个参数的字符串为异常对象的消息 // Objects.requireNonNull(name,"武器的名字呢?"); //以下的方法的第二参数是一个函数式接口, //使用一个Lambda表达式进行生成消息字符串行为的传递 // static
T requireNonNull(T obj, Supplier messageSupplier) Objects.requireNonNull(name,()->"武器的名字呢?".substring(3)); this.name = name; this.grade = grade; this.damage = damage; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public int getDamage() { return damage; } public void setDamage(int damage) { this.damage = damage; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + damage; result = prime * result + grade; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { //如果this和obj引用的是同一个对象,表示this和obj相等 @Override public boolean equals(Object obj) { //如果待比较对象是null,则表示不相等,因为this此时不可能是null if (obj == null) return false; //判断两对象是否是同一类型,如果不是,返回false if (getClass() != obj.getClass()) return false; //把待比较对象强制转型为this对象的类型 Wepon that =(Weapon) obj; //比较参与到相等判定中的所有实例变量的值是否都一致 //任何一个不一致都会返回false if (damage != that.damage) return false; if (grade != that.grade) return false; if (name == null) { if (that.name != null) return false; } else if (!name.equalsIgnoreCase(that.name)) return false; //通过了所有判断,都没有的返回false,则表示两对象相等,返回true return true; } @Override public String toString() { return "Weapon [name=" + name + ", grade=" + grade + ", damage=" + damage + "]"; } } package org.forten.api.string; public class StringTest002 { public static void main(String[] args) { //使用String类的构造器实例化的对象是存储在内存堆空间中的 String s1 = new String("abc"); String s2 = new String("abc"); System.out.println(s1 == s2);// false //s1.intern()会把s1的字符串内容复制到串池中,并返回串池中的字符串 //s3.引用的是串池中的字符串地址,而s1还是在引用着队中的字符串 String s3 = s1.intern(); System.out.println(s1 == s2);// false System.out.println(s1 == "abc"); System.out.println(s2 == "abc"); System.out.println(s3 == "abc"); } }
package org.forten.api.string; import java.io.UnsupportedEncodingException; import java.util.Arrays; public class StringTest003 { public static void main(String[] args) throws UnsupportedEncodingException { String s1 = new String("abc"); System.out.println(s1); char[] charArr = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; String s2 = new String(charArr); System.out.println(s2); String s3 = new String(charArr, 2, 4); System.out.println(s3); byte[] bytes = { 97, 98, 99, 100, 101, 102, 103 }; String s4 = new String(bytes); System.out.println(s4); String s5 = new String(bytes, 2, 4); System.out.println(s5); String s6 = "abcdefghijklmn"; // 通过s6.length()可以得到字符串s6的长度 System.out.println(s6.length()); bytes = s6.getBytes(); System.out.println(bytes.length); System.out.println(Arrays.toString(bytes)); System.out.println("----------------------------------"); String s7 = "齐天大朕万万岁!"; System.out.println(s7.length());// 7 bytes = s7.getBytes(); System.out.println(bytes.length);// 21 System.out.println(Arrays.toString(bytes)); String s8 = new String(bytes, "GBK"); System.out.println(s8.length()); System.out.println(s8); bytes = s8.getBytes("GBK"); String s9 = new String(bytes,"UTF-8"); System.out.println(s9.length()); System.out.println(s9); } }
package org.forten.api.string; public class StringTest001 { public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); String s4 = new String("abc"); System.out.println(s1 == s2);// true System.out.println(s3 == s4);// false } }