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,280 points
57 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,540 points
0 votes

We can do this in 2 ways:

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

answered Jul 27, 2018 by samarth295
• 2,190 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 16, 2018 in Java by samarth295
• 2,190 points
67 views
0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
113 views
+14 votes
18 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 in Java by Suresh
• 700 points
64,347 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 Apr 30, 2018 in Java by Rishabh
• 3,540 points
37 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
83 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 parth295
• 4,600 points
64 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,540 points
42 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,540 points
37 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,840 points
40 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,040 points
105 views