ToString (), string,string.valueof

Source: Internet
Author: User
Tags locale lowercase object object string format stringbuffer
toString (), string,string.valueof
Reference: The difference between StringBuffer and string http://t240178168.iteye.com/blog/1667215  Common methods are object#tostring (), (String) objects to be converted, string.valueof (object), and so on.

The following is an analysis of these methods.

Method 1: Using the Object#tostring () method

Take a look at the following example:

Object object = GetObject ();
System.out.println (Object.ToString ());

In this use method, because there is already a public method in the Java.lang.Object class. ToString (), you can call this method for any Java object in a strict sense. Note, however, that you must ensure that object is not a null value, or you will throw a NullPointerException exception when used. When this method is used, the derived class usually overwrites the ToString () method in object.

Method 2: Use the type conversion (String) object method

This is the standard type conversion, which converts object to a string type value. When using this method, it is important to note that the type must be able to be converted to a string type. Therefore, it is best to use instanceof to do a type check to determine whether it can be converted. Otherwise, it is easy to throw calsscastexception exceptions. In addition, it is necessary to be particularly careful that a grammar check does not cause an error when the object is defined as objects of type object when it is turned into a string, which can lead to potential errors. Be extra careful at this time.

such as: Object obj = new Integer (100);
String strval = (string) obj;

An error will occur at run time because the integer type is cast to a string type and cannot be passed.

However, the integer obj = new integer (100);
String strval = (string) obj;
In the case of a format code, a syntax error will be reported. In addition, a null value can be cast to any Java class type, and (String) NULL is also legal.

Method 3: Using String.valueof (Object)

String.valueof (Object) is based on object#tostring (). But it differs from object#tostring (). In the analysis of previous Method 1, it was mentioned that the latter should be used with a guarantee of not being null. However, when using the third method, you will not have to worry about whether the object is a null value.

To illustrate the problem, let's analyze the relevant source code.

JDK string# valueof (Object) source code is as follows:

public static String valueof (Object obj)
{
return (obj = null)? "Null": obj.tostring ();
}

From the above source can be very clear to see null values do not worry about the reasons. However, this also gives us the hidden danger exactly.
We should note that when object is null, the value of String.valueof (object) is the string "null" rather than null ... Be mindful of the use of the process.

We use

if (string.valueof (object) ==null) {
System.out.println ("The value passed in is null.) ”);
}
What might be the problem with such a statement. Again, when you output to the console, visually the following statements differ in the results of execution:
SYSTEM.OUT.PRINTLN (string.valueof (null));
SYSTEM.OUT.PRINTLN (NULL);
The output we see will be exactly the same thing: null, but they don't mean the same.




usage Collection of string Blog Categories:Java String construction Method:
String ()
Initializes a newly created String object that represents a sequence of empty characters.
String (byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default character set.
String (byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified child array of bytes using the specified character set.
String (byte[] bytes, int offset, int length, String charsetname)
Constructs a new String by decoding the specified child array of bytes using the specified character set.
String (byte[] bytes, string charsetname)
Constructs a new String by decoding the specified byte array with the specified character set.
String (char[] value)
Assigns a new String that represents the sequence of characters contained in the current character array parameter.
String (char[] value, int offset, int count)
Assigns a new String that contains the characters from a single child array of the character array argument.
String (int[] codepoints, int offset, int count)
Assigns a new String that contains the characters of one of the child arrays of the Unicode code point group argument.
String (string original)
Initializes a newly created string object that represents a sequence of characters that is the same as the parameter; in other words, the newly created string is a copy of the parameter string.
String (StringBuffer buffer)
Assigns a new string containing the sequence of characters currently contained in the string buffer argument.
String (StringBuilder builder)
Assigns a new string containing the sequence of characters currently contained in the String builder argument
Method Summary:
char charAt (int index)
Returns the char value at the specified index.
int codepointat (int index)
Returns the character (Unicode code point) at the specified index.
int codepointbefore (int index)
Returns the character before the specified index (Unicode code point).
int codepointcount (int beginindex, int endindex)
Returns the number of Unicode code points in the specified text range for this String.
int CompareTo (String anotherstring)
Compares two strings in dictionary order (case-sensitive).
int comparetoignorecase (String str)
Compares two strings in dictionary order, regardless of case.
String concat (String str)
The specified string is linked to the end of this string.
Boolean contains (Charsequence s)
Returns true if and only if this string contains a specified sequence of char values.
Boolean contentequals (Charsequence CS)
Returns true if and only if this String represents the same char value as the specified sequence.
Boolean contentequals (StringBuffer SB)
Returns true if and only if this String represents the same sequence of characters as the specified stringbuffer.
Static String copyvalueof (char[] data)
Returns a string representing the sequence of characters in the specified array.
Static String copyvalueof (char[] data, int offset, int count)
Returns a string representing the sequence of characters in the specified array.
Boolean endsWith (String suffix)
Tests whether this string ends with the specified suffix.
Boolean equals (Object anobject)
Compares this string with the specified object.
Boolean equalsignorecase (String anotherstring)
Compares this string to another string, regardless of case.
Static String format (Locale L, String format, Object ... args)
Returns a formatted string using the specified locale, format string, and parameters.
static string format (string format, Object ... args)
Returns a formatted string using the specified format string and parameters.
Byte[] GetBytes ()
Decodes this String into a sequence of bytes using the platform default character set and stores the result in a new byte array.
Byte[] GetBytes (String charsetname)
Decodes this String into a sequence of bytes using the specified character set and stores the result in a new byte array.
void GetChars (int srcbegin, int srcend, char[] DST, int dstbegin)
Copies the character from this string to the destination character array.
int Hashcode ()
Returns the hash code for this string.
int indexOf (int ch)
Returns the index at which the specified character appears for the first time in this string.
int indexOf (int ch, int fromindex)
Searches from the specified index, returning the index at the specified character for the first time in this string.
int indexOf (String str)
Returns the index of the first occurrence of the specified substring in this string.
int indexOf (String str, int fromindex)
Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
String Intern ()
Returns a normalized representation of a string object.
int lastindexof (int ch)
Returns the index of the last occurrence of the specified character in this string.
int lastindexof (int ch, int fromindex)
Returns the index of the last occurrence of the specified character in this string, starting at the specified index.
int LastIndexOf (String str)
Returns the index of the specified substring that appears at the far right of this string.
int LastIndexOf (String str, int fromindex)
Returns the index of the specified substring that was last seen in this string, starting with a backward search at the specified index.
int Length ()
Returns the length of this string.
Boolean matches (String regex)
Notifies this string whether a given regular expression is matched.
int offsetbycodepoints (int index, int codepointoffset)
Returns the index of the Codepointoffset code point offset from the given index in this String.
Boolean Regionmatches (boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
Boolean regionmatches (int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
String replace (char Oldchar, char Newchar)
Returns a new string that is generated by replacing all the oldchar that appear in this string with Newchar.
String replace (charsequence target, charsequence replacement)
Replaces each substring of this string matching a literal target sequence with the specified literal substitution sequence.
String ReplaceAll (string regex, string replacement)
Replaces each substring of the given regular expression with this string, using the given replacement string.
String Replacefirst (string regex, string replacement)
Replaces the string with the given replacement string to match the first substring of the given regular expression.
String[] Split (String regex)
Splits this string according to the match of the given regular expression.
String[] Split (String regex, int limit)
Splits this string according to the given regular expression.
Boolean startswith (String PRefix)
Tests whether this string starts with the specified prefix.
Boolean startswith (String prefix, int toffset)
Tests whether this string starts with the specified prefix, starting with the specified index.
Charsequence subsequence (int beginindex, int endindex)
Returns a new sequence of characters, which is a subsequence of this sequence.
String substring (int beginindex)
Returns a new string that is a substring of this string.
String substring (int beginindex, int endindex)
Returns a new string that is a substring of this string.
Char[] ToCharArray ()
Converts this string to a new character array.
String toLowerCase ()
Use the default locale's rules to convert all characters in this String to lowercase.
String toLowerCase (Locale Locale)
Converts all characters in this String to lowercase using the given Locale rule.
String toString ()
Returns the object itself (it is already a string.) )。
String toUpperCase ()
Use the default locale's rules to convert all characters in this String to uppercase.
String toUpperCase (Locale Locale)
Converts all characters in this String to uppercase using the given Locale rule.
String Trim ()
Returns a copy of the string, ignoring leading and trailing blanks.
Static String valueof (Boolean B)
Returns the string representation of a Boolean argument.
Static String valueof (char c)
Returns the string representation of the char parameter.
Static String valueof (char[] data)
Returns a string representation of a char array parameter.
Static String valueof (char[] data, int offset, int count)
Returns a string representation of a specific child array of a char array parameter.
Static String valueof (double D)
Returns a string representation of the double parameter.
Static String valueof (float f)
Returns the string representation of the float parameter.
static String valueof (int i)
Returns the string representation of the int parameter.
Static String valueof (long L)
Returns a string representation of the long argument.
Static String valueof (Object obj)
Returns the string representation of the Object parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.