Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-04-2010
Member
 
Join Date: Mar 2010
Posts: 162
Passing by reference in Java

Hello,
For basic types as int, char. Passing by value. For objects, passing by value of the reference (by abuse of language, passed by reference).
Here is the code
Code:
str = mstr "Hello";
exchange(mstr);
System.out.println(mstr);
 
void exchange(str unstr)
{
   unstr = Goodbye;
}
So, if any one can explain me then it would be great. Thanks in advance.
Reply With Quote
  #2  
Old 06-04-2010
Member
 
Join Date: Nov 2009
Posts: 347
Re: Passing by reference in Java

Hello,
See, this code
Code:
void exchange(String unstr)
Has the appeal of the method changes (mstr), the reference of the object is referenced by mstr fill in the local variable to the method that is the parameter unstr
See here
HTML Code:
unstr = Goodbye;
You replaced the reference in unstr by reference to the string constant "goodbye". The variable mstr is in no way affects the reference and storage on the same subject before.
Reply With Quote
  #3  
Old 06-04-2010
Member
 
Join Date: Nov 2009
Posts: 343
Re: Passing by reference in Java

Hello,
In your method changes when you do
Code:
unstr = "goodbye"
you change just the value of the parameter "unstr", which is a copy of mstr. So obviously, you do not change mstr
Code:
void exchange(unclassmutbl subject) {
    object.setQuelqueChose(newValeur());
}
Here for cons, you change the contents of the object passed as parameter (but there is no setter on the String class (because String is immutable), you can not change its value).
Reply With Quote
  #4  
Old 06-04-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Passing by reference in Java

Hello,
Passing by value means "passing by copying the value." So when you pass an object you pass a copy of the reference, and if that reference is changed that does not impact the reference of the calling code (the new reference exists in the method body).
By cons, if the object is changed, so there it works:
Code:
void exchange(StringBuffer strbuf)
	{
		strbuf.delete(0Sb.length()); / / We erase all
		strbuf.append(Goodbye);
	}
And see this code
Code:
		StringBuffer mstr = New StringBuffer("Hello");
exchange(mstr);
System.out.println(mstr);
Reply With Quote
  #5  
Old 06-04-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Passing by reference in Java

Hello,
Otherwise if you have not been changed, you can always do something like that
Code:
str = mstr "Hello";
Object[] t = New Object[] {mstr};
exchange(t);
mstr = t[0];
System.out.println(mstr);
 
void exchange(Object[] t)
{
   t[0] = Goodbye;
}
This is somewhat hidden misery but sometimes there is no alternative
Reply With Quote
  #6  
Old 06-04-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Passing by reference in Java

Hello,
Or they choose a gender approach to CORBA:
Code:
class GenericHolder <T>
{
  public T val;

  public GenericHolder(T aval)
  {
    val = aval;
  }
}
But in general it is preferable to avoid this kind of transition parameters. It's not for nothing that AC was not included in the Java language.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Passing by reference in Java"
Thread Thread Starter Forum Replies Last Post
Passing optional parameters in java Miles Runner Software Development 5 05-03-2010 10:20 AM
Message Passing in Java rashmi_ay Software Development 5 25-02-2010 10:33 PM
Java & Oracle: Passing Date garfield1 Software Development 5 04-01-2010 12:30 PM
Best reference book for java Sheenas Software Development 5 19-12-2009 08:15 AM
Problem Passing Data Dynamically in Java Kushan Software Development 3 26-03-2009 02:26 PM


All times are GMT +5.5. The time now is 09:06 AM.