Today we will look at Java String to int conversion and then java int to String conversion. Java provides several ways to convert String to int and int to String.

今天,我们将看一下Java String到int的转换,然后是java int到String的转换。 Java提供了几种将String转换为int以及将int转换为String的方法。

Java字符串到int (Java String to int)

  • Using Integer.parseInt method使用Integer.parseInt方法
  • Using Integer.valueOf method使用Integer.valueOf方法
  • Using above methods with different radix.使用上述方法使用不同的基数。

Java String to int示例 (Java String to int example)

Let’s write a simple java program to convert String to an int, note that String is an Object whereas int is a primitive data type. An integer can be written in Decimal, Hexadecimal, Octal Binary or any other nonstandard formats.

让我们编写一个简单的Java程序将String转换为int,注意String是一个Object,而int是原始数据类型。 整数可以十进制,十六进制,八进制二进制或任何其他非标准格式写入。

Java 7 one of the feature is Binary Literals. In this example, we will explore how to convert String to int using different radix values.

Java 7的功能之一是Binary Literals 。 在此示例中,我们将探讨如何使用不同的基数值将String转换为int。

package com.journaldev.string;public class JavaStringToInt {public static void main(String[] args) {String str = "10";int x = Integer.parseInt(str);System.out.println("Java String to int value using Integer.parseInt() = "+x);int y = Integer.valueOf(str);System.out.println("Java String to int value using Integer.valueOf() = "+y);String binary = "0100"; //4int a = Integer.valueOf(binary, 2);int b = Integer.parseInt(binary, 2);System.out.println("Java Binary String to int value = "+a);System.out.println("Java Binary String to int value = "+b);String octal = "0100"; //64int c = Integer.valueOf(octal, 8);int d = Integer.parseInt(octal, 8);System.out.println("Java Octal String to int value = "+c);System.out.println("Java Octal String to int value = "+d);String hexadecimal = "0100"; //256int e = Integer.valueOf(hexadecimal, 16);int f = Integer.parseInt(hexadecimal, 16);System.out.println("Java hexadecimal String to int value = "+e);System.out.println("Java hexadecimal String to int value = "+f);String customRadix = "0100"; //25int g = Integer.valueOf(customRadix, 5);int h = Integer.parseInt(customRadix, 5);System.out.println("Java String with radix 5 to int value = "+g);System.out.println("Java String with radix 5 to int value = "+h);}}

Output of the above program is:

上面程序的输出是:

Java String to int value using Integer.parseInt() = 10
Java String to int value using Integer.valueOf() = 10
Java Binary String to int value = 4
Java Binary String to int value = 4
Java Octal String to int value = 64
Java Octal String to int value = 64
Java hexadecimal String to int value = 256
Java hexadecimal String to int value = 256
Java String with radix 5 to int value = 25
Java String with radix 5 to int value = 25
  • Interegr.parseInt(String str) is the most commonly used method to convert String to int, if String represents int in decimal format. Internally, it invokes method Interegr.parseInt(String str, int radix) with radix value as 10.如果String以十进制格式表示int,则Interegr.parseInt(String str)是将String转换为int的最常用方法。 在内部,它调用方法Interegr.parseInt(String str, int radix) ,基数为10。
  • By passing redix value as 2, we can convert Binary int representation String to int, same is true for other known formats Octal, Hexadecimal also.通过将redix值传递为2,我们可以将Binary int表示形式的String转换为int,对于其他已知格式Octal,十六进制也是如此。
  • Minimum radix value is 2 and maximum radix value is 36. For radix 36, it includes 0 to 9 and then A(10) to Z(36). The conversion from String to int is case insensitive.最小基数为2,最大基数为36。对于基数36,它包括0到9,然后包括A(10)到Z(36)。 从String到int的转换不区分大小写。
  • Integer.valueOf(String str) is also a way to convert String to int. There is also an overloaded method where we can pass radix, as you can see from above example code.Integer.valueOf(String str)也是一种将String转换为int的方法。 从上面的示例代码可以看到,还有一个重载的方法可以传递基数。
  • If the String representation does not contain a parsable integer, both above methods throw NumberFormatException.如果String表示形式不包含可分析的整数,则上述两种方法都将引发NumberFormatException
  • The String representation should only contain the value and not the radix prefix associated with int, like 0x, 0b etc.字符串表示形式应仅包含值,而不应包含与int相关联的基数前缀,例如0x,0b等。

Java int转换为String (Java int to String)

  • Using + operator to concatenate int to string.使用+运算符将int连接到字符串。
  • Using String.valueOf method.使用String.valueOf方法。
  • Using String.format method使用String.format方法

Java int到String的示例 (Java int to String example)

Let’s write a simple program to convert java int to String object using different methods.

让我们编写一个简单的程序,使用不同的方法将java int转换为String对象。

package com.journaldev.string;public class JavaIntToString {public static void main(String[] args) {int i = 10;int j = 0xEF;int k = 0123;int l = 0b111;String str = "" + i;String str1 = String.valueOf(j);String str2 = String.format("%d", k);String str3 = "" + l;System.out.println(i + " decimal int to String " + str);System.out.println(j + " hexadecimal int to String " + str1);System.out.println(k + " octal int to String " + str2);System.out.println(l + " binary int to String " + str3);}}

The output of the above program is:

上面程序的输出是:

10 decimal int to String 10
239 hexadecimal int to String 239
83 octal int to String 83
7 binary int to String 7
  • +” operator is overloaded for String and it can be used to convert int to String. It also takes care of different formats like Hexadecimal, Octal, Binary and convert them to decimal format before returning the final String object.String的“ + ”运算符已重载,可用于将int转换为String。 它还照顾不同的格式,例如十六进制,八进制,二进制,并在返回最终的String对象之前将它们转换为十进制格式。
  • String.valueOf(int i) can be used to convert int to String. valueOf() method is overloaded to accept float, long, double etc. Internally it uses Integer.toString(int i) method to return the String object.String.valueOf(int i)可用于将int转换为String。 valueOf()方法已重载以接受float,long,double等。在内部,它使用Integer.toString(int i)方法返回String对象。
  • String.format(String format, Object... args) is also another way to convert int to String object.String.format(String format, Object... args)也是将int转换为String对象的另一种方法。
  • Before converting int to string, value is always converted to decimal format and then assigned to string. As you can see from the output above, all the string representations are in the decimal format.在将int转换为字符串之前,值始终会转换为十进制格式,然后再分配给字符串。 从上面的输出中可以看到,所有的字符串表示形式都是十进制格式。

That’s all for Java String to int and int to String conversion, we will cover more java interview questions in future posts.

Java字符串到int以及从int到String的转换就这些了,我们将在以后的文章中介绍更多Java面试问题 。

Reference: Integer API Doc

参考: Integer API文档

翻译自: https://www.journaldev.com/727/java-string-int

Java String到int,Java int到String相关推荐

  1. (备忘)Java数据类型中String、Integer、int相互间的转换

    1.Integer转换成int的方法 Integer i; int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Inte ...

  2. Java数据类型中String、Integer、int相互间的转换

    1.Integer转换成int的方法 Integer i;  int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Int ...

  3. java datetime转int_java日期int和String互转

    /** * 时间unix转换 * @param timestampString * @return */ public static String TimeStampDate(String times ...

  4. Java中的byte[]/char[]/int/String数据类型转换

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51743969 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  5. Java——(1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age) (2)定义Map集合,用Student对象作为key

    分析以下需求,并用代码实现: (1)定义一个学生类Student,包含属性:姓名(String name).年龄(int age) (2)定义Map集合,用Student对象作为key,用字符串(此表 ...

  6. java把string转int类型_java把String类型转换为int类型的方法

    java把String类型转换为int类型的方法 发布时间:2020-08-20 14:32:03 来源:亿速云 阅读:73 作者:小新 这篇文章将为大家详细讲解有关java把String类型转换为i ...

  7. java String类型转化为Int类型

    [将String 类型转化为int 类型] 一: public class IntegerDemo {public static void main(String[] args) {String st ...

  8. java中将string类型转int类型或者将string类型转long类型方法(亲测)

    将字串 String 转换成整数 int 两种方法: 1).int i = Integer.parseInt("111"); 或 i = Integer.parseInt([Str ...

  9. 第三次学JAVA再学不好就吃翔(part63)--String和int的相互转换

    学习笔记,仅供参考 文章目录 String和int的相互转换 将int转换为String 将String转换为int String和int的相互转换 将int转换为String 将int类型数据与St ...

  10. int char转换成string java,java中int,char,string三种类型的相互转换

    如何将字串 String 转换成整数 int? int i = Integer.valueOf(my_str).intValue(); int i=Integer.parseInt(str); 如何将 ...

最新文章

  1. 蚂蚁森林合种计划(2020.10.31,7天有效,每周更新)
  2. 基于DDD的.NET开发框架 - ABP领域服务
  3. Python学习-28.Python中的列表切片
  4. 来了,单片机最强科普总结!
  5. 好程序员分享居中一个float元素
  6. 中国互联网大佬隐退简史
  7. 曾鸣:为什么要让「听得见炮火的士兵」做决定?| 干货
  8. 如何备份MySql的数据库
  9. (1.2)mysql 索引概念
  10. 提升交互设计必备的28本好书
  11. 动画 | 什么是红黑树?(与2-3-4树等价)
  12. 浅谈大小端(Endian)与位域
  13. 微信企业号开发模式的PHP代码
  14. 12、计算机如何实现开根号?
  15. 微型计算机显示器的标准接口,显示器原理及接口显示器BIOS编程I(原理部分)
  16. ERR_NAME_NOT_RESOLVED错误的解决方法
  17. 解决百度网盘限速的软件
  18. 微软商店游戏进不去服务器,微软应用商店一登陆就出这,登陆不了
  19. python实现查询qq是否在线
  20. 黑客挂马紧盯娃娃 儿童节育儿教育网站被挂马

热门文章

  1. Spring注入静态类型
  2. 使用一个for循环将N*N的二维数组的所有值置1
  3. ORACLE 制定时间 加N月
  4. Mvc6 错误Microsoft.AspNet.Http.Features.IRequestIdentifierFeature
  5. 我的 Visual Studio . NET 配置
  6. WPF在一个窗口中实现多个视图
  7. [转载] 简单工厂模式和工厂方法模式在Python中的实现
  8. [转载] Java中final关键字
  9. python——redis
  10. python 匿名函数 day15