On Sep 5, 1:25 pm, "Krupinski Norbert"
<[EMAIL PROTECTED]> wrote:
> Verson with StringBuffer doesn't work either:
>
> public class smiec {
>
> static StringBuffer a =new StringBuffer ("aaa");
> static StringBuffer b = new StringBuffer ("bbb");
>
> static void swap(StringBuffer x, StringBuffer y){
> String temp = x.toString();
> x =new StringBuffer ("jj");
> y = null;
>
> }
>
> public static void main(String args[]) {
>
> System.out.println("Before swap "
> + "a = " + a
> + " b = " + b);
> swap(a, b);
> System.out.println("After swap "
> + "a = " + a
> + " b = " + b);
>
> }
> }
This one works:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String firstString ="aaa";
String secondString = "bbb";
StringBuffer firstStringBuffer = new StringBuffer();
StringBuffer secondStringBuffer = new StringBuffer();
firstStringBuffer.append(firstString);
secondStringBuffer.append(secondString);
System.out.println("Before swap - firstString = " +
firstString + " secondString = " + secondString );
swap(firstStringBuffer, secondStringBuffer);
firstString = firstStringBuffer.toString();
secondString = secondStringBuffer.toString();
System.out.println("After swap - firstString = " + firstString
+ " secondString = " + secondString );
}
static void swap(StringBuffer firstStringBuffer, StringBuffer
secondStringBuffer) {
StringBuffer temporaryStringBuffer = new StringBuffer();
temporaryStringBuffer.append(firstStringBuffer);
firstStringBuffer.replace(0, secondStringBuffer.length(),
secondStringBuffer.toString());
secondStringBuffer.replace(0, temporaryStringBuffer.length(),
temporaryStringBuffer.toString());
}
}
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---