Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output problem

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
Code:
public class TestBuffer { 

     public void myBuf( StringBuffer s, StringBuffer s1) { 
            s.append(" how are you") ; 
            s = s1; //[COLOR=blue] question 1 [/color]
     } 

     public static void main ( String args[] ) { 
             TestBuffer tb = new TestBuffer(); 
              StringBuffer s = new StringBuffer("Hello"); 
              StringBuffer s1 = new StringBuffer("doing"); 
              tb.myBuf(s, s1); 
              System.out.print(s); 
      } 
}

output of this code > Hello how are you

question 1 > why the output is not "doing" ?

look the assignmnet s = s1 . it means whatever s1 is pointing (in this case "doing" ), the same thing will be pointed to by s now.


now print(s)
so, the output must be " doing " .

bcoz assignment of refernces means they will refer to the same chunk of memory and in this case that will be "doing" due to s=s1

whats wrong with this code ? how the output is "Hello how are you " ? it should have been "doing".

can you tell on which logic it is showing that output ?

 
Because you have 2 variables with the name "s" and 2 variables with the name "s1".

The variables "s" and "s1" in your main method and in the myBuf method are NOT the same.

Maybe if you change your code to
Code:
     public void myBuf( StringBuffer myBufs, StringBuffer myBufs1) {
            myBufs.append(" how are you") ;
            myBufs = myBufs1; // question 1
     }
you'll see.
 
hi , i did that way, but the answer is same .

i did not understand where i was wrong.

i tried
Code:
public class TestBuffer {




           public void myBuf( StringBuffer myBufs, StringBuffer myBufs1)
           {
            myBufs.append(" how are you") ;
            myBufs = myBufs1; // question 1
           }



     public static void main ( String args[] ) {
             TestBuffer tb = new TestBuffer();
              StringBuffer s = new StringBuffer("Hello");
              StringBuffer s1 = new StringBuffer("doing");
              tb.myBuf(s, s1);
              System.out.print(s);
      }
}

output >
Hello how are you

it is same output as above.

i want to know where i was wrong in my justification.

can you tell what exactly happening here s=s1 ?



 
It's nothing wrong with s=s1.
I don't think you are interested in a working solution:
Code:
    return s1;
}
// ...
     System.out.print (tb.myBuf (s, s1));

Normally objects are passed as references to a method.
If the function modifies the object, the object is afterwards in a modified state.
There are some exceptions from this rule:
Primitiv Datatypes are passed by value, which means, a copy of the object is implicitly generated, and after the method finished, the original variable is unchanged.
Those primitive Datatypes are: int, long, double, float, char, byte, ...?
And String.
It's new to me, that Stringbuffer behaves the same, and it's a pity, that this fact isn't mentioned on Page 1 of the javadoc for Stringbuffer (and String).

You may work around in different ways:
Create a wrapper-object, which contains the String. This will be passed by reference.
Or work with an array of length 1.
Arrays are passed by reference too, even arrays of primitiv types as int, float, boolean...
Or return a value in your method.
This is the normal way, if your method isn't returning a value though. The stringmanipulating funcitons of java.lang.String behave exactly that way:
String foo = " foo ";
foo.trim (); // surprise: foo unchanged!
foo = foo.trim (); // that's the way it is...
 
The java language passes arguments only "by value", that is, the argument cannot be changed by the method called. When an object instance is passed as an argument to a method, the value of the argument is a reference to the object. The contents of the object can be changed in the called method, but the object reference is never changed."

PS : Strings are immutable
StringBuffer is mutable (and behave like any other class passed as a parameter)

Code:
public class TestBuffer {

    public void myBuf( String s, StringBuffer sb ) {
      System.out.println("**      String " + s);
      System.out.println("**StringBuffer " + sb);
      s = s + "AA";
      sb = sb.append("BBB");
      System.out.println("**      String " + s);
      System.out.println("**StringBuffer " + sb);
    }

    public static void main ( String args[] ) {
      TestBuffer tb = new TestBuffer();
      String s = new String("Hello");
      StringBuffer sb = new StringBuffer("doing");
      System.out.println("      String Before " + s);
      System.out.println("StringBuffer Before " + sb);
      System.out.println();
      tb.myBuf(s, sb);
      System.out.println();
      System.out.println("      String After " + s);
      System.out.println("StringBuffer After " + sb);      }
}
The output :
Code:
      String Before Hello
StringBuffer Before doing
**      String Hello
**StringBuffer doing
**      String HelloAA
**StringBuffer doingBBB
      String After Hello
StringBuffer After doingBBB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top