Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Returning array of arrays / passing by reference

Posted on 2004-09-04
14
Medium Priority
?
344 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

636 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