DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. A Java Language Quirk

A Java Language Quirk

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Nov. 09, 10 · Interview
Like (0)
Save
Tweet
Share
8.29K Views

Join the DZone community and get the full member experience.

Join For Free

The boys at Sun made really an amazing thing when designing Java. I mean, everyone is cursing it now, predicting Java is dead because of its verbosity and such but could those persons have done better?  I doubt it. Just consider Java ‘s penetration in the enterprise… Verbosity enforces readibility, a nice feature in the enterprise where people move from position to position, in and out.

However, I have stumbled upon a troubling quirk. Most Java’s nuts and bolts are enforced at compile time, and erased at runtime. If nothing wrong gets compiled, there’s no reason to check at runtime. The glitch I found compiles fine but throws an exception at runtime.

Consider the following piece of code:

Object[] objects= new String[1];

objects[0] = new Integer(5);

It compiles juts fine but if you execute it, you will be faced with an strange java.lang.ArrayStoreException… Which statement is wrong? Obviously, the first one. How can an array of strings can be considered an array of objects?

Now, I’m not too fond of generics and the convoluted way they are implemented in Java 5 but this particular quirk is not possible with them. None of the two following snippets will compile:

// Type mismatch: cannot convert from ArrayList<String> to List<Object>
List<Object> list = new ArrayList<String>();
List<? extends Object> list = new ArrayList<String>();

// The method add(capture#1-of ? extends Object) in the type List<capture#1-of ? extends Object> is not applicable for the arguments (Integer)
list.add(new Integer(3));

For those interested in theory, this behaviour where an array of String can be seen as an array of Object is called covariance. Funny to say it, but Java 5 generics are actually an improvement over array whereas covariance is concerned!

Note that Scala makes arrays nonvariant at compile-time and thus correctly handles the problem:

// type mismatch;  found   : Array[String]  required: Array[java.lang.Object]
var array:Array[Object] = new Array[String](0)

From http://blog.frankel.ch/a-java-language-quirk

Java (programming language) Java language

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • Multi-Cloud Integration
  • Create a REST API in C# Using ChatGPT
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: