In: Computer Science
Write a method that concatenates two strings passed as
parameters.
Version #1 - assign to the 1st parameter the
concatenation of both parameters.
Perform the concatenation using the + operator.
We are assuming reference parameter work that way (they
don't).
Version #2 - fine. return the value of the 1st param
after concatenating
to it the second parameter then.
Version #3 - Let's do it the proper way now, I
say.
Create a temporary String reference variable in your method.
Assign to it the result of concatenating both parameters.
Return the temporary variable.
Please note, this exercise is only useful if you take
the time to trace or draw
a stack diagram to make sure you exactly understand
why each version works or
does not work (and how they do so). Office hours are a
good place to get help
if you are rusty with tracing or stack diagrams.
*/
public class TinkeringWithStringsLive {
public static void main(String[] args){
String s1 = "hello";
String s2 = "world";
version1(s1,s2);
System.out.println(s1);
System.out.println(version2(s1,s2));
System.out.println(version3(s1,s2));
} // end main
public static void version1(String s1, String
s2){
// please note that the return type is void
// we modify s1 instead of returning anything
// worth a try, right?
}// end method version1
public static String version2(String s1, String
s2){
return "";
}// end method version2
public static String version3(String s1, String
s2){
return "";
}// end method version3
} // end class
public class TinkeringWithStringsLive { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; version1(s1, s2); System.out.println(s1); System.out.println(version2(s1, s2)); System.out.println(version3(s1, s2)); } // end main public static void version1(String s1, String s2) { // Return is void, hence we are not returning anything s1 = s1 + s2; // When values are passed to the method, they are given as a copy of original value // Hence s1 and s2 are copies inside method. // If we change s1 in the method, it is not going to affect anything outside the method }// end method version1 public static String version2(String s1, String s2) { s1 = s1 + s2; // Here, we change the string s1 inside the method and then return the modified string. // the returned string is captured in main method and shown there, hence we see the change. return s1; }// end method version2 public static String version3(String s1, String s2) { String temp = s1 + s2; // We just created another string inside the method which is concatenated, and we are returning // that. main method captures and shows it. return temp; }// end method version3 } // end class
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.