Unformatted text preview:

Java 1 5 Jan 13 2019 Versions of Java Oak Designed for embedded devices Java Original not very good version but it had applets Java 1 1 Adds inner classes and a completely new event handling model Java 1 2 Includes Swing but no new syntax Java 1 3 Additional methods and packages but no new syntax Java 1 Java 2 Java 1 4 More additions and the assert statement Java 1 5 Generics enums new for loop Java 5 0 and other new syntax 2 Reason for changes The new language features all have one thing in common they take some common idiom and provide linguistic support for it In other words they shift the responsibility for writing the boilerplate code from the programmer to the compiler Joshua Bloch senior staff engineer Sun Microsystems 3 New features Generics Enhanced for loop Provides all the well known benefits of the Typesafe Enum pattern Static import Automatic wrapping and unwrapping of primitives Typesafe enums Syntactic sugar to support the Iterator interface Autoboxing unboxing Compile time type safety for collections without casting Lets you avoid qualifying static members with class names Scanner and Formatter Finally simplified input and formatted output 4 New methods in java util Arrays Java now has convenient methods for printing arrays Java now has convenient methods for comparing arrays Arrays toString myArray for 1 dimensional arrays Arrays deepToString myArray for multidimensional arrays Arrays equals myArray myOtherArray for 1 dimensional arrays Arrays deepEquals myArray myOtherArray for multidimensional arrays It is important to note that these methods do not override the public String toString and public boolean equals Object instance methods inherited from Object The new methods are static methods of the java util Arrays class 5 Generics A generic is a method that is recompiled with different types as the need arises The bad news The good news Instead of saying List words new ArrayList You ll have to say List String words new ArrayList String Replaces runtime type checks with compile time checks No casting instead of String title String words get i you use String title words get i Some classes and interfaces that have been genericized are Vector ArrayList LinkedList Hashtable HashMap Stack Queue PriorityQueue Dictionary TreeMap and TreeSet 6 Generic Iterators To iterate over generic collections it s a good idea to use a generic iterator List String listOfStrings new LinkedList String for Iterator String i listOfStrings iterator i hasNext String s i next System out println s 7 Writing generic methods private void printListOfStrings List String list for Iterator String i list iterator i hasNext System out println i next This method should be called with a parameter of type List String but it can be called with a parameter of type List The disadvantage is that the compiler won t catch errors instead errors will cause a ClassCastException This is necessary for backward compatibility Similarly the Iterator need not be an Iterator String 8 Type wildcards Here s a simple no generics method to print out any list The above still works in Java 1 5 but now it generates warning messages private void printList List list for Iterator i list iterator i hasNext System out println i next Java 1 5 incorporates lint like C lint to look for possible problems You should eliminate all errors and warnings in your final code so you need to tell Java that any type is acceptable private void printListOfStrings List list for Iterator i list iterator i hasNext System out println i next 9 Writing your own generic types public class Box T private List T contents public Box contents new ArrayList T public void add T thing contents add thing public T grab if contents size 0 return contents remove 0 else return null Sun s recommendation is to use single capital letters such as T for types Many people including myself don t think much of this recommendation 10 New for statement The syntax of the new statement is for type var array or for type var collection Example for float x myRealArray myRealSum x For a collection class that has an Iterator instead of for Iterator iter c iterator iter hasNext TimerTask iter next cancel you can now say for TimerTask task c task cancel 11 Auto boxing and unboxing Java won t let you use a primitive value where an object is required you need a wrapper Similarly you can t use an object where a primitive is required you need to unwrap it int n Integer myVector lastElement intValue Java 1 5 makes this automatic myVector add new Integer 5 Vector Integer myVector new Vector Integer myVector add 5 int n myVector lastElement Other extensions make this as transparent as possible For example control statements that previously required a boolean if while do while can now take a Boolean There are some subtle issues with equality tests though 12 Enumerations An enumeration or enum is simply a set of constants to represent various values Here s the old way of doing it public public public public final final final final int int int int SPRING 0 SUMMER 1 FALL 2 WINTER 3 This is a nuisance and is error prone as well Here s the new way of doing it enum Season WINTER SPRING SUMMER FALL 13 enums are classes An enum is actually a new type of class You can declare them as inner classes or outer classes You can declare variables of an enum type and get type safety and compile time checking enums extend java lang Enum and implement java lang Comparable Each declared value is an instance of the enum class Enums are implicitly public static and final You can compare enums with either equals or Hence enums can be sorted Enums override toString and provide valueOf Example Season season Season WINTER System out println season prints WINTER season Season valueOf SPRING sets season to Season SPRING 14 Advantages of the new enum Enums provide compile time type safety Enums provide a proper name space for the enumerated type If you add remove or reorder constants you must recompile and then everything is OK again Enum printed values are informative With int enums you have to prefix the constants for example seasonWINTER or S WINTER to get anything like a name space Enums are robust int enums don t provide any type safety at all season 43 If you print an int enum you just see a number Because enums are objects you can put them in collections Because enums are classes you can add fields and methods 15 Enums really are classes public enum Coin enums can have instance variables private final int value


View Full Document

Penn CIT 591 - JAVA

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view JAVA and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JAVA and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?