This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList issues? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList issues?" Watch "ArrayList issues?" New topic
Author

ArrayList issues?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

java.util.ArrayList

"You can pass a typed array list to the update method without any casts.
ArrayList<Employee> staff = ...;
employeeDB.update(staff);
The staff object is simply passed to the update method.

Even though you get no error or warning from the compiler, this call is not
completely safe.

The update method might add elements into the array list that are not of type
Employee. When these elements are retrieved, an exception occurs. This sounds scary, but if
you think about it, the behavior is simply as it was before Java SE 5.0. The integrity of the
virtual machine is never jeopardized. In this situation, you do not lose security, but you also
do not benefit from the compile-time checks."

Core Java Fundamentals 8th Edition, P.210.

Has anyone encountered issues with ArrayLists?

I have been working on a toy program and passing ArrayLists throughout my program, for example this is the header of the getWinner() method, which should read the cards and find out the winning hands (although I still need pointers on how to go about them because my simple mathematical deductions are not good enough apparently




Unfortunately reference values cannot be passed in Java as far as I know, because it would save my workhorse some memory


Jon
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Without seeing the rest of the reference, I suspect the book is talking about the situation where you pass a typed collection into a method that takes an untyped collection.

Basically, you should almost never do this. All collection references should be typed, everywhere. And if you do this the problem never arises. The one exception is if you are forced to interact with existing code that doesn't use generics...in which case, yes, you need to be aware of the possibility that your type safety could be compromised.

Not quite sure what you mean by your last comment - reference values get passed in Java all the time. The references themselves are copied, but the thing it references isn't.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Matthew Brown wrote:Without seeing the rest of the reference, I suspect the book is talking about the situation where you pass a typed collection into a method that takes an untyped collection.

Basically, you should almost never do this. All collection references should be typed, everywhere. And if you do this the problem never arises. The one exception is if you are forced to interact with existing code that doesn't use generics...in which case, yes, you need to be aware of the possibility that your type safety could be compromised
.
Ok noted


Not quite sure what you mean by your last comment - reference values get passed in Java all the time. The references themselves are copied, but the thing it references isn't

"The Java Programming language always uses call by value. That means that the method gets a copy of all parameter values"
Core Java Volume I Fundamentals 8th Edition (pg.138)

Can you provide an example?


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

It passes references by value (when you're not talking about primitives). So in the following code:
At the end listA will contain "A" and "one", because both listA and the list reference in method1 referenced the same object.

listB will contain "B", because a copy of the reference was taken, so setting list to a new value in method2 has no effect on listB.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList issues?