This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array and ArrayList 1) Thread safety 2) Type Safety

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Thread Safety
Arrays and ArrayLists are not thread safe:
An ArrayList, just like an array is not thread safe.

If you have multiple threads trying to add and remove elements from an ArrayList,
you have to write additional code to ensure thread safety.

2) ArrayLists are type safe:
Since ArrayList is a generics enabled class, it helps you write type safe code.
For example, if you have:


will not compile because the compiler knows that list1 can only contain Strings.

Arrays are also type safe.
For example, if you have:


But you can do


This will compile fine but will fail at runtime.
This is a hole in the type safety provided by arrays.

Would someone please verify the following:

1. Arrays and ArrayLists are not thread safe.
2. Arrays and Arraylists are type safe with some exceptions based on the examples above.

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to have the idea... If I am not mistaken the example on ArrayLists and Arrays and type safety is from some tutorial somewhere. The exception to the rule you demonstrated, or the tutorial demonstrated, was interesting and confusing where an Object array reference variable actual points to a String array object and then the programmer tries to change the first element to hold an Integer. This seems like it would work since an Object array can hold anything since everything in the Java API IS-A or extends from java.lang.Object. I guess it blows up with a runtime exception like java.lang.ArrayStoreException because of the fact that the reference points to the String array.

I hope this helps. I have not yet passed the OCAJP7 so I hope the moderator chimes in with something more substantial.

Also, threads are not on the OCAJP7 exam... I would not worry too much about threads and synchronized blocks or methods with this first Java programmer exam.

Regards,

Ted
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted North wrote:I hope this helps. I have not yet passed the OCAJP7 so I hope the moderator chimes in with something more substantial.

Uh-oh, that's me Let's give it a try.

Let's start with thread safety. It's true. Both array and ArrayList are not thread-safe. Actually that statement is true for every mutable object. (I will even go a bit further. Even if you have a thread-safe class, like StringBuffer or Vector, you still have to be very careful when more than 1 thread will be accessing. But as you can notice this comment is placed into brackets, because it's not on this certification. You'll learn everything about threads, synchronized methods and thread safety in the OCPJP7 certification and you'll encounter many and experiences then )

Now for the type safety. The example of the array is correct. For an ArrayList it's a bit more complex. But for the OCAJP7 you just need to know that when you have an ArrayList<String>, you can't add integers to this collection. So it allows to write type safe code. (But as you maybe know these generics were introduced in Java 5. So in Java 1.4 (and prior versions) you had a simple ArrayList (without generic parameter). You could add everything (except for primitives, autoboxing was also added Java 5) to this collection (Integer, String, Cat, Dog, Plane,...) and you would not get any runtime exception on adding, but you have to be very careful when retrieving these elements using instanceof operator to decide the type of the object, because an Integer is not a Dog, nor a Plane Java is known for its backwards compatibility, so in Java 7 you can - or must - combine generics lists with non-generic lists. This combination could also result in a hole in the type safety. This remark is also in between brackets, so the same applies as above: it will be covered in the OCPJP7 certification, don't worry about it now)

Hope it helps!
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

Thank-you for chiming in with lots of useful Java information.

Regards,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic