Tips&Tricks | I trucchi del mestiere |
![]() |
Usare cut & paste in Java |
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); String str = textarea.getText(); StringSelection contents = new StringSelection(str); cb.setContents(contents, null); |
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable content = cb.getContents(this); try { String str = (String)content.getTransferData(DataFlavor.stringFlavor); textarea.setText(str); } catch (Throwable e) { System.err.println(e); } |
![]() |
Concatenazione di stringhe e performance |
String string1 = "Ciao "; String string2 = "Federico"; string1 = string1 + string2; |
StringBuffer buffer1 = "Ciao "; String string2 = "Federico"; buffer1.append(string2); |
![]() |
Pass-by-Value e Pass-By-Reference |
void passByValue(int i) { f = 15; } void passByReference(StringBuffer sb) { sb.append(" vedo?"); } void provaMetodi() { int i = 1; StringBuffer sb = new StringBuffer("Cosa"); passByValue(i); passByReference(sb); System.out.println("Value of i: " + i); System.out.println("Value of sb: " + sb); } |
Value of i: 1 Value of sb: Cosa vedo? |
![]() |
La corto-circuitazione degli operatori booleani |
if ( object != null && object.equalsTo(otherObject) ) { // fai qualcosa con object } |
![]() |
Thread ed eccezioni |
public void uncaughtException(Thread t, Throwable e) |
Thread(ThreadGroup tg, Runnable target, String name) Thread(ThreadGroup tg, Runnable target) Thread(ThreadGroup tg, String name) |
![]() |
Internazionalizzare le applicazioni |
NumberFormat nf2 = NumberFormat.getInstance(Locale.ITALIAN); System.out.println(nf2.format(1234.56)); |
1.234,56 |
![]() |
Classi interne e classi anonime |
class UnaClasseNormale { void unMetodo() {} void unAltroMetodo() {} } class UnAltraClasseNormale { // Queste due classi saranno visibili solo in UnAltraClasseNormale static class UnaClasseInterna {} // classe interna statica class AltraClasseInterna {} // classe interna void unMioMetodo() { class ClasseInternaLocale {} // classe interna locale al metodo } void mioAltroMetodo() { // classe anonima: ridefinisce un metodo della classe originale UnaClasseNormale bref = new UnaClasseNormale () { void unMetodo() {} // Qui ridefinisco il metodo originale }; } } |
![]() |
Come dichiarare array anonimi |
myMethod(new int[] {10, 23, 45, 9, 12, 59}); // dichiarazione di myMethod: void myMethod(int[] values) |
![]() |
Gestire le versioni dei package |
Manifest-Version: 1.0 Name: it/fedmest/myclasses Specification-Title: Java Package con Versioni Specification-Vendor: Federico Mestrone Specification-Version: 1.0 Implementation-Title: it.fedmest.myclasses Implementation-Vendor: FedericoMestrone.Com Implementation-Version: Build 1.0.3-b32 |
Package pkg = Package.getPackage("it.fedmest.myclasses"); System.out.println("Package name:\t" + pkg.getName()); System.out.println("Spec title:\t" + pkg.getSpecificationTitle()); System.out.println("Spec vendor:\t" + pkg.getSpecificationVendor()); System.out.println("Spec version:\t" + pkg.getSpecificationVersion()); System.out.println("Impl title:\t" + pkg.getImplementationTitle()); System.out.println("Impl vendor:\t" + pkg.getImplementationVendor()); System.out.println("Impl version:\t" + pkg.getImplementationVersion()); |
![]() |
Realizzare un file Zip in Java |
BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream("D:\\filezippato.zip"); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); out.setMethod(ZipOutputStream.DEFLATED); // Attiva la compressione byte data[] = new byte[2048]; FileInputStream fi = new FileInputStream("D:\\filenormale.doc"); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry("D:\\filenormale.doc"); out.putNextEntry(entry); int count; while((count = origin.read(data, 0, 2048)) != -1) { out.write(data, 0, count); } origin.close(); out.close(); |
![]() |
ZipAnywhere: WinZip secondo Java |
![]() |
Memoria a disposizione |
Runtime rt = Runtime.getRuntime(); System.out.println("Memoria totale massima a disposizione della VM: " + rt.totalMemory()); System.out.println("Memoria ancora non utilizzata di quella a disposizione: " + rt.freeMemory()); |