wood burning stoves*
The moose likes Java in General and the fly likes Isn't Java type-safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Isn Watch "Isn New topic
Author

Isn't Java type-safe?

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
What exactly is meaning of type-safety? Surprisingly I came across one site that said "Java is not type safe". Is it true
Link to that site http://www.cis.upenn.edu/~bcpierce/courses/629/papers/Saraswat-javabug.html


I am slow but sure
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

First, note that that information was written in 1997. That's very long ago, especially considering how fast things change in the world of computers and programming languages.

I didn't read the whole article in detail, but it looks like they have come up with some hack that involves class loaders, and that the JVM apparently doesn't check for, and from that they conclude that Java is not type-safe. It sounds like a theoretical possibility that doesn't really have a lot to do with how the Java programming language is used in practice by most people. Since this was written in the time of Java version 1.0, this might not even apply anymore to Java 5 or 6.

I wouldn't take that page as a serious flaw or bug in the JVM or Java programming language.

Java is type-safe because the compiler checks the types of variables and expressions in your code, and will give you an error if the types don't match. I wouldn't make it too complicated and worry about some paper from 1997 which is based on some special hack.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
Thanks Jesper
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It would be interesting to try running their example to see what happens now. The basic thesis of this article seems to be that the JVM will happily treat instances of two classes by the same name, loaded by different class loaders, as being instances of the same class. My understanding is that a Java type is identified by a (ClassLoader, Class) pair, and that two classes loaded by different ClassLoaders are not the same. The ClassLoader semantics were firmed up in Java 1.2, and I expect that this article's propositions were invalid shortly after publication. I'll give it a try when I have a chance.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

OK, I tried it, and yeah, they fixed this many years ago. In the article, you see core dumps and other oddities; now the programs simply fail with an exception like this:

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of DelegatingLoader) previously initiated loading for a different type with name "R"

I attached my cleaned-up version of the code in case anybody wants to play with it, as it was rather a mess by today's standards, and wouldn't quite compile as-is.




[Download typesafe.zip] Download

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40


Java's history with Bell Labs is an interesting one. Everyone will remember Microsoft as trying to weaken, destroy, or take Java. But very few people realize that Bell Labs was taking potshots from the sidelines as well -- most likely because they had a competing technology coming out.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Isn't Java type-safe?