Java und Komponente, Zusammenfassung Teil 1
Java Übersicht < -- oder --> gehe zu Java and Components Part 2// Einzeilige Kommentare
/*
mehrzeilige
Kommentarblöcke */
Einfache Datentypen (primitive datatypes)
byte
8 Bit (-128 bis 127)
short
16 Bit (-32768 bis 32767
int
32 Bit (-2.147.483.648 bis 2.147.483.647)
long
64 Bit (-9.2E18 bis 9.2E18)
char Zeichen 16 Bit
boolean (true, false)
float
Fliesskommazahl 32 Bit
double Fliesskommazahl 64 Bit
static final double PI = 3.14456;
int primZahlen
[ ] = {2,3,5,7};
float matrix [ ] [ ];
Integer arrayOfInteger[
];
arrayOfIntegers = new Integer
[100];
for ( int i= 0; i<arrayOfIntegers.length;
i++)
{ arrayOfIntegers[i] = Integer(i); }
if (p instance
of ClassName)
{ s = (ClassName)p); }
if (boolean) statement
else statement
switch
(var)
{
case value: statement
case default: statement
}
break [label];
continue [label];
return (value);
for(int i=0; i<value;
i++) statement
while (boolean) statement
do statement while (boolean)
label: statement
ÄusserstesPackage.InneresPackage.InnerstesPackage
import
java.awt.event.*
(java.lang wird automatisch importiert)
class
Student extends Person implements Printable,
PersonInterface
// final bedeutet, das die Klasse
{
// nicht mehr abgeleitet werden kann
private String name;
public
Person()
//Standart Konstuktor
{ super(); //ruft den Konstruktor
der Oberklasse auf
name = new String; }
public void finalize()
//Destruktor
{ //....; }
public String toString()
{ return name; }
public void print()
{ System.out.println("Der Name lautet" + name);
}
}
abstract class
GraphischesObjekt
{
abstact void draw(); //rein virtuelle Methode;
}
equals(Object) Vergleicht zwei Objekte auf inhaltliche
Gleichheit
finalize
Destruktor, nach Bedarf überschreiben
getClass()
Liefert ein Objekt vom Typ Class, Informationen über die Klasse
objektA.getClass().getName()
toString()
Wandelt ein Objekt in eine String-Repräsentation, zum überschreiben
clone()
CopyConstruktor clone() verwendent
notify(), notifyAll(),
wait()
zur Synchronisation von Threads
interface Collectable
[extends listOfSuperInterfaces]
{
int MAXIMUM_ELEMENTS = 1000; //Konstante
void add(Object anObject);
Object find(Object anObject);
}
Exceptionhandling
try
{
if ( true ) throw new IOException("damn
shit")
}
catch (<ExceptionClassName>
<aObject>
{ do this and that }
[
finally
//Optionaler Block
{ ..
// Dieser Block wird immer Ausgeführt
} ]
Application app;
MenuItem createMenuItem(String
label, char shortcut)
{
MenuItem item = new MenuItem(label, new
MenuShortCut(shortcut));
item.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent
e)
{ app.doCommand(command);
}
} );
return item;
}
class SimpleThread
extends Thread implements Runnable
{
public SimpleThread(String str)
{ super(str); }
}
public void run()
{
for(int i=0; i<10; i++)
{ System.out.println(I + " " +getName());
try
{ sleep((int) (Math.random()*1000));
//oder Thread.sleep(milliSeconds);
}
catch (InterruptdedException e) { }
System.out.println("DONE! "+ getName
() );
}
new SimpleThread("Holla").start(); //Thread starten
public
synchronized int get()
{
while (avaiable == false)
{
try
{ wait(); //gibt Monitor temporär frei
} catch (InterruptedException e) { }
}
avaible = false;
notifyAll;
return content;
}