What's New in Java 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

7/15/13

What's New in Java 1.5?

[ Home ]

What's New in Java 1.5?


Contents 1. The Changes
1. 2. 3. 4. 5. 6. 7. Generics For/in loop Autoboxing/Unboxing Typesafe Enums Varargs Static Import Annotations (Metadata)

2. How it will affect you NOW


1. To add to your lesson plan...

3. How it will affect you eventually...


1. How can the new features help?

References

Summary:
Sun has recently "upgraded" the Java language to include many new features such as Generics, Type-Safe Enumerations, Automatic Boxing and Unboxing, Annotations, For/In loops, and Static Imports. How will this affect the way you teach APCS? Immediately, it will have a minimal effect, but as you need to expand the scope of your APCS course to encompass the new features it might be difficult to find time. I'm going to discuss the current effect 1.5 has on APCS as well as possible future implications it may have on your course.

1. The Changes
I'm going to start by describing the major changes in this new, more robust language. Many of the effects will become more clear as each new component is described. The Big List 1. 2. 3. 4. 5. 6. 7. Generics For/in loop Autoboxing/Unboxing Typesafe Enums Varargs Static Import Annotations (Metadata)

1.1 Generics
www.cs.indiana.edu/classes/jett/sstamm/ 1/9

7/15/13

What's New in Java 1.5?

Generics provide a way to create and use typesafe data structures. This means that no longer do you have to create a L i s tof the basic O b j e c t sthen typecast every time you pull stuff out! You can declare your list to automatically typecast the stuff inside:
L i s tt h i n g s=c r e a t e L i s t O f B o r k O b j e c t s ( ) ; f o r ( I t e r a t o ri=t h i n g s . i t e r a t o r ( );i . h a s N e x t ( );){ B o r ki t e m=( B o r k ) i . n e x t ( ) ; / / d os o m e t h i n gu s e f u l }

Simply becomes...
L i s t < B o r k >t h i n g s=c r e a t e L i s t O f B o r k O b j e c t s ( ) ; f o r ( I t e r a t o r < S t r i n g >i=t h i n g s . i t e r a t o r ( );i . h a s N e x t ( );){ B o r ki t e m=i . n e x t ( ) ; / / d os o m e t h i n gu s e f u l }

The Java compiler also protects t h i n g sfrom having non- B o r kobjects inside. If you try to put a S t r i n gor anything else in, you'll get an error. This essentially means that you can create a list with specific types now instead of just objects. These exist in other classes such as M a pwhich uses two:
M a p < S t r i n g ,B o r k >m y M a p=n e wH a s h M a p < S t r i n g ,B o r k > ( ) ;

That creates a map that uses a S t r i n gkey and a B o r kvalue. This implementation kind of looks like something using templates in C++... Be careful though! You can create a string iterator that iterates over a list of non-strings that will only become an error at runtime. The compiler doesn't catch this. Also, sometimes when using a type that can be parameterized, but not specifying the parameter type (in angle brackets) you can get lint warnings. This just means that you haven't provided a specific type, and the current definition of the type is "fuzzy" -- like lint, get it? 1.2 For/in loop This is probably one of the coolest new features. I personally hate using iterators--it seems redundant and annoying sometimes. Well, lucky for me there's a way around them now! So now you can do this:
f o r ( I t e r a t o rl i n e u p=l i s t . i t e r a t o r ( );l i n e u p . h a s N e x t ( );){ O b j e c tt h a t T h i n g=l i n e u p . n e x t ( ) ; m y M o n s t e r . e a t ( t h a t T h i n g ) ; }

In a shortened:
f o r ( O b j e c tt h a t T h i n g:l i s t ){ m y M o n s t e r . e a t ( t h a t T h i n g ) ; }
www.cs.indiana.edu/classes/jett/sstamm/ 2/9

7/15/13

What's New in Java 1.5?

Much prettier, no? This works with arrays too.


i n t [ ]n u m s={1 ,2 ,3 ,4 ,5 ,6} ; f o r ( i n tn:n u m s ){ S y s t e m . o u t . p r i n t l n ( n ) ; }

Say you want to get your class to work with this nifty loop. Then, you have to implement I t e r a b l e(or extend something that does). This involves telling I t e r a b l ewhat type of things you iterate over. You can define a custom iterator to do something more robust, but for this illustration, I'm just going to grab one out of the list.
p u b l i cc l a s sM a i l B o xi m p l e m e n t sI t e r a b l e < M a i l M e s s a g e >{ / * *s t r u c t u r es t o r i n gt h em e s s a g e s* / p r i v a t eA r r a y L i s t < M a i l M e s s a g e >m e s s a g e s ; / / . . . / * * *I m p l e m e n t e df o rI t e r a b l e . * / p u b l i cI t e r a t o r < M a i l M e s s a g e > ( ){ r e t u r nm e s s a g e s . i t e r a t o r ( ) ; } / / . . . }

For more detailed information, see Java 1.5 Tiger: A Developer's Notebook[4] or the information on Sun's J2SE 5.0 language documentation. 1.3 Autoboxing/Unboxing
I n t e g e ri=n e wI n t e g e r ( 4 ) ; i n tj=i . i n t V a l u e ( ) ; N u m b e rn=n e wF l o a t ( 3 . 1 4 1 5 9 ) ; B o o l e a ns t u f f=n e wB o o l e a n ( f a l s e ) ; / /s t u f fb e f o r e?m u s tb eab o o l e a n( l o w e rc a s e ) S y s t e m . o u t . p r i n t l n (s t u f f . b o o l e a n V a l u e ( )?" Y e p ":" N o p e ") ;

Sick of this? Me too. Do this instead:


I n t e g e ri=4 ; i n tj=i ; N u m b e rn=3 . 1 4 1 5 9 f ; B o o l e a ns t u f f=f a l s e ; S y s t e m . o u t . p r i n t l n (s t u f f?" Y e p ":" N o p e ") ;

This is pretty nice. Especially since you can use + +and other similar operators with the wrapper types now too. 1.4 Typesafe Enums
www.cs.indiana.edu/classes/jett/sstamm/ 3/9

7/15/13

What's New in Java 1.5?

Enums are just magic classes to help prevent the methodless-interface antipattern. They let you make classes that will enumerate values, but also keep the types specific. Before, we could simulate enums with a bunch of s t a t i c f i n a li n tvariables or something. The problem with those is that you could confuse any int with one of the constants. With enumerations, only the values in the e n u mare valid. For example:
p u b l i ce n u mJ e t t S t a f f{ A D R I A N , A R I J I T , B E T H , E R I C , K A T I E , K A T Y , R A J A , R I C H , S U Z A N N E } ; J e t t S t a f fx=J e t t S t a f f . S U Z A N N E ;

Now, it gets even cooler. I don't have to keep track of separate information to store, say the peoples' full names. I can associate them directly, just like in a class! Each of the values of J e t t S t a f fare instances of the J e t t S t a f fenumeration, so we can define a constructor and a t o S t r i n g ( )method.
p u b l i ce n u mJ e t t S t a f f{ A D R I A N ( " A d r i a nG e r m a n " ) , A R I J I T ( " A r i j i tS e n g u p t a " ) , B E T H ( " B e t hP l a l e " ) , E R I C ( " E r i cW e r n e r t " ) , K A T I E ( " K a t i eA .S i e k " ) , K A T Y ( " K a t yB o r n e r " ) , R A J A ( " R a j aS o o r i a m u r t h i " ) , R I C H ( " R i c hK i c k " ) , S U Z A N N E ( " S u z a n n eM e n z e l " ) ; p r i v a t eS t r i n gn a m e ; p u b l i cJ e t t S t a f f ( S t r i n gn ){t h i s . n a m e=n ;} p u b l i cS t r i n gt o S t r i n g ( ){r e t u r nt h i s . n a m e ;} } J e t t S t a f fx=J e t t S t a f f . S U Z A N N E ; S y s t e m . o u t . p r i n t l n ( x ) ;

But wait, it gets cooler! Now you can also give each enumerated value a custom body. Since they're each instances, you could design a toString() method for each:
p u b l i ce n u mJ e t t S t a f f{ A D R I A N ( " A d r i a nG e r m a n " ){ p u b l i cS t r i n gt o S t r i n g ( ){ r e t u r nn a m e+"( d g e r m a n @ i n d i a n a . e d u ) " ; } } , A R J I T ( " A r j i tS e n g u p t a " ){ p u b l i cS t r i n gt o S t r i n g ( ){ r e t u r nn a m e+"( a s e n g u p t @ i n d i a n a . e d u ) " ;
www.cs.indiana.edu/classes/jett/sstamm/ 4/9

7/15/13

What's New in Java 1.5?

} } , / /a n do nf o rt h er e s t . . . p r i v a t eS t r i n gn a m e ; p u b l i cJ e t t S t a f f ( S t r i n gn ){t h i s . n a m e=n ;} } J e t t S t a f fx=J e t t S t a f f . S U Z A N N E ; S y s t e m . o u t . p r i n t l n ( x ) ;

Last but not least, enums can extend each other. Imagine that! 1.5 Varargs What is your impression of "..."? It's a nice little note that might come in handy. Notice how when you pass arguments from the command line ...
C : / >j a v aM y P r o gabc Y o ug a v em e3a r g s ! Y a y . . . .

... you don't have to pass an array of stuff. The runtime automatically converts the arguments into an array of strings. You can do that now in all of your methods! For example, instead of doing this:
p u b l i cc l a s sV a r A r g s{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ S t r i n g [ ]n e w A r g s={ " a " ," b " ," c " } ; v a M e t h o d ( n e w A r g s ) ; } p u b l i cv o i dv a M e t h o d ( S t r i n g [ ]a r g s ){ S y s t e m . o u t . p r i n t l n ( " Y o ug a v em e"+a r g s . l e n g t h+"a r g s ! Y a y . " ) ; } }

You can declare it more easily, and not have to construct the array ahead of time:
p u b l i cc l a s sV a r A r g s{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ v a M e t h o d ( " a " ," b " ," c " ) ; } p u b l i cv o i dv a M e t h o d ( S t r i n g . . .a r g s ){ S y s t e m . o u t . p r i n t l n ( " Y o ug a v em e"+a r g s . l e n g t h+"a r g s ! Y a y . " ) ; } }

Notice that when you use the . . .syntax, it automatically treats that parameter as an array but you don't have to pass it in that way. Nifty. Let's add one of those for/in loops:
www.cs.indiana.edu/classes/jett/sstamm/ 5/9

7/15/13

What's New in Java 1.5?

p u b l i cc l a s sV a r A r g s{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ v a M e t h o d ( " a " ," b " ," c " ) ; } p u b l i cv o i dv a M e t h o d ( S t r i n g . . .a r g s ){ f o r ( S t r i n gs:a r g s ) S y s t e m . o u t . p r i n t l n ( s ) ; } }

1.6 Static Import Remember making all those interfaces that just have constants in them?
i m p o r tj a v a . a w t . * ; p u b l i ci n t e r f a c eB o u n c i n g B a l l C o n s t a n t s{ p u b l i cs t a t i cf i n a lC o l o rB A C K _ C O L O R=C o l o r . W H I T E ; p u b l i cs t a t i cf i n a lC o l o rB A L L _ C O L O R=C o l o r . B L U E ; p u b l i cs t a t i cf i n a li n tB A L L _ S I Z E=5 0 ; }

Scrap that. Put these into a REAL class and then just import the static members from all the other ones using i m p o r ts t a t i c< P a c k a g eo rC l a s s > ; . This addition is pretty straightforward, and it helps prevent bloat and the "methodless interface" design antipattern. 1.7 Annotations (Metadata) Now for the weirdest part. Annotations are not really something that will affect how you program in Java, unless you need to associate some sort of metadata or annotations with classes, methods, variables, etc. So what are annotations anyway? That's a good question. They provide a little extra information about the classes you write, and a class can use the R e f l e c t i o npackage later to read the annotations. These are useful because you can attach extra information to your code that may determine how it is used or maybe if it is used at all. For example, in J2SE 5, you can declare your intent to override a method like t o S t r i n g ( )in one of your classes:
p u b l i cc l a s sM y C l a s se x t e n d sO b j e c t{ @ O v e r r i d e p u b l i cS t r i n gt o S t r i n g ( ){ r e t u r n" M yo v e r r i d d e nm e t h o d ! " ; } }

In the above example, we declare that we will override the immediately following t o S t r i n g ( )method. So the compiler looks in our superclass (O b j e c t ) for the same metho and makes sure it exists. If for some reason we had overloaded t o S t r i n g ( )by declaring it with different parameters and maybe return type, then the compiler would throw an error saying we didn't override correctly. This is really useful if you want to make sure you
www.cs.indiana.edu/classes/jett/sstamm/ 6/9

7/15/13

What's New in Java 1.5?

override a method as opposed to simply overloading it. Of course you can define your own annotations. They're basically like interfaces, but they can contain values. An example annotation looks like:
p u b l i c@ i n t e r f a c eC o n f e r e n c e{ S t r i n gw h a t ( ) ; S t r i n gw h e n ( ) ; S t r i n gl o c a t i o n ( ) ; }

This annotation declares three members: w h a t ,w h e n ,l o c a t i o nand sets them up to have "getters" and "setters" automatically! That means each @ C o n f e r e n c eannotation has those three fields associated with it, and I don't have to define the accessor and mutator methods to set them up (see the next code listing). If I define this annotation like this, I can use it to mark code that I use for the Jett conference:
@ C o n f e r e n c e ( w h a t = " J E T T " , w h e n = " N o v e m b e r2 0 0 4 " , l o c a t i o n = " I U B " ) p u b l i cc l a s sM y M a g i c J e t t C l a s s{ / / . . . }

And now the @ C o n f e r e n c etype of data is associated with my class. Later on, I could write an analyzer that goes through all of my code and lets me know which classes were used at conferences as well as which conferences they were used at and when. This specific example doesn't have any effect on the way M y M a g i c J e t t C l a s soperates. So the annotations require two-fold cooperation: the programmer must properly annotate her code to provide adequate metadata needed and other developers who will want to know this metadata must know how to extract it using the Java Reflection package. That's a whole hours-long session on how to extract them, so I'm not going to go into depth here. Where are they useful? Say you are working with RMI (Remote Method Invocation) and you don't want all of your methods available remotely. You could annotate the remotable ones with a @ R e m o t eannotation, then whatever serves up the remote access can only allow those to be remotely accessed. There are a ton of great uses for these, and they are fully extendable (you can annotate annotations)!

2. How it will affect you NOW


All of the previous Java language features will still work. Some are deprecated, but not very many. You should also know that the 2004 exam was written before 1.5 was available, so its features are not at all critical. The topics covered by the exam don't include generic typing, annotations/metadata or variable arguments (since really they're just language features and not so much programming constructs). This is good news, since if your students don't immediately master these new features it won't harm them. What if my students use new 1.5 language features on the exam? If they're used properly (and the code is wellwww.cs.indiana.edu/classes/jett/sstamm/ 7/9

7/15/13

What's New in Java 1.5?

designed), the readers will most likely not deduct any points. But in the end the deductions are up to the readers. Automatic boxing and unboxing as well as correct use of the for/in loop will be acceptable. The biggest problem would be a student who tries to use one of these new features (such as generics or enums) but misuses them terribly, obscuring the code. Then a reader might not at all understand what constructs the student was trying to use and mistake it for non-code garbage. Because the readers are as new to the 1.5 features as we are, we cannot expect them to have mastered everything to the point of being able to identify misuse of one. 2.1. To add to your lesson plan... You may not want to add much of this to your lesson plan immediately, since most of it is not necessary for good programming. Sneaking in the for/in loop and autoboxing and unboxing would be quite simple (since the students are likely to leave out the manual boxing and unboxing regardless if the compiler doesn't complain). At least for the time being, if the students can understand Java and write good 1.4 code, it will most likely be acceptable.

3. How it will affect you eventually...


Nobody knows. Really it's up to the AP board and college CS departments where we go from here. How much of this new Java stuff becomes mainstream and intravenous so that we breathe it like objects? It is my personal opinion that the generics, for/in loop, autoboxing static import and varargs stuff is all syntax sugar: it's just a little bit of convienience and not a really big construct such as other big paradigm shifts like procedural to object oriented programming. 3.1. How can the new features help? So now I step off my soapbox and ask you to think of how these new features might streamline your teaching methods. How can you work these into your lesson plan to help teach the ideas of polymorphism, generic types, AOP (Aspect oriented programming: the annotations stuff), and enumerations? In what ways can they help you teach the current topics in a more streamlined way?

References
1. J2SE(tm) 5.0 New Features. Sun Microsystems. http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html 2. New Language Features for Ease of Development in the Java 2 Platform, Standard Edition 1.5: A Conversation with Joshua Bloch. Heiss, Janice J. May 8, 2003. Sun Microsystems. http://java.sun.com/features/2003/05/bloch_qa.html 3. Java 1.5: Where does it fit into AP Computer Science?. Weiss, Mark. Florida International University. AP Central & Collegeboard.com. http://apcentral.collegeboard.com/members/article/1,3046,151-165-036930,00.html
www.cs.indiana.edu/classes/jett/sstamm/ 8/9

7/15/13

What's New in Java 1.5?

4. Java 1.5 Tiger: A Developer's Notebook. McLaughlin, Brett & Flanagan, David. O'Reilly Media, Inc, 2004 ISBN 0-596-00738-8.

www.cs.indiana.edu/classes/jett/sstamm/

9/9

You might also like