Integer to String conversion in java

0 votes

I'm learning to work on strings in java. This is a code I got from an online source.

int i = 5;
String strI = "" + i;

I'm not familiar with Java. Is this the way to convert int to string or its just and unorthodox way to do so ?

Apr 13, 2018 in Java by developer_1
• 3,350 points
1,194 views

2 answers to this question.

0 votes
int i = 5;
String strI = "" + i;
Although the above mentioned method does convert an integer to string but there are other ways too for doing the same task:
1. int i = 5;
   String strI = Integer.toString(i); #Converts an integer to string
2. int i = 5;
   String strI = String.valueOf(i); #Gets a string representation of an object (integer)

answered Apr 13, 2018 by Rishabh
• 3,620 points
0 votes

We can do this in 2 ways:

String strI = String.valueOf(i);
String string = Integer.toString(i);

answered Jul 28, 2018 by samarth295
• 2,220 points

Related Questions In Java

0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
1,574 views
0 votes
2 answers
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,640 views
0 votes
1 answer

How to pad string in Java?

Padding to 10 characters: String.format("%10s", "foo").replace(' ', '*'); String.format("%-10s", ...READ MORE

answered May 1, 2018 in Java by Rishabh
• 3,620 points
1,038 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,315 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,640 points
3,061 views
0 votes
1 answer

Formatting Double Value in Java

An alternative is to use String.format: double[] arr = ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
2,502 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
1,194 views
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,920 points
1,729 views
0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,140 points
3,960 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP