java包装类——Integer



1、parseInt()  

String[] str = {"12","23","45"};
  int sum = 0;
  for(int i=0;i<str.length;i++){
   int myint = Integer.parseInt(str[i]);
   sum = sum+myint;
  }
  System.out.print("数组各元素之和是"+sum);


public static int parseInt(String s)
                    throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ( '\u002D') to indicate a negative value or an ASCII plus sign '+' ( '\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

2、

  String str1 = Integer.toString(255);
  String str2 = Integer.toBinaryString(255);
  String str3 = Integer.toHexString(255);
  String str4 = Integer.toOctalString(255);
  System.out.print("256十进制、二进制、十六进制、八进制分别表示为:"+str1+"、"+str2+"、"+str3+"、"+str4);

运行结果:

256十进制、二进制、十六进制、八进制分别表示为:255、11111111、ff、377

toString
public static String toString(int i,
              int radix)
Returns a string representation of the first argument in the radix specified by the second argument.

If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.

If the first argument is negative, the first element of the result is the ASCII minus character '-' ('\u002D'). If the first argument is not negative, no sign character appears in the result.

The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the magnitude will not be the zero character. The following ASCII characters are used as digits:

0123456789abcdefghijklmnopqrstuvwxyz
These are '\u0030' through '\u0039' and '\u0061' through '\u007A'. If radix is N, then the first N of these characters are used as radix- N digits in the order shown. Thus, the digits for hexadecimal (radix 16) are 0123456789abcdef. If uppercase letters are desired, the String.toUpperCase() method may be called on the result:
Integer.toString(n, 16).toUpperCase()

Parameters:
i - an integer to be converted to a string.
radix - the radix to use in the string representation.
Returns:
a string representation of the argument in the specified radix.
See Also:
Character.MAX_RADIX, Character.MIN_RADIX


  • toHexString
    public static String toHexString(int i)
    Returns a string representation of the integer argument as an unsigned integer in base 16.

    The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as hexadecimal digits:

    0123456789abcdef
    These are the characters '\u0030' through '\u0039' and '\u0061' through '\u0066'. If uppercase letters are desired, the String.toUpperCase() method may be called on the result:
    Integer.toHexString(n).toUpperCase()

    Parameters:
    i - an integer to be converted to a string.
    Returns:
    the string representation of the unsigned integer value represented by the argument in hexadecimal (base 16).
    Since:
    JDK1.0.2


  • toOctalString
    public static String toOctalString(int i)
    Returns a string representation of the integer argument as an unsigned integer in base 8.

    The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in octal (base 8) with no extra leading 0s.

    If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as octal digits:

    01234567
    These are the characters '\u0030' through '\u0037'.

    Parameters:
    i - an integer to be converted to a string.
    Returns:
    the string representation of the unsigned integer value represented by the argument in octal (base 8).
    Since:
    JDK1.0.2


  • toBinaryString
    public static String toBinaryString(int i)
    Returns a string representation of the integer argument as an unsigned integer in base 2.

    The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters '0' ('\u0030') and '1' ('\u0031') are used as binary digits.

    Parameters:
    i - an integer to be converted to a string.
    Returns:
    the string representation of the unsigned integer value represented by the argument in binary (base 2).
    Since:
    JDK1.0.2


  • toString
    public static String toString(int i)
    Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
    Parameters:
    i - an integer to be converted.
    Returns:
    a string representation of the argument in base 10.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值