-
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.
-
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?
-
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.
-
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
-
sorry, i didn't realise that the primitive type objects were immutable. I've never had need to use them before.
-
Thanks to everyone who helped. I was able to solve all my problem with this.
-
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>)
-
what are they for anyway? getting primitives into collections like vectors?
-
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..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|