Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java int to String

farenda 2016-11-02 0

In Java int to String conversion can be done in a different ways and converted String can be in different formats. In this post we’ll show on examples how to convert them.

Concatenate String with int

The simplest method is to use automatic conversion of int to String using + operator:

int x = 42;
String s = "" + x;
System.out.printf("Number: '%s'%n", s);

The above code produces the following output:

Number: '42'

int to String using Integer.toString(int)

Another way to convert int to String is using Integer.toString(int) method:

int x = 42;
String s = Integer.toString(x);
System.out.printf("Number: '%s'%n", s);

The result is as expected:

Number: '42'

Java 8 map int to String

The Integer.toString(int) method seems unneeded, but it turns out that it’s very handy when working with Java 8 Streams. In the following example we’ve got a Stream of int numbers and want to convert them to Strings. Having Integer.toString(int) we can pass it as a method reference to the Stream.mapToObj(IntFunction) to do int to String conversion:

List<String> numbers = IntStream.of(1, 2, 3)
    .mapToObj(Integer::toString)
    .collect(Collectors.toList());
System.out.printf("Numbers: '%s'%n", numbers);

Stream of int converted to List of Strings:

Numbers: '[1, 2, 3]'

Integer to String conversion

If you have Integer objects you can convert them using their toString() method:

Integer x = 42;
String s = x.toString();
System.out.printf("Number: '%s'%n", s);

The code prints the same result:

Number: '42'

int to String in other base

Integer class has a number of methods to help in conversion of Integer to String in different bases – binary, octal, hexadecimal. These methods are sometimes helpful in algorithmic exercises, when there’s a need to operate on binary Strings.

int to n-based String

int x = 42;
System.out.println(Integer.toString(x, 3));

The number as 3-based String:

1120

int to binary String

int x = 42;
System.out.println(Integer.toBinaryString(x));

Prints:

101010

int to octal String

int x = 42;
System.out.println(Integer.toOctalString(x));

Prints:

52

int to Hex String

int x = 42;
System.out.println(Integer.toHexString(x));

Prints:

2a

References:

  • See Java String to int for reverse conversion
  • Java Basics Tutorial
Share with the World!
Categories Java Tags java, java-basics, java8
Previous: Java String to int
Next: log4j example configuration – console logger

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored