Home >Java >javaTutorial >What does valueof mean in java
The valueOf method in Java converts a string, primitive type, or object to its corresponding wrapped type: wrap a string into a wrapper type, such as converting "123" to an Integer type. Convert a base type to a wrapper type, such as int 123 to an Integer type. Convert an object to a wrapper type, such as converting a MyObject object to the Object type.
valueOf
in Java valueOf
is a commonly used static method in Java , used to convert strings, primitive types, and objects to their corresponding wrapped types.
valueOf
method wraps the specified value into its corresponding packaging type, such as:
String
Type: String.valueOf()
Integer
Type: Integer.valueOf()
Float
Type: Float.valueOf()
valueOf
The syntax of the method is as follows:
<code class="java">public static <T> T valueOf(String value)</code>
Among them:
is the generic type of the packaging type
is the string value to be converted
Integer Type:
<code class="java">Integer num = Integer.valueOf("123"); System.out.println(num); // 输出:123</code>Convert the basic type to the wrapped type Convert the value 123 of type int to
Integer Type:
<code class="java">Integer num = Integer.valueOf(123); System.out.println(num); // 输出:123</code>Convert object to wrapper typeConvert an object of type
MyObject to
Object type:
<code class="java">Object obj = Object.valueOf(myObject);</code>Note
method can only convert strings to wrapped types, not to primitive types.
method will throw a
NumberFormatException exception.
The above is the detailed content of What does valueof mean in java. For more information, please follow other related articles on the PHP Chinese website!