Results 1 to 7 of 7
  1. #1
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Post Problem in String object

    when an object is passed as an argument in a function,it is passed through object reference not value.right.!!
    but when i apply it to String object it doesn't work
    see below code:

    class string
    {
    static void change(String str)
    {
    str="java world";
    }
    public static void main(String s[])
    {
    String str1="java";
    System.out.println(str1);
    string.change(str1);
    System.out.println(str1);
    }
    }
    /*
    =========
    output:->
    =========
    java
    java
    */

    please any body can help me in this problem..!!!
    thnx in advance..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Java passes all parameters by value; for primitives their value is copied on the stack and for non-primitives ('real' objects) their reference value (a pointer) is copied and pushed on the stack. Your example will never work the way you intended itto work. Remember: pass by value, always.

    kind regards,

    Jos

  3. #3
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Post

    thnx jos.
    so may i say that every element whether it is primitive or non primitive, it is always pass through value only..!!

    if it is like that then what happen in the below case:

    class string
    {
    String s="java";
    }
    class n
    {
    static void change(string val)
    {
    val.s="java world";
    }

    public static void main(String str[])
    {
    string s1=new string();
    System.out.println(s1.s);
    n.change(s1);
    System.out.println(s1.s);
    }

    }

    /*
    =======
    output:->
    =======
    java
    java world
    */

    please jos can you tell me the difference between the execution of both the code, one written here and the privious one.

    thnx for your reply..

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Yuvarajsinh View Post
    please any body can help me in this problem..!!!

    when you pass an object reference to a method and the method modify the object then the changes are also seen in the calling method. but with strings the things behave a little different. when you modify a string, then the old reference is abandonned and a new object is created with a new reference. that's also what is ment when you hear "string objects are immutable". now, when you change the string in your method, you will have another string object and logical also another reference to it. this means the calling method and the method itself are referencing different object. do have the same reference to the changed object try this:

    Java Code:
    class StringDemo {
    	static String change(String str) {
    		str = "java world";
    		return str;
    	}
    
    	public static void main(String s[]) {
    		String str1 = "java";
    		System.out.println(str1);
    		str1 = change(str1);
    		System.out.println(str1);
    	}
    }
    this is ok, because after the call to change() the new reference given back from change() is assigned to str1.
    Last edited by j2me64; 04-12-2010 at 02:47 PM.

  5. #5
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    now i get it properly..
    thnx a lot.

  6. #6
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Smile

    now i get it properly..
    thnx a lot.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    The thing with String is that there is no method in String to change the data within it.

    This line here:
    str="java world";
    isn't changing the existing value stored in an existing String object (str)...it is pointing "str" to a new object, ie changing it's reference.

    Similarly this:
    Java Code:
    void changeMyObject(MyObject obj) {
        obj = new MyObject();
    }
    does not change the reference of the parameter passed in as obj.

Similar Threads

  1. String to an object?
    By bobbyboyy in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 08:43 PM
  2. Object name by string variable?
    By zerkz in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 08:16 AM
  3. Object to String.
    By Gelembjuk in forum New To Java
    Replies: 6
    Last Post: 10-29-2008, 11:19 PM
  4. Object from String
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:20 PM
  5. String vs Object
    By Gilgamesh in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 11:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •