Java tostring or null reference

Class Objects

This class consists of static utility methods for operating on objects, or checking certain conditions before operation. These utilities include null -safe or null -tolerant methods for computing the hash code of an object, returning a string for an object, comparing two objects, and checking if indexes or sub-range values are out of bounds.

Method Summary

Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Читайте также:  Coding in advanced java

Returns the first argument if it is non- null and otherwise returns the non- null value of supplier.get() .

Returns a string equivalent to the string returned by Object.toString if that method and hashCode are not overridden.

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

Methods declared in class java.lang.Object

Method Details

equals

Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null , true is returned. Otherwise, if the first argument is not null , equality is determined by calling the equals method of the first argument with the second argument of this method. Otherwise, false is returned.

deepEquals

Returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.

hashCode

hash

Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]) . This method is useful for implementing Object.hashCode() on objects containing multiple fields. For example, if an object that has three fields, x , y , and z , one could write:

@Override public int hashCode()

Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling hashCode(Object) .

toString

toString

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

toIdentityString

Returns a string equivalent to the string returned by Object.toString if that method and hashCode are not overridden.

compare

Returns 0 if the arguments are identical and c.compare(a, b) otherwise. Consequently, if both arguments are null 0 is returned. Note that if one of the arguments is null , a NullPointerException may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for null values.

requireNonNull

Checks that the specified object reference is not null . This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:

requireNonNull

Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:

public Foo(Bar bar, Baz baz)

isNull

nonNull

requireNonNullElse

requireNonNullElseGet

Returns the first argument if it is non- null and otherwise returns the non- null value of supplier.get() .

requireNonNull

Checks that the specified object reference is not null and throws a customized NullPointerException if it is. Unlike the method requireNonNull(Object, String) , this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.

checkIndex

Источник

Java tostring or null reference

This class consists of static utility methods for operating on objects. These utilities include null -safe or null -tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.

Method Summary

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

Methods inherited from class java.lang.Object

Method Detail

equals

Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null , true is returned and if exactly one argument is null , false is returned. Otherwise, equality is determined by using the equals method of the first argument.

deepEquals

Returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.

hashCode

hash

Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]) . This method is useful for implementing Object.hashCode() on objects containing multiple fields. For example, if an object that has three fields, x , y , and z , one could write:

@Override public int hashCode()

Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling hashCode(Object) .

toString

toString

public 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.

compare

Returns 0 if the arguments are identical and c.compare(a, b) otherwise. Consequently, if both arguments are null 0 is returned. Note that if one of the arguments is null , a NullPointerException may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for null values.

requireNonNull

public static T requireNonNull(T obj)

Checks that the specified object reference is not null . This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:

requireNonNull

Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:

Источник

Null and toString()

send pies

posted 11 years ago

  • Report post to moderator
  • Hi all,
    I want to ask about the following code snippet

    My question is ,in the first case,toString() is invoked implicitly on null,and it compiled and ran just fine,but then what is wrong with second line.

    Bartender

    Chrome

    send pies

    posted 11 years ago

  • Report post to moderator
  • toString() is only called implicitly if the object passed isn’t null. If it is, the literal «null» is printed instead.

    Bartender

    send pies

    posted 11 years ago

    • 1
  • Report post to moderator
  • Sudhanshu Mishra wrote: Hi all,
    I want to ask about the following code snippet

    My question is ,in the first case,toString() is invoked implicitly on null,

    No, it’s not. Somewhere down in one of the methods that println() calls, there’s code that’s something like

    but then what is wrong with second line.

    You’re dereferencing a null pointer.

    Источник

    Class Objects

    This class consists of static utility methods for operating on objects, or checking certain conditions before operation. These utilities include null -safe or null -tolerant methods for computing the hash code of an object, returning a string for an object, comparing two objects, and checking if indexes or sub-range values are out of bounds.

    Method Summary

    Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

    Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

    Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

    Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

    Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

    Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

    Returns the first argument if it is non- null and otherwise returns the non- null value of supplier.get() .

    Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

    Methods declared in class java.lang.Object

    Method Details

    equals

    Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null , true is returned. Otherwise, if the first argument is not null , equality is determined by calling the equals method of the first argument with the second argument of this method. Otherwise, false is returned.

    deepEquals

    Returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.

    hashCode

    hash

    Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]) . This method is useful for implementing Object.hashCode() on objects containing multiple fields. For example, if an object that has three fields, x , y , and z , one could write:

    @Override public int hashCode()

    Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling hashCode(Object) .

    toString

    toString

    Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

    compare

    Returns 0 if the arguments are identical and c.compare(a, b) otherwise. Consequently, if both arguments are null 0 is returned. Note that if one of the arguments is null , a NullPointerException may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for null values.

    requireNonNull

    Checks that the specified object reference is not null . This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:

    requireNonNull

    Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:

    public Foo(Bar bar, Baz baz)

    isNull

    nonNull

    requireNonNullElse

    requireNonNullElseGet

    Returns the first argument if it is non- null and otherwise returns the non- null value of supplier.get() .

    requireNonNull

    Checks that the specified object reference is not null and throws a customized NullPointerException if it is. Unlike the method requireNonNull(Object, String) , this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.

    checkIndex

    Источник

    Оцените статью