?
Solved

Returning array of arrays / passing by reference

Posted on 2004-09-04
14
Medium Priority
?
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 260 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 260 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 120 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 120 total points
ID: 11984277
Just a comment to aviadbd: String is an immutable class :).
Cheers,
vn77
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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