Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Isn't Java type-safe?

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Filename: typesafe.zip
Description: Cleaned up code
File size: 4 Kbytes
[Download typesafe.zip] Download Attachment
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic