• Home
  • MAD
    • IOS Series
    • Android Series
    • Flutter Series
    • Xamarin Series
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
  • Home
  • MAD
    • IOS Series
    • Android Series
    • Flutter Series
    • Xamarin Series
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • MAD
    • IOS Series
    • Android Series
    • Flutter Series
    • Xamarin Series
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
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

Liked it? Take a second to support Ranjan on Patreon!
become a patron button
  • 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 Ranjan - 1652 Views
Tags | JAVA
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, 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

mycodetips
mycodetips

Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

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

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com