Java Tutorial/Apache Common/ObjectUtils

Материал из Java эксперт
Перейти к: навигация, поиск

ObjectUtils.defaultIfNull

import org.apache.rumons.lang.ObjectUtils;
public class MainClass {
    public static void main(String[] args) {
        //Create ObjectUtilsTrial instance
        MyClass one = new MyClass();
        MyClass two = one; //Same Reference
        MyClass three = new MyClass(); //New Object
        MyClass four = null;
        //four is null, returns DEFAULT
        System.out.print("1) If null return DEFAULT >>>");
        System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT"));
    }
}
class MyClass{
    public String toString() {
        return "toString Output";
    }
}



1) If null return DEFAULT >>>DEFAULT



ObjectUtils.equals

import org.apache.rumons.lang.ObjectUtils;
public class MainClass {
    public static void main(String[] args) {
        //Create ObjectUtilsTrial instance
        MyClass one = new MyClass();
        MyClass two = one; //Same Reference
        MyClass three = new MyClass(); //New Object
        MyClass four = null;

        //one and two point to the same object
        System.out.print("2) References to the same object >>>");
        System.out.println(ObjectUtils.equals(one, two));
    }
}
class MyClass{
    public String toString() {
        return "toString Output";
    }
}



2) References to the same object >>>true



ObjectUtils.equals2

import org.apache.rumons.lang.ObjectUtils;
public class MainClass {
    public static void main(String[] args) {
        //Create ObjectUtilsTrial instance
        MyClass one = new MyClass();
        MyClass two = one; //Same Reference
        MyClass three = new MyClass(); //New Object
        MyClass four = null;
        //one and three are different objects
        System.out.print("3) Check object references and not values >>>");
        System.out.println(ObjectUtils.equals(one, three));
    }
}
class MyClass{
    public String toString() {
        return "toString Output";
    }
}



3) Check object references and not values >>>false



ObjectUtils.identityToString

import org.apache.rumons.lang.ObjectUtils;
public class MainClass {
    public static void main(String[] args) {
        //Create ObjectUtilsTrial instance
        MyClass one = new MyClass();
        MyClass two = one; //Same Reference
        MyClass three = new MyClass(); //New Object
        MyClass four = null;
        //Object details displayed..toString is not called
        System.out.print("5) Display object details >>>");
        System.out.println(ObjectUtils.identityToString(one));
    }
}
class MyClass{
    public String toString() {
        return "toString Output";
    }
}



5) Display object details >>>MyClass@b89838



ObjectUtils.toString

import org.apache.rumons.lang.ObjectUtils;
public class MainClass {
    public static void main(String[] args) {
        //Create ObjectUtilsTrial instance
        MyClass one = new MyClass();
        MyClass two = one; //Same Reference
        MyClass three = new MyClass(); //New Object
        MyClass four = null;
        //Pass null get empty string
        System.out.print("6) Pass null and get back an Empty string >>>");
        System.out.println("**" + ObjectUtils.toString(null) + "**");
    }
}
class MyClass{
    public String toString() {
        return "toString Output";
    }
}



6) Pass null and get back an Empty string >>>****