By reference


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: By reference

  1. #1
    Join Date
    Jan 2004
    Posts
    22

    By reference

    How can I pass a variable into a function by reference. This is frustratingly simple, but I am somehow unable to find something on it using Google. I'm assuming that's because I'm using the wrong vobabulary, so I'll explain what I mean. I want a variable passed into a function, when modified there, to have its incarnation in the place that called it be modified as well.

    Thanks.

  2. #2
    Join Date
    Feb 2004
    Posts
    11
    Hi. Primitive types are always passed by value while instances are passed by reference in Java. What are you trying to pass in your method, a primitive type or an instance?

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    all primitive types have an associated class that you can use to make instances of them eg int has the class Integer. If you pass the Integer object instead of the primitive int it will pass by reference instead of by value.

  4. #4
    Join Date
    Feb 2004
    Posts
    808

    Re: By reference

    Originally posted by Oinkerwinkle
    I want a variable passed into a function, when modified there, to have its incarnation in the place that called it be modified as well.

    Thanks.
    this can only be done with objects. the following are primitives:

    int char short byte long double float boolean

    everything else is an Object, even Strings...

    Note that ALL of the Objects that represent primitives are IMMUTABLE. To go further than (and counteract) what mikeBarr81 said:

    Yes it is true that if you pass an Object like a String, or Integer to a method (not function - thats C language, this is java) it is passed by reference:

    Code:
      String s = "hello world";
      myClass.myMethod( s ); //yes, it is passed by reference
    BUT, and its a big butt..

    ANOTHER reference is created and attached to the String s:

    Code:
    public class myClass{
      public void myMethod(String s_temp){
    ...
    but because all primitive-modelling objects are IMMUTABLE, you CANNOT modify that and change the original parameter:

    Code:
    public class myClass{
      public void myMethod(String s_temp){
        s_temp = s_temp + " goodbye world";
      }
    }
    because they are immutable, any change necessitates making a new variable, so s_temp is dosconnected from s, and pointed to a new string, that the value of s, plus " goodbye world" generates

    -
    it is then thrown away when the end of the method is reached, if it is not returned

    ______

    so bear it in mind, even if you pass by reference, you cannot alter the contents of a class unless it exposes public member variables, or public setXXX method()s - for everything else, a new object will be created.
    And, like noted before the following Objects that represent primitives;

    Character, Boolean, Byte, Short, Integer, Long, Double , Float, String

    -> are all immutable

    -

    your choice of language, and expected method of coding indicate to ,me that youre a procedural programmers, maybe C?
    well, just a quick FYI, in java it isnt like it is in C.. we return values that we want to keep, and if there are many different types of value we want to keep, we make a class just for those, and return that. a similar idea would be C structs
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    sorry, i didn't realise that the primitive type objects were immutable. I've never had need to use them before.

  6. #6
    Join Date
    Jan 2004
    Posts
    22
    Thanks to everyone who helped. I was able to solve all my problem with this.

  7. #7
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by mikeBarr81
    sorry, i didn't realise that the primitive type objects were immutable. I've never had need to use them before.
    lol, s'ok.. myself neither.. apart from Integer.parseInt() i never really used them (cause they're <censored>)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  8. #8
    Join Date
    Feb 2004
    Posts
    541
    what are they for anyway? getting primitives into collections like vectors?

  9. #9
    Join Date
    Feb 2004
    Posts
    808
    pretty much..

    they provide utility methods to do with numerical representation and allow for numerical storage in object containers. they also follow string's lead in being immutable, therefore maintaining consistency..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center