A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© keto 中级黑马   /  2015-6-25 20:10  /  649 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

*
* String和int的相互转换:
*
* String 到  int
* 1.因为要从String转为int,所以可以去int的包转类中去找一些方法:
*                 Integer-->构造方法--> intValue()-->int
*                 Integer-->public static int parseInt(String s)--int
*                 Integer-->public static Integer valueOf(String s)-->intValue()--int
* int 到 String
*                 Integer --> public static String toString(int i)--> String
*                 String --> public static String valueOf(int i) --> String
*/
public class Demo {
        public static void main(String[] args) {
                //String -- int
                //1.
                String str = "20";
                int num = new Integer(str).intValue();
                System.out.println("num = " + num);
               
                //2.
                int num2 = Integer.parseInt(str);
                System.out.println("num2 = " + num2);
               
                //3.
                int num3 = Integer.valueOf(str).intValue();
                System.out.println("num3 = " + num3);
               
                //int -- String
               
                int value = 1000;
                String s = Integer.toString(value);
                System.out.println("s = " + s);
               
                String s2 = String.valueOf(value);
                System.out.println("s2 = " + s2);
        }
}

12 个回复

倒序浏览
感谢分享!
回复 使用道具 举报
谢谢楼主!
回复 使用道具 举报
。。上午做题还用到来的。。
回复 使用道具 举报
感谢分享!
回复 使用道具 举报
学习..............................................了
回复 使用道具 举报
来学习的
回复 使用道具 举报
Morgan 中级黑马 2015-6-26 08:19:05
8#
支持,顶!!!
回复 使用道具 举报
谢谢分享!正好在学这部分!
回复 使用道具 举报
学习学习
回复 使用道具 举报
楼主分享的好,谢谢分享
回复 使用道具 举报
很详细,谢谢分享
回复 使用道具 举报
受教了,做了很久没做出来
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马