Java : Objects with Examples
Objects (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Objects methods.
Summary
This class consists of static utility methods for operating on objects, or checking certain conditions before operation.
final String str1 = null;
final String str2 = "abcd";
System.out.println(Objects.equals(str1, str2)); // false
System.out.println(Objects.equals(str1, null)); // true
System.out.println(Objects.equals(str2, "abcd")); // true
System.out.println(Objects.hash(123, 789, "abc")); // 268807
final var list = new ArrayList<String>();
list.add("a");
list.add(null);
list.add("b");
list.add(null);
list.add("c");
System.out.println(list); // [a, null, b, null, c]
final var result = list.stream().filter(Objects::nonNull).toList();
System.out.println(result); // [a, b, c]
Methods
static int checkFromIndexSize (int fromIndex, int size, int length)
Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).
System.out.println(Objects.checkFromIndexSize(0, 0, 5)); // 0
System.out.println(Objects.checkFromIndexSize(0, 1, 5)); // 0
System.out.println(Objects.checkFromIndexSize(0, 2, 5)); // 0
System.out.println(Objects.checkFromIndexSize(0, 3, 5)); // 0
System.out.println(Objects.checkFromIndexSize(0, 4, 5)); // 0
System.out.println(Objects.checkFromIndexSize(0, 5, 5)); // 0
//Objects.checkFromIndexSize(0, 6, 5); // IndexOutOfBoundsException
System.out.println(Objects.checkFromIndexSize(3, 0, 5)); // 3
System.out.println(Objects.checkFromIndexSize(3, 1, 5)); // 3
System.out.println(Objects.checkFromIndexSize(3, 2, 5)); // 3
//Objects.checkFromIndexSize(3, 3, 5); // IndexOutOfBoundsException
System.out.println(Objects.checkFromIndexSize(0, 1, 5)); // 0
System.out.println(Objects.checkFromIndexSize(1, 1, 5)); // 1
System.out.println(Objects.checkFromIndexSize(2, 1, 5)); // 2
System.out.println(Objects.checkFromIndexSize(3, 1, 5)); // 3
System.out.println(Objects.checkFromIndexSize(4, 1, 5)); // 4
//Objects.checkFromIndexSize(5, 1, 5); // IndexOutOfBoundsException
//Objects.checkFromIndexSize(-1, 0, 5); // IndexOutOfBoundsException
//Objects.checkFromIndexSize(0, -1, 5); // IndexOutOfBoundsException
//Objects.checkFromIndexSize(0, 0, -1); // IndexOutOfBoundsException
static long checkFromIndexSize (long fromIndex, long size, long length)
Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).
Please see checkFromIndexSize(int fromIndex, int size, int length). This method is for a long type.
static int checkFromToIndex (int fromIndex, int toIndex, int length)
Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).
System.out.println(Objects.checkFromToIndex(0, 0, 5)); // 0
System.out.println(Objects.checkFromToIndex(0, 1, 5)); // 0
System.out.println(Objects.checkFromToIndex(0, 2, 5)); // 0
System.out.println(Objects.checkFromToIndex(0, 3, 5)); // 0
System.out.println(Objects.checkFromToIndex(0, 4, 5)); // 0
System.out.println(Objects.checkFromToIndex(0, 5, 5)); // 0
//Objects.checkFromToIndex(0, 6, 5); // IndexOutOfBoundsException
//Objects.checkFromToIndex(3, 2, 5); // IndexOutOfBoundsException
System.out.println(Objects.checkFromToIndex(3, 3, 5)); // 3
System.out.println(Objects.checkFromToIndex(3, 4, 5)); // 3
System.out.println(Objects.checkFromToIndex(3, 5, 5)); // 3
//Objects.checkFromToIndex(3, 6, 5); // IndexOutOfBoundsException
System.out.println(Objects.checkFromToIndex(0, 5, 5)); // 0
System.out.println(Objects.checkFromToIndex(1, 5, 5)); // 1
System.out.println(Objects.checkFromToIndex(2, 5, 5)); // 2
System.out.println(Objects.checkFromToIndex(3, 5, 5)); // 3
System.out.println(Objects.checkFromToIndex(4, 5, 5)); // 4
System.out.println(Objects.checkFromToIndex(5, 5, 5)); // 5
//Objects.checkFromToIndex(6, 5, 5); // IndexOutOfBoundsException
//Objects.checkFromToIndex(-1, 0, 5); // IndexOutOfBoundsException
//Objects.checkFromToIndex(0, -1, 5); // IndexOutOfBoundsException
//Objects.checkFromToIndex(0, 0, -1); // IndexOutOfBoundsException
static long checkFromToIndex (long fromIndex, long toIndex, long length)
Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).
Please see checkFromToIndex(int fromIndex, int toIndex, int length). This method is for a long type.
static int checkIndex (int index, int length)
Checks if the index is within the bounds of the range from 0 (inclusive) to length (exclusive).
//Objects.checkIndex(-1, 5); // IndexOutOfBoundsException
System.out.println(Objects.checkIndex(0, 5)); // 0
System.out.println(Objects.checkIndex(1, 5)); // 1
System.out.println(Objects.checkIndex(2, 5)); // 2
System.out.println(Objects.checkIndex(3, 5)); // 3
System.out.println(Objects.checkIndex(4, 5)); // 4
//Objects.checkIndex(5, 5); // IndexOutOfBoundsException
//Objects.checkIndex(0, 0); // IndexOutOfBoundsException
//Objects.checkIndex(0, -1); // IndexOutOfBoundsException
static long checkIndex (long index, long length)
Checks if the index is within the bounds of the range from 0 (inclusive) to length (exclusive).
Please see checkIndex(int index, int length). This method is for a long type.
static <T> int compare (T a, T b, Comparator<? super T> c)
Returns 0 if the arguments are identical and c.compare(a, b) otherwise.
final var c = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 < o2) {
return -1;
} else if (o1 > o2) {
return 1;
} else {
return 0;
}
}
};
System.out.println(Objects.compare(null, null, c)); // 0
System.out.println(Objects.compare(0, 5, c)); // -1
System.out.println(Objects.compare(5, 0, c)); // 1
System.out.println(Objects.compare(123, 123, c)); // 0
static boolean deepEquals (Object a, Object b)
Returns true if the arguments are deeply equal to each other and false otherwise.
final var str1 = "aaa";
final var str2 = "aaa";
System.out.println(Objects.equals(str1, str2)); // true
System.out.println(Objects.deepEquals(str1, str2)); // true
final var str3 = "bbb";
System.out.println(Objects.equals(str1, str3)); // false
System.out.println(Objects.deepEquals(str1, str3)); // false
final String[] array1 = {"a", "b", "c"};
final String[] array2 = {"a", "b", "c"};
System.out.println(Objects.equals(array1, array2)); // false
System.out.println(Objects.deepEquals(array1, array2)); // true
final String[] array3 = {"x", "y", "z"};
System.out.println(Objects.equals(array1, array3)); // false
System.out.println(Objects.deepEquals(array1, array3)); // false
final int[][] array1 = {
{1, 2}, {3, 4},
{5, 6}, {7, 8}
};
final int[][] array2 = {
{1, 2}, {3, 4},
{5, 6}, {7, 8}
};
System.out.println(Objects.equals(array1, array2)); // false
System.out.println(Objects.deepEquals(array1, array2)); // true
final int[][] array3 = {
{1, 2}, {3, 4},
{100, 200}, {300, 400}
};
System.out.println(Objects.equals(array1, array3)); // false
System.out.println(Objects.deepEquals(array1, array3)); // false
System.out.println(Objects.deepEquals(null, null)); // true
System.out.println(Objects.deepEquals(null, 1)); // false
System.out.println(Objects.deepEquals(2, null)); // false
static boolean equals (Object a, Object b)
Returns true if the arguments are equal to each other and false otherwise.
System.out.println(Objects.equals(1, 2)); // false
System.out.println(Objects.equals(3, 3)); // true
System.out.println(Objects.equals("abc", "def")); // false
System.out.println(Objects.equals("xxx", "xxx")); // true
System.out.println(Objects.equals("123", 123)); // false
System.out.println(Objects.equals(null, null)); // true
System.out.println(Objects.equals(null, 1)); // false
System.out.println(Objects.equals(2, null)); // false
final String[] array1 = {"a", "b", "c"};
final String[] array2 = {"a", "b", "c"};
System.out.println(Objects.equals(array1, array2)); // false
System.out.println(Objects.deepEquals(array1, array2)); // true
static int hash (Object... values)
Generates a hash code for a sequence of input values.
System.out.println(Objects.hash(1)); // 32
System.out.println(Objects.hash(1, 2)); // 994
System.out.println(Objects.hash(2, 1)); // 1024
System.out.println(Objects.hash(1, 2, 3)); // 30817
System.out.println(Objects.hash("a", "b", "c")); // 126145
System.out.println(Objects.hash(1, 2, 3, "a", "b", "c")); // 918165601
final int[] array1 = {1, 2, 3};
final int[] array2 = {1, 2, 3};
final int[] array3 = {4, 5, 6};
System.out.println(Objects.hash(array1)); // 1544785573
System.out.println(Objects.hash(array2)); // 664022656
System.out.println(Objects.hash(array3)); // 1426882641
System.out.println(Arrays.hashCode(array1)); // 30817
System.out.println(Arrays.hashCode(array2)); // 30817
static int hashCode (Object o)
Returns the hash code of a non-null argument and 0 for a null argument.
System.out.println(Objects.hashCode(null)); // 0
System.out.println(Objects.hashCode(123)); // 123
System.out.println(Objects.hashCode("abc")); // 96354
final int[] array1 = {1, 2, 3};
final int[] array2 = {1, 2, 3};
final int[] array3 = {4, 5, 6};
System.out.println(Objects.hashCode(array1)); // 1129405362
System.out.println(Objects.hashCode(array2)); // 556034497
System.out.println(Objects.hashCode(array3)); // 606135032
System.out.println(Arrays.hashCode(array1)); // 30817
System.out.println(Arrays.hashCode(array2)); // 30817
static boolean isNull (Object obj)
Returns true if the provided reference is null otherwise returns false.
System.out.println(Objects.isNull(null)); // true
System.out.println(Objects.isNull(123)); // false
System.out.println(Objects.isNull("abc")); // false
final var list = new ArrayList<String>();
list.add("a");
list.add(null);
list.add("b");
list.add(null);
list.add("c");
System.out.println(list); // [a, null, b, null, c]
final var result = list.stream().filter(Objects::isNull).toList();
System.out.println(result); // [null, null]
static boolean nonNull (Object obj)
Returns true if the provided reference is non-null otherwise returns false.
System.out.println(Objects.nonNull(null)); // false
System.out.println(Objects.nonNull(123)); // true
System.out.println(Objects.nonNull("abc")); // true
final var list = new ArrayList<String>();
list.add("a");
list.add(null);
list.add("b");
list.add(null);
list.add("c");
System.out.println(list); // [a, null, b, null, c]
final var result = list.stream().filter(Objects::nonNull).toList();
System.out.println(result); // [a, b, c]
static <T> T requireNonNull (T obj)
Checks that the specified object reference is not null.
class Book {
private final String title;
private final String content;
Book(String title, String content) {
this.title = Objects.requireNonNull(title);
this.content = Objects.requireNonNull(content);
}
@Override
public String toString() {
return "title : " + title + ", content : " + content;
}
}
//new Book(null, "abcd"); // NullPointerException
//new Book("XYZ", null); // NullPointerException
final var book = new Book("XYZ", "abcd");
System.out.println(book); // title : XYZ, content : abcd
static <T> T requireNonNull (T obj, String message)
Checks that the specified object reference is not null and throws a customized NullPointerException if it is.
class Book {
private final String title;
private final String content;
Book(String title, String content) {
this.title = Objects.requireNonNull(title, "title must not be null");
this.content = Objects.requireNonNull(content, "content must not be null");
}
@Override
public String toString() {
return "title : " + title + ", content : " + content;
}
}
//new Book(null, "abcd"); // NullPointerException: title must not be null
//new Book("XYZ", null); // NullPointerException: content must not be null
final var book = new Book("XYZ", "abcd");
System.out.println(book); // title : XYZ, content : abcd
static <T> T requireNonNull (T obj, Supplier<String> messageSupplier)
Checks that the specified object reference is not null and throws a customized NullPointerException if it is.
class Book {
private final String title;
private final String content;
Book(String title, String content) {
this.title = Objects.requireNonNull(title, () -> "title must not be null");
this.content = Objects.requireNonNull(content, () -> "content must not be null");
}
@Override
public String toString() {
return "title : " + title + ", content : " + content;
}
}
//new Book(null, "abcd"); // NullPointerException: title must not be null
//new Book("XYZ", null); // NullPointerException: content must not be null
final var book = new Book("XYZ", "abcd");
System.out.println(book); // title : XYZ, content : abcd
static <T> T requireNonNullElse (T obj, T defaultObj)
Returns the first argument if it is non-null and otherwise returns the non-null second argument.
class Book {
private final String title;
private final String content;
Book(String title, String content) {
this.title = Objects.requireNonNullElse(title, "default title");
this.content = Objects.requireNonNullElse(content, "default content");
}
@Override
public String toString() {
return "title : " + title + ", content : " + content;
}
}
final var book1 = new Book(null, "abcd");
System.out.println(book1); // title : default title, content : abcd
final var book2 = new Book("XYZ", null);
System.out.println(book2); // title : XYZ, content : default content
final var book3 = new Book("XYZ", "abcd");
System.out.println(book3); // title : XYZ, content : abcd
static <T> T requireNonNullElseGet (T obj, Supplier<? extends T> supplier)
Returns the first argument if it is non-null and otherwise returns the non-null value of supplier.get().
class Book {
private final String title;
private final String content;
Book(String title, String content) {
this.title = Objects.requireNonNullElseGet(title, () -> "default title");
this.content = Objects.requireNonNullElseGet(content, () -> "default content");
}
@Override
public String toString() {
return "title : " + title + ", content : " + content;
}
}
final var book1 = new Book(null, "abcd");
System.out.println(book1); // title : default title, content : abcd
final var book2 = new Book("XYZ", null);
System.out.println(book2); // title : XYZ, content : default content
final var book3 = new Book("XYZ", "abcd");
System.out.println(book3); // title : XYZ, content : abcd
static String toString (Object o)
Returns the result of calling toString for a non- null argument and "null" for a null argument.
final var str1 = Objects.toString(null);
System.out.println(str1); // "null"
final var str2 = Objects.toString(123);
System.out.println(str2); // "123"
final var str3 = Objects.toString("abcd");
System.out.println(str3); // "abcd"
static String toString (Object o, String nullDefault)
Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.
final var str1 = Objects.toString(null, "XYZ");
System.out.println(str1); // "XYZ"
final var str2 = Objects.toString(123, "XYZ");
System.out.println(str2); // "123"
final var str3 = Objects.toString("abcd", "XYZ");
System.out.println(str3); // "abcd"