That should already be fast - it'll use StringBuilder internally for concatenation. Arguably using StringBuilder explicitly could eliminate the concatenation of empty strings, but it's not likely to make a big difference.
How often are you doing this, anyway? It must be pretty often, for it to be a bottleneck... do you really need to do it that often?
EDIT: For those who are saying "Use StringBuilder, it'll be faster" - consider this code:
public class Test
{
public static void main(String[] args)
{
int x = 10;
int y = 20;
int z = 30;
String foo = x + "," + y + "," + z + ";";
System.out.println(foo);
}
}
Compile that, then use javap -c to see what the compiler generates...
That should already be fast - it'll use StringBuilder internally for concatenation. Arguably using StringBuilder explicitly could eliminate the concatenation of empty strings, but it's not likely to make a big difference.
How often are you doing this, anyway? It must be pretty often, for it to be a bottleneck... do you really need to do it that often?
EDIT: For those who are saying "Use StringBuilder, it'll be faster" - consider this code:
public class Test
{
public static void main(String[] args)
{
int x = 10;
int y = 20;
int z = 30;
String foo = x + "," + y + "," + z + ";";
System.out.println(foo);
}
}
Compile that, then use javap -c to see what the compiler generates...
Use a StringBuilder.
String string = new StringBuilder("abcd").append(23).append(false).append("xyz").toString();
integer - How to concatenate int values in java? - Stack Overflow
concatenation - Java concat with integer and string - Stack Overflow
java - How to concatenate int and string - Stack Overflow
concatenating string and numbers Java - Stack Overflow
Videos
The easiest (but somewhat dirty) way:
String result = "" + a + b + c + d + e
Edit: I don't recommend this and agree with Jon's comment. Adding those extra empty strings is probably the best compromise between shortness and clarity.
This worked for me.
int i = 14;
int j = 26;
int k = Integer.valueOf(String.valueOf(i) + String.valueOf(j));
System.out.println(k);
It turned out as 1426
Edit: your question was changed after I posted my answer and it seems a more fitting answer has been posted, Instead of deleting my post i'm going to leave the last bit here in case you need to do any conversion from your joined array later in your project.
You also have the option to parse your data (this may be useful to you if you ever want to get the int's back from your array.
int tempInt = Integer.parseInt(tempString);
or alternatively:
String tempString = String.valueOf(tempArray[i]);
A good reference for changing types can be found at javadevnotes
you have two approachs :
1 - using arrayList:
ArrayList a = new ArrayList();
for(int i = 0 ; i < intArray.length ; i++)
a.add(intArray[i]);
for(int i = 0 ; i < strArray.length ; i++)
a.add(strArray[i]);
now you have answer in ArrayList
2 - use String.valueof() method :
String combinedStrings[] = new String[strArray.length+intArray.length];
int index= 0;
for(int i = 0 ; i < strArray.length ; i++)
combinedStrings[index++] = strArray[i];
for(int i = 0 ; i < intArray.length ; i++)
combinedStrings[index++] = String.valueOf(intArray[i]);
now you have answer in combinedStrings array
Its the BODMAS Rule
I am showing the Order of precedence below from Higher to Low:
B - Bracket
O - Power
DM - Division and Multiplication
AS - Addition and Substraction
This works from Left to Right if the Operators are of Same precedence
Now
System.out.println("printing: " + x + y);
"printing: " : Is a String"
"+" : Is the only overloaded operator in Java which will concatenate Number to String.
As we have 2 "+" operator here, and x+y falls after the "printing:" + as already taken place, Its considering x and y as Strings too.
So the output is 2010.
System.out.println("printing: " + x * y);
Here the
"*": Has higher precedence than +
So its x*y first then printing: +
So the output is 200
Do it like this if you want 200 as output in first case:
System.out.println("printing: "+ (x+y));
The Order of precedence of Bracket is higher to Addition.
Basic math tells you that adding numbers is done each at a time.
So "printing: " + x is computed first. As it s a string + int the result is "printing: 20". Then you add y so "printing: 20" + y equals "printing: 2010".
In the second case, multiplying is prioritary. So first x * y is calculated and equals 200. Then "printing: " + 200 equals "printing: 200".
The first will become:
StringBuilder sb = new StringBuilder (String.valueOf (someStr));
sb.append (3);
sb.append ("]");
String newStr = sb.toString ();
the second will become:
StringBuilder sb = new StringBuilder (String.valueOf (someStr));
sb.append ("3");
sb.append ("]");
String newStr = sb.toString ();
Here is disassembly:
public String foo (String someStr)
{
String newStr = someStr + 3 + "]";
return newStr;
}
public String bar (String someStr)
{
String newStr = someStr + "3" + "]";
return newStr;
}
public java.lang.String foo(java.lang.String);
Code:
0: new #16 // class java/lang/StringBuilder
3: dup
4: aload_1
5: invokestatic #18 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
8: invokespecial #24 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
11: iconst_3
12: invokevirtual #27 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
15: ldc #31 // String ]
17: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
20: invokevirtual #36 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
23: astore_2
24: aload_2
25: areturn
public java.lang.String bar(java.lang.String);
Code:
0: new #16 // class java/lang/StringBuilder
3: dup
4: aload_1
5: invokestatic #18 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
8: invokespecial #24 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
11: ldc #44 // String 3
13: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
16: ldc #31 // String ]
18: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: invokevirtual #36 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
24: astore_2
25: aload_2
26: areturn
There won't be any noticeable difference between both. Use what you find the most logical and readable. I would use
String newStr = someStr + "3]";
Hello there
Is there a way to do this without converting to String?
int x = 5; int y = 1; int z = x + y; // I want the result to be 51, not 6
I know I could just do
String z = "" + x + y;
But that seems like a messy workaround and it also needs to be converted back to an int if I ever want to work with it again.
But then and again, I could write a method for that but in all the years Java has existed someone else must have ran into this issue, right?