Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing String as method parameter  RSS feed

 
sat kat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Pl look at below two code snippets:
1.
aMethod(){
String str = "hello";
bMethod(str);
}
2.
aMethod(){
bMethod("hello");
}

Which is the more effecient way from above two approaches from performance point of view?
Is it going to create a string object in second case?
-Satya
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be absolutely no difference. They should generate exactly the same bytecode. Neither one will create a new string. String objects are never copied in Java just by assigning them to a variable or passing them as an argument to something.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!