Article View: borland.public.jbuilder.java.language
Article #7270Re: can i change a int type Not Integer type argument in a function ?
From: Dave Bird
Date: Thu, 28 Jun 2001 23:05
Date: Thu, 28 Jun 2001 23:05
70 lines
3050 bytes
3050 bytes
In<3b3b7ae2_1@dnews>, dnoyeB <lamont_gilbert@rigidsoftware.com> writes:
>"wallance" <wallance@sh.ctil.com> wrote in message news:3b3b1371_2@dnews...
>> hi,
>> i want to change a int type argument in java,what should i do?
>> for example:
>>
>> public func1(){
>>
>> int i = 10;
>> func2(i);
>> System.out.println(i) ===> 11
>>
>> }
>> public func2(int arg){
>> arg = arg + 1;
>> }
>>
>> i know in java your can trans a object .then it can modify by reference.
>> but this is int type,not object.what should i do?
>No you can not alter objects within functions. AS in C and C++, passing a
>primitive OR object into a function always copies. If its a primitive, you
>get a copied primitive, if its an object, you get a copied reference.
>Changing the copy does not affect the original.
>That way you never have to worry about passing around your references and
>getting them back null. Again, the only context a value can be set to null
>is the context in which it was created.
Erm, yes, this is a very :-( common question.
In Java everything is effectively a 32/64 bit word.
If that is a number and you pass it into a method,
then a copy of the number arrives.
Otherwise that will be a "reference" [probably just the address but
what does it matter to you?] to a class or array object.
Pass it to a method, and what arrives is a FRESH COPY of the reference
to the SAME OLD object referred.
The equivalent of having a reference to a number is....
make it an array of one number, then it will be handled by reference.
class MyClass inherits OldClass{
public int method1(){
int theAnswer[] = new [0]int; // array of one ints
method2( theAnswer ); // numbered [0] to [0].
......
}
void method2( int refAnswer[] ){
refAnswer[0] = 99;
}
}
--
^-^-^-@@-^-;-^ http://www.xemu.demon.co.uk/
(..)__u news:alt.smoking.mooses
Technical Details
Message-ID:
<Kd3ASoEFh7O7Ew1B@xemu.demon.co.uk>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!dispose.news.demon.net!news.demon.co.uk!demon!xemu.demon.co.uk!daveReferences:
<3b3b1371_2@dnews> <3b3b7ae2_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,9b3cb30cd9a02399 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-06-28 18:12:09 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!dispose.news.demon.net!news.demon.co.uk!demon!xemu.demon.co.uk!dave From: Dave Bird <dave@xemu.demon.co.uk> Newsgroups: borland.public.jbuilder.java.language Subject: Re: can i change a int type Not Integer type argument in a function ? Date: Fri, 29 Jun 2001 00:05:41 +0100 Organization: none Message-ID: <Kd3ASoEFh7O7Ew1B@xemu.demon.co.uk> References: <3b3b1371_2@dnews> <3b3b7ae2_1@dnews> NNTP-Posting-Host: xemu.demon.co.uk X-NNTP-Posting-Host: xemu.demon.co.uk:158.152.196.209 X-Trace: news.demon.co.uk 993777092 nnrp-10:25307 NO-IDENT xemu.demon.co.uk:158.152.196.209 X-Complaints-To: abuse@demon.net MIME-Version: 1.0 X-Newsreader: Turnpike Integrated Version 5.00 U <dQumtnY$x4rJ2u5tL5fS$n2vuP> Lines: 70