HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Convert int to String in Java

Convert int to String in Java

To convert int to String, use either String.valueOf() or Integer.toString() method.

1. Convert int to String – String.valueOf()

String.valueOf(int i) method returns the string representation of the int argument. The representation is exactly the one returned by the Integer.toString() method of one argument.

1.1. Syntax

/**
 * @param   i  - an int value.
 * @return  a  - string representation of the int argument.
 */
public static String valueOf(int i)

1.2. int to String Example

int countInt = 40;

String countStr = String.valueOf( countInt );

2. Convert int to String – Integer.toString()

Integer.toString(int i) method Returns a string object representing the specified integer passed as method argument. By default, argument is converted to signed decimal (radix 10) in string format.

2.1. Syntax

/**
 * @param   i  - an int value.
 * @return  a  - string representation of the int argument in base 10
 */
public static String toString(int i)

2.2. int to String Example

int countInt = 40;

String countStr = Integer.toString( countInt );

3. Convert Integer to String

To convert Integer object to String, simply call method toString() on integer object.

Integer year = new Integer(2018);

String yearString = year.toString();

4. Java example to convert int to String value

This example shows how to use above both methods i.e. String.valueOf() and Integer.toString() to convert a given integer value to string value.

In second part, it gives example to convert Integer object to String representation.

public class StringExample 
{
	public static void main(String[] args) 
	{
		
		// 1. Converting int value to String value
		
		int intParam = 1001;
		
		String strValue1 = String.valueOf(intParam);
		
		String strValue2 = Integer.toString(intParam);
		
		
		
		// 2. Converting Integer object to String value
		
		Integer integerParam = new Integer(2018);
		
		String strValue3 = String.valueOf(integerParam);
		
		String strValue4 = integerParam.toString();
		
		
		
		//Verify results
		
		System.out.println(strValue1);
		System.out.println(strValue2);
		System.out.println(strValue3);
		System.out.println(strValue4);
	}
}

Program Output:

1001
1001

2018
2018

Checkout this example to convert String to int values.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces