parseint (), parsefloat (), ValueOf (), Intvalue (), ToString Method Summary

Source: Internet
Author: User
Tags integer numbers

This article focuses on the conversion of numeric strings and numbers in the Java language, the conversion of integer numbers to strings, and the differences between individual methods.

1, Parsexxx () is Simpledatefomat inside the method, commonly used include parseint (), parsefloat ()
Use this method to get a specific string of the original data type. Parsexxx () is a static method that can have one parameter or two.
Grammar:
All variants of the method are given below:
static int parseint (String s)
static int parseint (String s, int radix)
Parameters:
Here are the details of the parameters:
? String S: This is the string representation of the decimal.
? int radix: This will be used to convert the string to an integer.
return value:
? parseint (String s): This returns a integer (decimal only).
? parseint (int i): This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix e Quals, 2, 8, or respectively) numbers as input.
Instance:
public class test{
public static void Main (String args[]) {
int x =integer.parseint ("9");
Double C = double.parsedouble ("5");
int B = Integer.parseint ("444", 16);
by Www.yiibai.com/java
SYSTEM.OUT.PRINTLN (x);
System.out.println (c);
System.out.println (b);
}
}
This will output the following results:
9
5.0
1092
2. ValueOf () method
The ValueOf () method, such as integer.valueof (), converts the string type to an integer type (note: It is an integer type, not an int type, and the int type is a simple type that represents a number. The integer type is a referenced complex type)
Such as:
String a= "123";
Integer c =integer.valueof (a);
The integer type can be converted to type int using the Intvalue method
int b =c.intvalue ();
This time, this b is equal to 123.

The difference between valueOf () and parseint ()
The biggest difference is that the return type is different.
The static int parseint (string s) parses the string argument as a signed decimal integer.
The static integer valueOf (int i) returns an Integer instance that represents the specified int value.
The static integer valueOf (String s) returns an integer object that holds the value of the specified String.

This can be summed up: parseint () returns the base type int, and valueof () returns the wrapper class integer. An integer can use an object method, and an int type cannot be converted to and from an object type.
3, Intvalue ()
Intvalue () is a method of the Java.lang.Number class.
Number is an abstract class. All numeric classes in Java inherit from it. That is to say, not only the integer has the Intvalue method, Double,long and so on have this method. The meaning of this method is: output int data.
The specific implementations in each numeric class are different. For example, the Intvalue method of the float class and the double class is to lose the decimal place, and the Intvalue method of long is not the same. Specific you can refer to the JDK source code!
The source code is under the JDK path. File name: Src.zip

4. ToString () can convert a reference type to string type.
For example, convert an integer to a string type:
Integer a = new integer (123);
String b =a.tostring (); At this point B is "123".

parseint (), parsefloat (), ValueOf (), Intvalue (), ToString Method Summary

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.