Integer toString() in Java Last Updated : 05 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The java.lang.Integer.toString() is an inbuilt method in Java which is used to return the String object representing this Integer's value. Syntax : public static String toString() Parameters: The method does not accept any parameters. Return Value:The method returns the string object of the particular Integer value. Below program illustrates the Java.lang.Integer.toString() method: java // Java program to illustrate the // toString() Method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer obj = new Integer(8); //It will return a string representation String stringvalue1 = obj.toString(); System.out.println("String Value= " + stringvalue1); Integer obj3 = new Integer(10); //It will return a string representation String stringvalue3 = obj3.toString(); System.out.println("String Value = " + stringvalue3); } } Output: String Value= 8 String Value = 10 The java.lang.Integer.toString(int a) is an inbuilt method in Java which is used to return a String object, representing the specified integer in the parameter. Syntax : public static String toString(int a) Parameters: The method accepts one parameter a of integer type and refers to the integer needed to be converted to string. Return Value: The method returns the string representation of the argument in a particular base. Examples: For base 8: Input: int a = 75 Output: "75" For base 10: Input: int a = -787 Output: "-787" Below programs illustrate the Java.lang.Integer.toString(int a) method: Program 1: java // Java program to illustrate the // toString(int a) method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer obj = new Integer(8); // It will return a string representation // in base 8 String stringvalue1 = obj.toString(75); System.out.println("String Value = " + stringvalue1); Integer obj2 = new Integer(8); // It will return a string representation // in base 2 String stringvalue2 = obj2.toString(6787); System.out.println("String Value = " + stringvalue2); Integer obj3 = new Integer(10); // It will return a string representation // in base 10 String stringvalue3 = obj3.toString(-787); System.out.println("String Value = " + stringvalue3); } } Output: String Value = 75 String Value = 6787 String Value = -787 Program 2: For decimal and string parameters. Note: This results in an error and as well for the absence of suitable Integer constructor. java // Java program to illustrate the // Java.lang.Integer.toString(int a)method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer obj = new Integer(8); String stringvalue1 = obj.toString(58.5); System.out.println("String Value = " + stringvalue1); Integer obj2 = new Integer(8); String stringvalue2 = obj2.toString("317"); System.out.println("String Value = " + stringvalue2); // Empty constructor will result in an error Integer obj3 = new Integer(); String stringvalue3 = obj3.toString(-787); System.out.println("String Value = " + stringvalue3); } } Output: prog.java:8: error: incompatible types: possible lossy conversion from double to int String stringvalue1 = obj.toString(58.5); ^ prog.java:12: error: incompatible types: String cannot be converted to int String stringvalue2 = obj2.toString("317"); ^ prog.java:17: error: no suitable constructor found for Integer(no arguments) Integer obj3 = new Integer(); ^ constructor Integer.Integer(int) is not applicable (actual and formal argument lists differ in length) constructor Integer.Integer(String) is not applicable (actual and formal argument lists differ in length) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 3 errors The java.lang.Integer.toString(int a, int base) is an inbuilt method in Java which is used to return a string representation of the argument a in the base, specified by the second argument base. If the radix/base is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the base 10 is used. The ASCII characters which are used as digits: 0 to 9 and a to z. Syntax: public static String toString(int a, int base) Parameter: The method accepts two parameters: a: This is of integer type and refers to the integer value that is to be converted. base: This is also of integer type and refers to the base that is to be used while representing the strings. Return Value: The method returns a string representation of the specified argument in the specified base. Examples: Input: a = 71, base = 2 Output: 1000111 Input: a = 314, base = 16 Output: 13a Below programs illustrate the Java.lang.Integer.toString(int a, int base) Method: Program 1: java // Java program to illustrate the // toString(int, int) Method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer a = new Integer(10); // It returns a string representation // in base 2 String returnvalue = a.toString(5254, 2); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 8 returnvalue = a.toString(35, 8); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 16 returnvalue = a.toString(47, 16); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 10 returnvalue = a.toString(451, 10); System.out.println("String Value = " + returnvalue); } } Output: String Value = 1010010000110 String Value = 43 String Value = 2f String Value = 451 Program 2: java // Java program to illustrate the // toString(int, int) Method import java.lang.*; public class Geeks{ public static void main(String[] args) { Integer a = new Integer(10); // It returns a string representation // in base 2 String returnvalue = a.toString(-525, 2); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 8 returnvalue = a.toString(31, 8); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 16 returnvalue = a.toString(28, 16); System.out.println("String Value = " + returnvalue); // It returns a string representation // in base 10 returnvalue = a.toString(29, 10); System.out.println("String Value = " + returnvalue); } } Output: String Value = -1000001101 String Value = 37 String Value = 1c String Value = 29 Comment More infoAdvertise with us Next Article Integer toString() in Java A ankita_chowrasia Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Integer Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Like