Java by API/org.apache.commons.lang/ObjectUtils
Содержание
ObjectUtils: defaultIfNull(Object object, Object defaultValue)
<source lang="java">
/* 1) If null return DEFAULT >>>DEFAULT 2) References to the same object >>>true 3) Check object references and not values >>>true 4) toSring gets invoked >>> 5) Display object details >>>java.lang.String@93dcd 6) Pass null and get back an Empty string >>>****
- /
import org.apache.rumons.lang.ObjectUtils; public class ObjectUtilsTrial {
public static void main(String[] args) { // Create ObjectUtilsTrial instance String one = new String(); String two = one; // Same Reference String three = new String(); // New Object String four = null; // four is null, returns DEFAULT System.out.print("1) If null return DEFAULT >>>"); System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT")); // one and two point to the same object System.out.print("2) References to the same object >>>"); System.out.println(ObjectUtils.equals(one, two)); // one and three are different objects System.out.print("3) Check object references and not values >>>"); System.out.println(ObjectUtils.equals(one, three)); // toString method gets called System.out.print("4) toSring gets invoked >>>"); System.out.println(one); // Object details displayed..toString is not called System.out.print("5) Display object details >>>"); System.out.println(ObjectUtils.identityToString(one)); // Pass null get empty string System.out.print("6) Pass null and get back an Empty string >>>"); System.out.println("**" + ObjectUtils.toString(null) + "**"); } public String toString() { return "toString Output"; }
}
</source>
ObjectUtils: equals(Object object1, Object object2)
<source lang="java">
/* 1) If null return DEFAULT >>>DEFAULT 2) References to the same object >>>true 3) Check object references and not values >>>true 4) toSring gets invoked >>> 5) Display object details >>>java.lang.String@93dcd 6) Pass null and get back an Empty string >>>****
- /
import org.apache.rumons.lang.ObjectUtils; public class ObjectUtilsTrial {
public static void main(String[] args) { // Create ObjectUtilsTrial instance String one = new String(); String two = one; // Same Reference String three = new String(); // New Object String four = null; // four is null, returns DEFAULT System.out.print("1) If null return DEFAULT >>>"); System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT")); // one and two point to the same object System.out.print("2) References to the same object >>>"); System.out.println(ObjectUtils.equals(one, two)); // one and three are different objects System.out.print("3) Check object references and not values >>>"); System.out.println(ObjectUtils.equals(one, three)); // toString method gets called System.out.print("4) toSring gets invoked >>>"); System.out.println(one); // Object details displayed..toString is not called System.out.print("5) Display object details >>>"); System.out.println(ObjectUtils.identityToString(one)); // Pass null get empty string System.out.print("6) Pass null and get back an Empty string >>>"); System.out.println("**" + ObjectUtils.toString(null) + "**"); } public String toString() { return "toString Output"; }
}
</source>
ObjectUtils: identityToString(Object obj)
<source lang="java">
/* 1) If null return DEFAULT >>>DEFAULT 2) References to the same object >>>true 3) Check object references and not values >>>true 4) toSring gets invoked >>> 5) Display object details >>>java.lang.String@93dcd 6) Pass null and get back an Empty string >>>****
- /
import org.apache.rumons.lang.ObjectUtils; public class ObjectUtilsTrial {
public static void main(String[] args) { // Create ObjectUtilsTrial instance String one = new String(); String two = one; // Same Reference String three = new String(); // New Object String four = null; // four is null, returns DEFAULT System.out.print("1) If null return DEFAULT >>>"); System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT")); // one and two point to the same object System.out.print("2) References to the same object >>>"); System.out.println(ObjectUtils.equals(one, two)); // one and three are different objects System.out.print("3) Check object references and not values >>>"); System.out.println(ObjectUtils.equals(one, three)); // toString method gets called System.out.print("4) toSring gets invoked >>>"); System.out.println(one); // Object details displayed..toString is not called System.out.print("5) Display object details >>>"); System.out.println(ObjectUtils.identityToString(one)); // Pass null get empty string System.out.print("6) Pass null and get back an Empty string >>>"); System.out.println("**" + ObjectUtils.toString(null) + "**"); } public String toString() { return "toString Output"; }
}
</source>
ObjectUtils: toString(Object obj)
<source lang="java">
/* 1) If null return DEFAULT >>>DEFAULT 2) References to the same object >>>true 3) Check object references and not values >>>true 4) toSring gets invoked >>> 5) Display object details >>>java.lang.String@93dcd 6) Pass null and get back an Empty string >>>****
- /
import org.apache.rumons.lang.ObjectUtils; public class ObjectUtilsTrial {
public static void main(String[] args) { // Create ObjectUtilsTrial instance String one = new String(); String two = one; // Same Reference String three = new String(); // New Object String four = null; // four is null, returns DEFAULT System.out.print("1) If null return DEFAULT >>>"); System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT")); // one and two point to the same object System.out.print("2) References to the same object >>>"); System.out.println(ObjectUtils.equals(one, two)); // one and three are different objects System.out.print("3) Check object references and not values >>>"); System.out.println(ObjectUtils.equals(one, three)); // toString method gets called System.out.print("4) toSring gets invoked >>>"); System.out.println(one); // Object details displayed..toString is not called System.out.print("5) Display object details >>>"); System.out.println(ObjectUtils.identityToString(one)); // Pass null get empty string System.out.print("6) Pass null and get back an Empty string >>>"); System.out.println("**" + ObjectUtils.toString(null) + "**"); } public String toString() { return "toString Output"; }
}
</source>