Java Coercion Type Conversion summary

Source: Internet
Author: User
Tags wrapper

Some summaries of object objects converted to string

--------------------------------------------------------------------------------


In the actual development and application of Java projects, it is often necessary to use the basic function of converting objects to string. This article will make a summary of the common conversion methods. 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:

1 object = GetObject ();

Note 1


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

A rule is that a particular connection must be between a transformed type.
such as:
string "123", only a number, can be directly integer.valueof ("123");
The "a" of the char type can be directly "int" ("a"), and the result is: "instance" (instantiated) can be used to
determine whether the conversion can be cast: if ("AAAA" instance of String) {// Code module}, this code means that if "aaaa" can instantiate a string type, it can be cast, which can reduce the occurrence of a type mismatch and the resulting conversion type throws an exception, the development process still has some effect.
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. But

Integer obj = new integer (100);
String strval = (string) obj;


A syntax error will be reported as the above format code.
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 objec.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. Imagine if we use the IF (String.valueof (object) ==null) {System.out.println ("incoming value 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 ((Object) null) (string.valueof); NOTE 2
SYSTEM.OUT.PRINTLN (NULL);


The output we see will be exactly the same: null, but do they mean the same thing?
Note 1: Light on the use of SYSTEM.OUT.PRINTLN, the statement System.out.println (Object.ToString ()); Change to System.out.println (object); Better. The use here is primarily to illustrate the use of object#tostring ().
NOTE 2: The recommended use of System.out.println (string.valueof (Object) null); It is not recommended to use SYSTEM.OUT.PRINTLN (string.valueof (null)), which is a good habit when using overloaded methods.
These are some of the summaries of converting object objects to strings.
The new integer.valueof () returns an Integer object.
Integer.parseint () Returns the value of an int.

The new Integer.valueof (). Intvalue (), and returns the value of an int.
Integer.valueof (String s) is a wrapper class that converts a variable with an actual value of a number to a String and then converts it to an integer wrapper class object (equivalent to an int) so that objects that have been turned off have methods and properties.
and Integer.parseint (string s) simply converts a string of numbers into numbers, noting that he returns an int variable without methods and properties.
1 strings converted to data
String to Integer:

String MyNumber = "1234";
int MyInt = Integer.parseint (MyNumber);


The string converts to a data type of byte, short, int, float, double, long, and can be referred to as Byte, short, Integer, float, double, and long parsexxx methods.
2 Data converted to string
Integers into strings:

int MyInt = 1234;
String MyString = "" + MyInt;


Other data types can be converted to strings using the same method.
3 decimal Conversions to other systems
The decimal integer is converted to a binary integer, and the result is a string:
integer.tobinarystring (int i);
Integers and long provide the tobinarystring, tohexstring, and tooctalstring methods that make it easy to convert data into binary, hexadecimal, and octal strings. A more powerful feature is its ToString (int/long i, int radix) method, which converts a decimal number into a string of arbitrary characters.
Data types such as Byte, short, float, and double, which can take advantage of an integer or a long tobinarystring, tohexstring, to The Octalstring and ToString methods are converted into string forms in other systems.
4 conversion to decimal in other systems
The binary string 14414 is converted to a decimal integer, and the result is 1234:

System.out.println (integer.valueof ("14414", 5);


The valueof (string source, int radix) method provided by integer and long enables you to convert arbitrary strings into decimal data.
5 integer to byte array conversion?

1 2 3 4 5 6 7 8 9 ten public  static  Byte [] Tobytearray (INT&N Bsp Number) {    int  temp = number;     byte [] b= new  byte [4];     ; for  (

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.