|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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; } |
#2
| |||
| |||
Re: Passing by reference in Java Hello, See, this code Code: void exchange(String unstr) See here HTML Code: unstr = Goodbye; |
#3
| |||
| |||
Re: Passing by reference in Java Hello, In your method changes when you do Code: unstr = "goodbye" Code: void exchange(unclassmutbl subject) { object.setQuelqueChose(newValeur()); } |
#4
| |||
| |||
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); } Code: StringBuffer mstr = New StringBuffer("Hello"); exchange(mstr); System.out.println(mstr); |
#5
| |||
| |||
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; } |
#6
| |||
| |||
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; } } |
![]() |
|
Tags: java, objects, passing values, programming language, reference |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |