Sushil,

Your main issue is that you need a better understanding of the way Java
handles strings (specifically). The first point is that all String objects
are immutable. In other words, you can't change the value of a String
object once it's created (this is your main problem). StringBuffers are
used when you concatinate different strings (they are not immutable). Your
code is really translated to the following (roughly):

mport java.lang.*;

public class test
{
    public static void main(String args[])
    {
       char data[] = {'A','A','A','A','A','A'};
       String str1 = new String(data);

       method1(str1);
       char data2[]  = {'A','f','t','e','r',' ','r','e','t','u','r','n','i
','n','g',' ','f','r','o','m',' ','s','t','r','1',':'}
       System.out.println( new StringBuffer().append(new
String(data2).append(str1).toString() );
    }

    static void method1(String str1)
    {
       char data[] = {'s','t','r','1',':',' '};
       System.out.println( new StringBuffer().append(new String("str1:
")).append(str1).toString() );

       char data2[] = {'x','x','x','x','x','x'};
       str1 = new String(data);
    }

}  // End of test

The StringBuffer and array expansion is really for your edification. The
true problem you're having is the last line of method1:

str1 = new String(data);

Basically, this assigns the pointer "str1" to a new String object.
("xxxxxxx") _only inside method1_. However, your main() method is still
pointing to the old String object ("AAAAAAA"), because the pointer can't be
changed.

To do this properly with a String you could change method2() to return a
String. So, method1() would become:

    static String method1(String str1)
    {
       System.out.println("str1: " + str1);
       return "xxxxxx";
    }

And in your main() method, you would change the last two lines to:

       System.out.println("After returning from str1: " + method1(str1));

HOWEVER, if all you're worried about is passing the a file object
reference, this point may be moot (since that object is _not_ immutable,
and would work fine in your example below). Have you tested this with your
file object, or only Strings?


Kito D. Mann
[EMAIL PROTECTED]

P.S. You should read the API docs on Strings and StringBuffers -- they're
kinda important <grin>.






[EMAIL PROTECTED]
Wednesday December 15, 1999 02:01 PM

Please respond to [EMAIL PROTECTED]
To:   [EMAIL PROTECTED]
cc:    (bcc: Kito Mann/PSG/Prudential)
Subject:  Re: Parameters passed by reference




Hi Mann:

Thanks for your reply.

I am providing you the listing of a small program:

================= Start of program ======================
import java.lang.*;

public class test
{
    public static void main(String args[])
    {
       String str1 = "AAAAAA";

       method1(str1);
       System.out.println("After returning from str1: " + str1);
    }

    static void method1(String str1)
    {
       System.out.println("str1: " + str1);
       str1 = "xxxxxx";
    }

}  // End of test
===================== End of program ========================

In this java program, I am setting value of variable str1=AAAAAAA and then
I
am calling another method which is just simply printing the value of str1
and set again set a new value which is "xxxxxxx" but now if I print the
value of str1 its containing the old value only.

Actually my requirement is for servlet only.  I am using "AS/400 Record
level of access", in order to access the DB2.  In servlets I am having lots
of methods: method1 is for positioning of file pointer, method2 is for
reading the record and so on.
So in order to maintain the file pointer my requirement is to pass the
AS400
file object as reference.

Thanks,
Sushil
Email:   [EMAIL PROTECTED]   (Personal)
          [EMAIL PROTECTED]  (Official)

-----Original Message-----
From: Kito Mann [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 15, 1999 1:20 PM
To: [EMAIL PROTECTED]
Subject: Re: Parameters passed by reference


Sushil,

First, this question is way off topic.

All Objecs in Java are passed by reference. Objects include Strings and
Arrays, but not the primitive types: (ints, chars, longs, floats, etc.).

So, for instance, in the following excerpt:

public void test()
{
  String s = "This is a string";
  myMethod(s);
  myMethod(s.clone());
  myMethod2(5);
}

public  void myMethod(String s)
{
   System.out.println(s);
}

public void myMethod2(int i)
{
  System.out.println(i);
}

The first call to myMethod() is passing the variable "s" by reference; the
second call is passing a _copy_ by reference, which is the closest you can
get to passing an Object by value. The call to myMethod2() will _always_
pass the variable by value, since the parameter is a primitive type and not
an Object. You can't pass primitive types by reference, but you can use one
of the wrapper Objects (Integer, Float, etc.) and pass those by reference.

Kito D. Mann
[EMAIL PROTECTED]




[EMAIL PROTECTED]
Wednesday December 15, 1999 09:27 AM

Please respond to [EMAIL PROTECTED]
To:   [EMAIL PROTECTED]
cc:    (bcc: Kito Mann/PSG/Prudential)
Subject:  Parameters passed by reference




Hi,

I would like to know that how can we pass a variable by REFERENCE to a
method within the same class.

I have read that in order to pass by reference, we have to pass the object
itself.  But in my case I have to pass a variable say string to a method
inside the same class.

Thanks,

Sushil
Email:   [EMAIL PROTECTED]   (Personal)
            [EMAIL PROTECTED]  (Official)

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to