This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

What does type safety mean to a list?  RSS feed

 
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following lines of codes:


So now, I ask.  What is the checkedList( ) method checking?
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API

Assuming a list contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, ...


So it looks like that assumption is false in your case: you already have an unsafe List. But after you create a checkedList, all additions are guaranteed to match the Class parameter.
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir, if I have the following, then what is the need for the Collections.checkedList() method?
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API documentation:

The generics mechanism in the language provides compile-time (static) type checking, but it is possible to defeat this mechanism with unchecked casts. Usually this is not a problem, as the compiler issues warnings on all such unchecked operations. There are, however, times when static type checking alone is not sufficient. For example, suppose a collection is passed to a third-party library and it is imperative that the library code not corrupt the collection by inserting an element of the wrong type.

 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rancher
Posts: 3989
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The checkList is not about protecting the reference being reassigned.
It's about preventing someone adding incorrect things to the List.


If I pass a List<Integer> into the above method, without it being a checkedList, then 6 will be added to the List.
If I pass in a checkedList version then it will throw an exception.


Running the above code using the previous method gives:
[6]
Exception....ClassCastException...etc etc
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sir.
The Integer.class and String.class.  Where does the class field come from? The documentation does not reveal this.
 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are asking.
The documentation for checkedList shows the second parameter is the class for the entries in the List.
 
Saloon Keeper
Posts: 9997
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a field. It's a class literal. You can compare it to the true and false boolean literals.
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How does a beginner even know this?  The class literal is not mentioned in the Java documentation. Not sure I have seen it in a book. Thank God for internet.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.8.2
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:. . . // Has the unchecked list automatically become a checked list? . . .

No, but adding anything to a raw type is inherently unsafe, so the compiler gives you a warning.
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the compiler issues a warning but goes ahead anyway.  At execution of the line of code, Java fires Exception java.lang.UnsupportedOperationException.  I thought the operation was insertion operation. The instance of ArrayList surely has the add() method. Why is the operation unsupported now? I would have naively expected the incompatible type kind of error.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That error may have more to do with how Arrays.asList works more than anything else. Check the JavaDocs for Arrays.asList(), it says it's a fixed-size list backed by the specified array.
 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the first line to:
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

An unchecked list references a checked list. Now, I am able to insert any item belonging to any type into the underlying list.  Is it the case that the instance is not actually checked, but only the "roads" leading to the instance may have check points? Forgive me for using the words "roads" and "check points".
 
Knute Snortum
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it will help to remember that an object's behavior comes from its type, not the object it references.  So an object of an unchecked type will behave as an unchecked object, even if it references a checked object.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll take a shot at clarifying what Knute said.  You have to understand the distinction between an object and a reference to an object. In your examples, myList is a reference with a type of List<Integer>. The actual object is an ArrayList<Integer>.

The Java compiler will do its type checking based on the declared reference type. So, when you use the myList reference, you'll get any protections that Java knows to apply based on the fact that it knows its dealing with a List<Integer>, not the fact that the actual object is an ArrayList. However, when you use the anotherList reference, which is declared simply as a raw List type, Java will do only what it can reasonably figure out based on that raw type. Looking at the actual runtime type of the object being referenced is the job of the JVM. However, because of Java generics type erasure, the runtime does not even know about generic types, so if you want to have the same kind of type-checking enforcement the compiler gives when you're running your program, you'll need that Collections.checkedList() wrapper.
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before now, I thought a type and a class were interchangeable. Example, instance of type String and instance of the String class means the same thing.

So an object of an unchecked type will behave as an unchecked object ...


Now,

Is ArrayList<Integer> in its own class?

When an object reference is assigned to another object reference as follows:
List<Long> list3 = new ArrayList<Long>();
List<Long> list4 = new ArrayList<Long>():
list3 = list4;
I am thinking that list3 will severe ties to its original object and connects to the same object that list4 connected to.  Now, list3 and list4 reference the same object.  To make it easy for me to comprehend, I see it as two roads leading to the same destination.
Please, clarify this for me.
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:Before now, I thought a type and a class were interchangeable. Example, instance of type String and instance of the String class means the same thing. . . .

But a String doesn't have a type. It has several. It is a String, an Object, a Serializable, a Comparable<String>, and a CharSequence. You can see all those types in its documentation.
 
Biniman Idugboe
Ranch Hand
Posts: 195
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Looking at the actual runtime type of the object being referenced is the job of the JVM. However, because of Java generics type erasure, the runtime does not even know about generic types, so if you want to have the same kind of type-checking enforcement the compiler gives when you're running your program, you'll need that Collections.checkedList() wrapper.


That makes it a lot easier. I was struggling to understand what the Collections.ckeckedList() method was actually doing.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!