基础类型和封装类互相转换
int i = 5; //基本类型转换成封装类型 Integer it = new Integer(i); //封装类型转换成基本类型 int i2 = it.intValue();
自动装箱拆箱
int i = 5;
//基本类型转换成封装类型
Integer it = new Integer(i);
//自动转换就叫装箱
Integer it2 = i;
int i = 5;
Integer it = new Integer(i);
//封装类型转换成基本类型
int i2 = it.intValue();
//自动转换就叫拆箱
int i3 = it;String和Integer转换
1 如何将字串 String 转换成整数 int?
A. 有两个方法:
1、 int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]);//直接使用静态方法,不会产生多余的对象,但会抛出异常 2、 int i = Integer.valueOf(my_str).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:
1、String s = String.valueOf(i);//直接使用String类的静态方法,只产生一个对象 2、String s = Integer.toString(i); 3、String s = "" + i;//会产生两个String对象
注: Double, Float, Long 转成字串的方法大同小异.
import java.util.*;
import java.lang.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
System.out.println(Integer.valueOf(str.substring(2),16).toString());
}
}
}
京公网安备 11010502036488号