Solved

Returning array of arrays / passing by reference

Posted on 2004-09-04
14
336 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility

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
Comment Utility
Just a comment to aviadbd: String is an immutable class :).
Cheers,
vn77
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0
 
LVL 3

Expert Comment

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

Author Comment

by:Daij-Djan
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
I do :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now