Está en la página 1de 17

WHAT IS

NEW IN
JAVA5

[Pick the By Martin Nad


date] martinnad@ymail.com
What is new in java5

What is new in java5


OVERVIEW

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.

How you define your annotation


We will look at an example:

Public @interface mytype {


Int myid();
String myString();
}
Here in the annotation you are not allowed to use parameters and
throws clauses.

Page 5
What is new in java5

@mytype(myid= 123, myString=”testing”)


Public static void myTypeTest(){}

If you have just one parameter in your @interface the paramtere


should name “value”;

Public @interface myoneparametertype{


String value;
}

Standard Annotation in Java 5


There is 3 pre definition parameter in java 5
1- Overridden
2- Suppresswarnings
3- Depprecture
Overridden tells to the compiler that the function is a overridden
function in the same class
Suppresswarnings is using mostly with Generic in Java5. When you use
a arrayList whit an unknown type, you should use this annotation
Depprecture tell to the programmer the function shouldn’t use any
more.
For more detail about annotation in java5 look at my paper for how to
use annonation in java, and hibernate.

GENERICS

Page 6
What is new in java5

In java5 it is possible to passed the types of the parameters to a class


as values to the methods.

A typical example is when you using Arraylist, linkedList and so on.


For example look at to the this examples:

In old factions:
ArrayList myArrayList = new ArrayList();
myArrayList = anotherArraylist;
String myString = (String) myArrayList.getFirst();

With using Generic:

ArrayList<String> myArrayList = new ArrayList<String>();


//some working on myArrayList
String myString = 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;

And also it is easier using Boolean too


Boolean test= new Boolean(false);
In older version
If(test == false){…}
Java 5
If(!test){…}

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());

Or reading from keyboard:


Scanner scanner= new Scanner(System.in);
System.out.println(“Give the car name: ”);
String typeofcar = scanner.nextString();
System.out.println(“Give the how old is your car:”);
Int age= scanner.nextInt();
System.out.println(typeofcar+" "+age);

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

Import static java.lang.Math;


Import static java.lang.System.out;
Pulic class SomeName{
Public static void main(String arg[]){
PrintStream o =System.out ;
o.printf(("pi = %5.2f”,PI);
}
}

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;

private static EnumSet<ENUMTEST> primary;

Page 11
What is new in java5

private static EnumSet<ENUMTEST> secondary;

public static EnumSet<ENUMTEST> getPrimaryColors() {


if(firstchose == null)
return EnumSet.of(DODGE, BENZ, CHEVROLET);
return firstchose;
}

public static EnumSet<ENUMTEST> getSecondaryColors() {


if(secondchose == null)
return EnumSet.complementOf(getPrimaryColors());
return secondchose;
}

public static void main(String[] args) {


System.out.println("First choose:
"+ENUMTEST.getPrimaryColors());
System.out.println("Second choose:
"+ENUMTEST.getSecondaryColors());
System.out.println("All: "+EnumSet.allOf(ENUMTEST.class));
}
}

Read more about Enum in my peaper, “Enum 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:

String[] types = new String[] {"Dodge", "chevrollet", "Benz", “BMW”};


for(int i = 0; i < type.length; i++) {
System.out.println("Type " + types[i]);
}
Now in java5

Page 13
What is new in java5

String[] types = new String[] {"Dodge", "chevrollet", "Benz", “BMW”};


for(String type : types) {
System.out.println("types " + type);
}

To be continuing...

DONATION

If you like this documentation and it was helpful


you can just donate 7$ by using this
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
xclick&hosted_button_id=5814019 or use this to put
any amont you like to donate
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
xclick&hosted_button_id=5813954 and if it doesn’t work
copy the link and put it on your browser.

Page 14
What is new in java5

if it doesn’t work copy the link and put it on your browser.

Page 15
What is new in java5

MY OTHER PAPERS

Properties in Java and Spring by Martin Nad


Spring and Cxf by Example
Jboss and Perm Gen Error
Using Ftp Service With Spring
JunIt
How to use Maven
ReFactoring
Maven and Custome Archetype
How to Write Effective Code
Using Generic in Java Why and where
what is new in java 5

Page 16
What is new in java5

Page 17

También podría gustarte