• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Interview
  • #Tips

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
JAVA

Typecasting : How to Convert ‘int’ to ‘String’ in JAVA

Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.

It is important to ensure that the casting is in line with run-time rules as well as compile-time rules in Java. Reference type casting is further divided into two types:

Upcasting

Upcasing involves the conversion of an object of SubType into an object of SuperType. Java has the provision for assigning the object without requiring the addition of an explicit cast. The compiler would know what is being done and would cast the SubType value to SuperType. This way, the object is brought to a basic level. Developers can add an explicit cast without worrying about any issues.

Downcasting

Downcasting involves the conversion of a SuperType object into a Subtype object. It is the most frequently used casting wherein it is communicated to the compiler that the value of the base object isn’t it’s own but of the SuperType object.

String.valueOf()

The String.valueOf() method converts int to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:

public static String valueOf(int i)
Java int to String Example using String.valueOf()
Let’s see the simple code to convert int to String in java.

int i=10;
String s=String.valueOf(i);//Now it will return “10”
Let’s see the simple example of converting String to int in java.

public class IntToStringExample1{
public static void main(String args[]){
int i=200;
String s=String.valueOf(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}

Integer.toString()

The Integer.toString() method converts int to String. The toString() is the static method of Integer class. The signature of toString() method is given below:

public static String toString(int i)
Java int to String Example using Integer.toString()
Let’s see the simple code to convert int to String in java using Integer.toString() method.

int i=10;
String s=Integer.toString(i);//Now it will return “10”
Let’s see the simple example of converting String to int in java.

public class IntToStringExample2{
public static void main(String args[]){
int i=200;
String s=Integer.toString(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}

String.format()

The String.format() method is used to format given arguments into String. It is introduced since Jdk 1.5.

public static String format(String format, Object… args)
Java int to String Example using String. format()
Let’s see the simple code to convert int to String in java using String. format() method.

public class IntToStringExample3{
public static void main(String args[]){
int i=200;
String s=String.format("%d",i);
System.out.println(s);
}}

Happy Coding

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Admin Blogger - June 14, 2021 - 1932 Views
Tags | JAVA
AUTHOR
Admin Blogger

This website is basically about of what we learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures and its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

FloatingPointNumbers

What is Floating-Point Operations

September 2, 2019
Next Post
Previous Post

Support us

Subscribe for updates

Join 8,276 other subscribers

Latest Posts

  • primitive-datatypes-new
    Exploring the Pros and Cons of Primitive Data Types
  • best practice clean code
    Essential Coding Standards and Best Practices for Clean Code
  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
  • Designing Mobile APPS
    Limitation and restriction of designing mobile apps
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

.Net Android Blogging Cloud Concept Database DSA ERROR ExcelTips Flutter Interview IOS IOSQuestions JAVA Javascript MAC-OS No-Mouse Objective-c Programming Quick Tips SEO Software Design & Architecture Swift SwiftUI Tips&Tricks Tools Troubleshoot Videos Web Wordpress Xamarin XCode

  • Exploring the Pros and Cons of Primitive Data Types
  • Essential Coding Standards and Best Practices for Clean Code
  • What Business Problems Solves in Low Level Design (LLD)
  • SRP : Single Responsibility Principle in Swift and Objective-C
  • Limitation and restriction of designing mobile apps
MyCodeTips

©mycodetips.com