Documentos de Académico
Documentos de Profesional
Documentos de Cultura
NEW IN
JAVA5
Overview
This paper is about java5 and how you can use the new functionality in
java5
We will look at briefly at those new features in java5 as well as:
Generic, annonation, autoboxing, declare arguments in fuction
definition, new printf, scanners, static imports and using enum.
Page 2
What is new in java5
TABLE OF CONTEXT:
1- Annotation
2- Generic
3- Autoboxing
4- Arguments
5- Printf, scanners
6- Static imports
7- Enum
8- For-loop
Page 3
What is new in java5
FOREWORD
Page 4
What is new in java5
USING ANNOTATION
Annotation is used for the first time in java5 and it has many benefits
as it can help us with the consistency between classes, check validity
of variables in the runtime.
You can use annotation to add meta-data to different elements as
class, methods, field enum, interface and so on. You can use
annotation anywhere that you can use the other modifaction as public,
static and final.
The meta-data are processed by compilers, javadoc, and etc.
You can also define your own annotation. Once you define yours, you
can use it where ever you want.
Page 5
What is new in java5
GENERICS
Page 6
What is new in java5
In old factions:
ArrayList myArrayList = new ArrayList();
myArrayList = anotherArraylist;
String myString = (String) myArrayList.getFirst();
In the fisrt case you should know the kind of your arraylist and you
should cast the value to the right type
And in the second case you have already use the right type of arraylist.
For more information look at my papers about Generic in java5
AUTOBOXING
It gets easier to use different types of datatype look at the following
example
Page 7
What is new in java5
Old version
Int myint= 55;
Interger myInteger = new Integer(myint);
In java5
Int myint= 55;
Integer myinteger= myint;
ARGUMENTS
There is a new opportunity in java5.
You can define just one method and call it with different numbers of
parameters as:
Public void newMethod(Object … args){}
And
Call it as newMethod(22);
newMethod(22,”mytest”);
Page 8
What is new in java5
and so on. The compiler takes hand about the numbers of arguments
automatically.
Printf
If you are worked with C, and have written some lines in C, you know
what is it about.
Printf give you more control of output and manipulating the output.
For example if you write as this line
Calendar cal = Calendar.getInstance();
System.out.printf(" today is: %1tA ", cal);
System.out.printf("this month is: %1TB", cal);
System.out.println();
System.out.printf("this year is: %1TY", cal);
System.out.printf ("pi = %5.2f, e = %5.4f %n", Math.PI, Math.E);
As you can see how easy you can working with different formats and
print out the part that you want.
SCANNER
To reading file or reading from standard input (as term in C) get easier
too-,
Look at this example for readin from a file:
File myfile = new File(“mytextfile”);
//We will read file by one line at once
Scanner scanner = new Scanner(myfile);
Page 9
What is new in java5
While(scanner.hasNextLine())
System.out.println(scanner.nextLine());
STATIC IMPORT
This new feature in java5 allow you just use static members in a class
For example before java 5, if you want to use the PI, you should write
as Math.PI
But in java 5 you import as those static members and just use them as
follow
Import static java.lang.Math;
Pulic class SomeName{
Public static void main(String arg[]){
System.out.printf(("pi = %5.2f”,PI);
}
}
And an other good example can be as follow
Page 10
What is new in java5
ENUM
In java5, you can work with enum more saftly, easier, cleaner than
other versions.
You got at least 4 different problem with enum before,
-typesafe
-namespace
-deficulty, to recompiling the code if you want to change or add any
constant
-none informative, because it just return integer.
Example:
public enum ENUMTEST {
DODGE, CHEVROLET, CAMERO, BENZ, AUDI, BMW;
Page 11
What is new in java5
Page 12
What is new in java5
FOR-LOOP
In old fashion:
Collection collection=hibernate.getResult();
for (Iterator<User> iterator = collection.iterator(); iterator.hasNext(); )
{
User user = iterator.next();
System.out.println(user.getName());
}
Problem here are: it is ugly, opportunity for error; and iterator has
repeated 3 times;
In java5
Collection collection=hibernate.getResult();
for (User user : collection){
System.out.println(user.getName());
}
It is more simple, more clean,
An other example:
Page 13
What is new in java5
To be continuing...
DONATION
Page 14
What is new in java5
Page 15
What is new in java5
MY OTHER PAPERS
Page 16
What is new in java5
Page 17