Am 28.07.2011 wurde die 7te Version von Java veröffentlicht. Windows und Linux Nutzer können sofort von der neuen Version profitieren. Mac-User müssen noch etwas warten.
Das sind die wichtigsten Veränderungen:
- Neue Filesystem-API (NIO 2)
- Bibliotheken für SCTP, SDP und TLS
- Unicode 6.0 Unterstützung
- JDBC 4.1
- Look-and-Feel wird Bestandteil der Spezifikation
- Sprachverbesserungen zur Unterstützung von dynamischen Programmiersprachen
Einige Veränderungen
Unterstriche
Seit Java 7 ist es möglich in Zahlen Unterstriche zu verwenden, welche eine bessere Lesbarkeit von Zahlen (vor allem von großen Zahlen) ermöglichen:
long zahl1 = 100__00; long zahl2 = 10_00_00;
Nicht möglich sind Unterstriche am Anfang oder am Ende. Ebenfalls sind Unterstriche vor oder nach einem Punkt nicht zugelassen.
long zahl1 = _100; // nicht zugelassen long zahl2 = 100_; // nicht zugelassen double zahl3 = 100_.00; // nicht zugelassen double zahl4 = 100._00; // nicht zugelassen
Collections
Eine weitere Veränderung betrifft Collections. In Java 6 mussten Collections wie folgt initialisiert werden:
Java 6:
Map<String,List<String>> collection= new HashMap<String, List<String>>();
Ab Java 7 muss man die Typen hinter „new“ nicht mehr erneut angeben.
Map<String,List<String>> collection= new HashMap();
Switch
Eine weitere Veränderung betrifft switch. Das ist eine Funktion auf die ich sehnsüchtig gewartet habe.
Es handelt sich nämlich um eine Unterstützung von Strings bei dem switch-Konstrukt.
Somit ist folgendes möglich:
public void istWochenende(String tag){
switch(tag){
case "Freitag":
System.out.println("Wochenende");
break;
case "Samstag":
System.out.println("Wochenende");
break;
case "Sonntag":
System.out.println("Wochenende");
break;
default:
System.out.println("Kein Wochenende");
}
}
Ausnahmen
Es gibt einige wesentliche Veränderungen bei der Ausnahmebehandlung in Java.
1.1. Multicatch
Eines der Veränderungen stellt das multi-catch dar, welches das Behandeln von mehreren Exceptions in einem Rumpf ermöglicht.
try { ...
} catch (IOException | InterruptedException ex) {
System.err.println(ex.toString());
}
1.2. Try-With-Ressources
Hat man mit Objekten gearbeitet, welche im Anschluss geschlossen werden sollten, muss man das in der Zukunft nicht mehr tun (Vorausgesetzt die Klasse implementiert das Interface AutoCloseable). Es ist also folgendes möglich:
Beispiel Java 6:
InputStream is = null;
try {
is = new FileInputStream(new File("conf.cfg"));
} catch (IOException ex) {
…
Beispiel Java 7
try (InputStream is = new FileInputStream(new File("conf.cfg"))) {
...
} catch (IOException ex) {
}
Um Strings in einem bestimmten Format zu speichern, kann man einfach an die Datenbank-URL folgendes ranhängen:
=> jdbc:mysql://localhost:3306/database?characterEncoding=latin2";
Nun werden alle String in dem Latin-2-Format abgespeichert.
Database.java
import java.util.ArrayList;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.ext.DatabaseFileLockedException;
public class Database {
static private Database instance = null;
static private String path = "database.db";
// Eigentlich ein Singelton
static public Database get(Object o) {
if (instance == null) {
try {
return new Database(o);
} catch (final Exception e) {
e.printStackTrace();
}
} else {
return instance;
}
return instance;
}
private final Class classType;
private final ID object;
private Database(Object o) throws Exception {
this.classType = o.getClass();
System.out.println(o.getClass());
this.object = (ID) o;
}
private boolean checkForObject() {
final ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), Database.path);
try {
final ObjectSet<ID> zustand = db.query(this.classType);
for (final ID o : zustand) {
if (o.getId() == this.object.getId()) {
return true;
}
}
} catch (final DatabaseFileLockedException e) {
throw new DatabaseFileLockedException("Datenbank Exception");
} finally {
db.close();
}
return false;
}
public ArrayList<Object> getAll() {
final ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), path);
final ArrayList<Object> arrayList = new ArrayList<Object>();
try {
final ObjectSet<Object> result = db.queryByExample(new Object());
while (result.hasNext()) {
final Object object = result.next();
arrayList.add(object);
}
} catch (final DatabaseFileLockedException e) {
e.printStackTrace();
} finally {
db.close();
}
return arrayList;
}
public ArrayList<Object> getAll(Class c) {
final ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), path);
final ArrayList<Object> arrayList = new ArrayList<Object>();
try {
final ObjectSet<Object> result = db.queryByExample(c);
while (result.hasNext()) {
final Object object = result.next();
arrayList.add(object);
}
} catch (final DatabaseFileLockedException e) {
System.out.println("not closed");
} finally {
db.close();
}
return arrayList;
}
public void remove() {
final ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), Database.path);
try {
final ObjectSet<ID> zustand = db.query(this.classType);
for (final ID o : zustand) {
if (o.getId() == this.object.getId()) {
db.delete(o);
}
}
} catch (final DatabaseFileLockedException e) {
throw new DatabaseFileLockedException("Datenbank Exception");
} finally {
db.close();
}
}
public void save() throws Exception {
if (this.checkForObject() == false) {
final ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), Database.path);
try {
db.store(this.object);
} catch (final DatabaseFileLockedException e) {
} finally {
db.close();
}
} else {
throw new Exception("Object allready exsist. You must update");
}
}
public void update() throws Exception {
final ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), Database.path);
try {
final ObjectSet<ID> zustand = db.query(this.classType);
for (final ID o : zustand) {
if (o.getId() == this.object.getId()) {
o.copyAttributes(this.object);
db.store(o);
}
}
} catch (final DatabaseFileLockedException e) {
} finally {
db.close();
}
}
}
static private Object instanz;
static public Object getInstance(){
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),Database.path);
try{
ObjectSet<Object> zustand = db.query(Object.class);
if(zustand.size()==0){
return new Object();
} else if(zustand.size()==1){
return zustand.get(0);
} else{
throw new IllegalStateException("Nur ein Objekt kann gespeichert werden");
}
} catch(final DatabaseFileLockedException e){
} finally{
db.close();
}
return instanz;
}
public ArrayList<Object> readWithSODA(final Object o, final String method,
final String valueAsString) {
// DB_PATH: Datenbank-Pfad als String
ObjectContainer db = Db4o.openFile(DB_PATH);
ObjectSet<Object> result = db.query(new Predicate<Object>() {
@Override
public boolean match(Object arg0) {
Method c = null;
try {
c = arg0.getClass().getMethod(method);
c.setAccessible(true);
Object o = c.invoke(arg0);
return o.toString().equals(valueAsString);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return true;
}
});
ArrayList<Object> resultList = new ArrayList<Object>();
for (Object object : result) {
resultList.add(object);
}
db.close();
return resultList;
}
Database db = Database.getInstance();
User u = new User();
u.setName("Max");
u.setNachname("Mustermann");
db.store(u);
System.out.println(db.readWithSODA(new User(), "getName", "Max"));
Ergebnis:
[[Mustermann ,Max]]



