java tostring 库_java toString方法

//---------------------------------------------------------------------//Convenience methods for toString output//---------------------------------------------------------------------

/*** Return a String representation of an object's overall identity.

*@paramobj the object (may be {@codenull})

*@returnthe object's identity as String representation,

* or an empty String if the object was {@codenull}*/

public staticString identityToString(Object obj) {if (obj == null) {returnEMPTY_STRING;

}return obj.getClass().getName() + "@" +getIdentityHexString(obj);

}/*** Return a hex String form of an object's identity hash code.

*@paramobj the object

*@returnthe object's identity code in hex notation*/

public staticString getIdentityHexString(Object obj) {returnInteger.toHexString(System.identityHashCode(obj));

}/*** Return a content-based String representation if {@codeobj} is

* not {@codenull}; otherwise returns an empty String.

*

Differs from {@link#nullSafeToString(Object)} in that it returns

* an empty String rather than "null" for a {@codenull} value.

*@paramobj the object to build a display String for

*@returna display String representation of {@codeobj}

*@see#nullSafeToString(Object)*/

public staticString getDisplayString(Object obj) {if (obj == null) {returnEMPTY_STRING;

}returnnullSafeToString(obj);

}/*** Determine the class name for the given object.

*

Returns {@code"null"} if {@codeobj} is {@codenull}.

*@paramobj the object to introspect (may be {@codenull})

*@returnthe corresponding class name*/

public staticString nullSafeClassName(Object obj) {return (obj != null ?obj.getClass().getName() : NULL_STRING);

}/*** Return a String representation of the specified Object.

*

Builds a String representation of the contents in case of an array.

* Returns {@code"null"} if {@codeobj} is {@codenull}.

*@paramobj the object to build a String representation for

*@returna String representation of {@codeobj}*/

public staticString nullSafeToString(Object obj) {if (obj == null) {returnNULL_STRING;

}if (obj instanceofString) {return(String) obj;

}if (obj instanceofObject[]) {returnnullSafeToString((Object[]) obj);

}if (obj instanceof boolean[]) {return nullSafeToString((boolean[]) obj);

}if (obj instanceof byte[]) {return nullSafeToString((byte[]) obj);

}if (obj instanceof char[]) {return nullSafeToString((char[]) obj);

}if (obj instanceof double[]) {return nullSafeToString((double[]) obj);

}if (obj instanceof float[]) {return nullSafeToString((float[]) obj);

}if (obj instanceof int[]) {return nullSafeToString((int[]) obj);

}if (obj instanceof long[]) {return nullSafeToString((long[]) obj);

}if (obj instanceof short[]) {return nullSafeToString((short[]) obj);

}

String str=obj.toString();return (str != null ?str : EMPTY_STRING);

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public staticString nullSafeToString(Object[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(String.valueOf(array[i]));

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(boolean[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(byte[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(char[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append("'").append(array[i]).append("'");

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(double[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(float[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(int[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(long[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}/*** Return a String representation of the contents of the specified array.

*

The String representation consists of a list of the array's elements,

* enclosed in curly braces ({@code"{}"}). Adjacent elements are separated

* by the characters {@code", "} (a comma followed by a space). Returns

* {@code"null"} if {@codearray} is {@codenull}.

*@paramarray the array to build a String representation for

*@returna String representation of {@codearray}*/

public static String nullSafeToString(short[] array) {if (array == null) {returnNULL_STRING;

}int length =array.length;if (length == 0) {returnEMPTY_ARRAY;

}

StringBuilder sb= newStringBuilder();for (int i = 0; i < length; i++) {if (i == 0) {

sb.append(ARRAY_START);

}else{

sb.append(ARRAY_ELEMENT_SEPARATOR);

}

sb.append(array[i]);

}

sb.append(ARRAY_END);returnsb.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值