• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Isn't Java type-safe?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A feeble attempt to tell you about our stuff that makes us money
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic