Solved

Returning array of arrays / passing by reference

Posted on 2004-09-04
14
339 Views
Last Modified: 2010-03-31
I have a categorizeArray method which is passed an array and needs to return an array of arrays. How to declare that?

Also, a question that interests me, related to this:
Before I decided to return an array of arrays, I thought i'd pass references to nulls - to be filled by an array each. Is it in java possible to pass by reference?
0
Comment
Question by:Daij-Djan
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 65 total points
ID: 11981707
>>and needs to return an array of arrays. How to declare that?

Like this:

public X[][] foo() {
}

(where 'X' is some type)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 65 total points
ID: 11981725
Is it in java possible to pass by reference?

In a word, no. You could pass an array, and have that same array altered though, *through the same reference* but you can't do something like

public void foo(X[] xarray) {
  xarray = new X[10];
  // do something
}

as 'xarray' won't be replaced by the new creation
0
 
LVL 15

Expert Comment

by:JakobA
ID: 11982068
In java alaa objects are passed by reference

class TestObj {
     public int x;
} // endclass

in your code:
     void testFuction( TestObj t ) {        // function to change the value in an object.
           t.x = 42;
     }  //endmethod testFunction

     TestObj to = new TestObj();
      to.x = 15;
      System.out.println( "testobject x value is: " +to.x );   // writes 15
      testFunction( to );                  // give object as parameter (it is automatically a reference parameter)
      System.out.println( "testobject x value is: " +to.x );   // writes 42

note that String objects cannot have their value changed. the only vay to get a String to change value is to create a new String with the new value.

regards JakobA
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11982115
>>In java alaa objects are passed by reference

No - they are all passed by value. If your statement were correct, the function i posted would work. A reference to an object is passed by value, which is why i said:

>>You could pass an array, and have that same array altered though, *through the same reference*

but that's not the same as passing by reference
0
 
LVL 3

Assisted Solution

by:aviadbd
aviadbd earned 30 total points
ID: 11982352

I guess Jacob tried to say that objects' references are passed by value. :)

In any case, the rule in Java is that everything is passed by value. The reason you can change the internals of an object passed to a method is because in Java, all objects are considered as pointers. For that reason, the pointer is passed by value - And like in C++, a pointer passed by value cannot be changed. However, calls to methods, or changes to the object's members, affect the object.

The reason a String cannot be changed, for all I know, is because a call to the assignment operator of a String is equal to calling the new operator with the string given. It doesnt change the internal value of the String object, but tries to replace whats it pointing at.

Good luck!
Aviad.
0
 
LVL 2

Assisted Solution

by:vn77
vn77 earned 30 total points
ID: 11984277
Just a comment to aviadbd: String is an immutable class :).
Cheers,
vn77
0
 
LVL 92

Expert Comment

by:objects
ID: 11986484
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11995747
8-)
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 11996072
Comment to vn77:
< Slaps on my head > 
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 11996923
DUH! sorry aviadbd! Of course you should get part of the points - sorry - ...... look for thread with points for you.

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21121511.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11997014
If you want to reopen this question, post a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body:
Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21119302.html
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12000989

Oh I actually didnt even think I should receive the points... But <humbly> if you so insist.. :)
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 12004232
I do :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference between sorce folder and folder in eclipise 3 29
java imports not found 4 30
web project error add remove 1 32
learn programming 8 43
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question