Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat an integer to a String - use String literal or primitive from performance and memory point of view?

Option 1:

String newStr = someStr + 3 + "]";

Option 2:

String newStr = someStr + "3" + "]";

Which option is better with regards to performance, memory and general practice? What are some recommended tools/ways I can use to measure memory usage of my code and its performance (besides measuring the start time and the end time and calculate the difference)

like image 687
user1739658 Avatar asked Feb 08 '13 23:02

user1739658


People also ask

Can we concatenate string and integer in Java?

To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.

What is the best way to concatenate strings in Java?

Using + Operator The + operator is one of the easiest ways to concatenate two strings in Java that is used by the vast majority of Java developers. We can also use it to concatenate the string with other data types such as an integer, long, etc.

Can you put an integer in a string?

You can use the String concatenation operator + to concatenate integers to a string in a clear and concise manner. Note that the compiler implicitly constructs an intermediate StringBuilder object, append the integers, and then call the toString() method.

What code is written by the compiler if you concatenate any string by string concatenation operator )?

Syntax: public String concat(String another)


3 Answers

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
like image 147
Mikhail Vladimirov Avatar answered Oct 30 '22 08:10

Mikhail Vladimirov


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]";
like image 31
JB Nizet Avatar answered Oct 30 '22 07:10

JB Nizet


I would recommend Jprofiler as a great java application profiling tool that helped me find lots of memory problems.

I don't think option 1 and 2 have a big difference in terms of memory usage, especially if it is for a desktop application.

like image 20
Simon Avatar answered Oct 30 '22 08:10

Simon