Solved

Returning array of arrays / passing by reference

Posted on 2004-09-04
14
340 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 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
Independent Software Vendors: 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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 112
Android development question 2 108
How to fix  socket closed error 11 63
Java ArrayList and if statement 2 55
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

737 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