Re: Call by reference in Java?

From: Dale King (kingd[at]tmicha[dot]net)
Date: 02/27/04


Date: Fri, 27 Feb 2004 13:48:04 -0500


"Ekim" <the.newsletter@gmx.net> wrote in message
news:c1ip91$1k981t$1@ID-222835.news.uni-berlin.de...
>
> I've a simple, maybe stupid question.
> How does a "pointer" look like in Java? I've just tried a "*" like in C,
but
> it didn't work.
> Now I'm not sure in which way I can solve my problem.
>
> The task is, that I want to call a procedure, and the parameters shall be
> call by reference.
> For example, this is my procedure call:
> checkParameter(kosten_pro_liter, liter, kosten, gesamt_km);
>
> The definition of my procedure looks like this:
> public void checkParameter(String strPreisProLiter, String
strAnzahlLiter,
> String strPreis, String strGesamtkilometer) {.....}
>
> What can I do so that after the procedure was executed, the parameters
have
> the value they have been given within the procedure?

As others have pointed out it is a good thing that you cannot pass by
reference in Java. The desire to pass by reference is usually a good
indicator that there is a flaw in your design. Usually it is caused by not
thinking in an object-oriented manner and thinking in a procedural mindset.
The fact that you used the word procedure 4 times is also a pretty good
indication of that ;-)

The solution is not to try to figure out how to emulate the behavior of C,
but to fix the design. Fixing the design implies coming up with the best
abstraction to fit the problem at hand. Without more knowledge of what
checkParameter is supposed to be doing I can't tell you what the best
abstraction is. I'm guessing the real issue may be that you are trying to do
to much in one method call and it should be separate method calls, but the
non-English names don't tel me what the method is supposed to do.

--
  Dale King


Relevant Pages

  • Re: Complex Specified Information - Pitman Formula
    ... Therefore a significant match between a reference and a test ... string is good evidence of non-random production. ... and there are no finite algorithms to compute their digits. ... probabilities of the different symbols the information source can produce. ...
    (talk.origins)
  • Re: String Reference Type
    ... All unary and binary operators have predefined implementations that are ... Therefore its always allocated in the heap and a variable of string ... As with all classes in this case y and x both reference the same String ... language depandant matter as below. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Abstract class variables question
    ... But as I think you've seen elsewhere in this thread, a value type can exist inside a class and in that case the value type is stored in the heap with the rest of the class instance. ... But as far as the "faster" goes, yes...to some extent value types have less overhead than reference types, and so can perform better in certain cases. ... Well, that would be true for a string object too, if there was any way to actually change a string. ... Seriously though, it is practically always the case that when you are writing an assignment to a reference, you're replacing the reference held by the variable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: completed my Varaint class.
    ... > virtual void setData ... note that your class can *by design* accept only ... to use a heterogeneous array containing floats or chars or complexes ... way to refer to 'string' even after the user had explicitly declared ...
    (alt.comp.lang.learn.c-cpp)
  • Sonar Design
    ... This is about the allocation of program resources, and distributed design ... This section is from chapter 2 of the reference book in Advanced ... PowerBuilder 4 Techniques, pp. 6 which describes high-level concepts used to ... when designing an architecture for a application. ...
    (comp.lang.misc)