Pass by reference

Started by
6 comments, last by NuffSaid 22 years, 6 months ago
How do you pass a primitive by reference in Java? The C++ way doesn''t seem to work, and the language doesn''t have pointers. This has puzzled me alot. I don''t find any info on this topic at all, which has led me to conclude that it is a) very simple and I''m stupid for asking it. b) not possible. Any one?
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
b)

There are basically two approaches used for working around this:

1) Change the design so that pass by reference isn''t needed: This typically involves returning some class with multiple fields that make up the interesting data. This is usually the preferred method

2) Create a MutablePrimitive style object. This is typically considered to be bad style.
you gonna have to do something like this
  class MyInterger{  public int interger;}class MainClass{  void main(String [] args)  {    MyInterger myInt;    someFunnction(myInt);    // myInt.interger is now 500  }  void someFunction(MyInterger myInt)  {    // do something to the int    myInt.interger = 500;  }}  



"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
That sucks. I need a function that swaps the values of 2 integers(part of a quicksort, and no, I need the quicksort as I''m just experimenting on my own). Its going to be called many times, therefore, wrapping it in a class isn''t a very good option.

[rant]
What kinda language doesn''t support pass by reference of primitives? C++ does that. Pascal does that. Heck, even VB does that. I can see why Java doesn''t implement multiple inheritance & pointers, but what''s wrong with references(for primitives)?
[/rant]
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Hi.

The Java is a stupid langage that only works for small applets and for Servlets (for this it''s very good).

In Java, all objects passed to functions, are passed by reference. Thats means that if you need to swap the values of two integers, you need to make two objects of the class Integer, then the prototype of the function looks like :

public function swap(Integer a, Integer b)

the body of the function is homework for you, but is very easy and doesn''t have problems. After call the function, you can extract the values of the two objects into two primitives ints if you like.

That kind of conversion (primitive int to clas Integer) are a lot used in Java. In Java there are a lot of things that only can be done using this shit. This is one of the reason why Java will never be as fast as C/C++.





"If you''''re gonna die, die with your boots on"
"If you''re gonna die, die with your boots on"
There is always the single element array technique.
ie:
  int[] x = new int[1];int[] y = new int[1];public void swap(int []a, int [] b){  int temp;  temp = a[0];  a[0]=b[0];  b[0]=temp;} ...x[0]=20;y[0]=50;swap(x,y)    


Edited by - thed77 on October 22, 2001 6:07:29 AM
If you missed a great moment in history don't worry, it'll repeat itself.
Here''s how I managed to finally solve the problem of swapping integers. Here''s what I did

  //in some classpublic static void swap(int[] a, int i, int j) {  int temp = a[i];  a[i] = a[j];  a[j] = temp;}  


Not exactly what I had in mind, but its the most elegant solution to my problem.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Yeah, that is by far the best solution to needing a swap function...

Gelmir, I'll ignore most of the incorrect stuff you posted. However, there is one point I'd like to clear up:

quote:
In Java, all objects passed to functions, are passed by reference.


False. In java there are two basic data types: primitives and references. Both are passed by value in function calls. That's why code that looks like this:

      void swap(String s1, String s2){    String temp = s1;    s1 = s2;    s2 = temp;}    


just doesn't work. If objects were actually passed by reference, that would swap the two strings. Instead, references are passed by value, so the above code does nothing.

Edited by - c_wraith on October 24, 2001 2:45:58 PM

This topic is closed to new replies.

Advertisement