The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java class literals

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Norman Richards

Posts: 396
Nickname: orb
Registered: Jun, 2003

Norman Richards is co-author of XDoclet in Action
Java class literals Posted: Jun 25, 2004 12:22 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Norman Richards.
Original Post: Java class literals
Feed Title: Orb [norman richards]
Feed URL: http://members.capmac.org/~orb/blog.cgi/tech/java?flav=rss
Feed Description: Monkey number 312,978,199
Latest Java Buzz Posts
Latest Java Buzz Posts by Norman Richards
Latest Posts From Orb [norman richards]

Advertisement

I've been doing Java for a long time and I feel pretty confident that I know just about everything there is to know about the language and JVM. Obviously that is an overstatement, but I do try to make sure I always know more than the guy next to me. But, I sure feel dumb today.

Class literals have been around since Java 1.1. Everyone knows you can use String.class> to get the Class object for java.lang.String. Under the covers the compiler caches a the results of Class.forName("java.lang.String") in your class, and all is well. When I've needed a Class object to represent a primitive type like int, I've always used Integer.TYPE. Somehow in the mix of things, I never realized you could also use int.class to get the Class. This compiles down to a reference to Integer.TYPE, so it's really just a question of readability. It's no big deal, but I'm shocked I never realized this was possible before.

But, my shame doesn't end there. What if you need a Class for an array type? You have to call Class.forName("[I") and curse the language designers, right? Well, that's what I thought until I found out I could have simply used int[].class instead. Again, there's no magic there. The same Class.forName() method is called. But, there's no doubt which one makes for better code.

So, I hang my head in shame that somehow a basic language construct that has been around forever has escaped me for so long. For penance, I'll have to read the Java language specification to see if I've overlooked any other ebarassingly simple language features.

Read: Java class literals

Topic: I'm Presenting at NFJS: Atlanta Previous Topic   Next Topic Topic: Java Regex APIs and Quoting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use