| Program Structure |
public class Class
{
private int intAtribut;
private Object objectAtribut;
public static void main(String[] args)
{
int i = 0;
if( args.length == 1 )
i = args[0];
System.out.println( "You entered " + i + "\n" );
}
}
|
| Comments |
| Data Types |
boolean
char
byte
float
double
short, int, long
object
void
|
| Arrays |
int[] a1 = { 1, 2, 3, 4, 5 };
System.out.println("The array has " + a1.length + "elements.");
a1[0] = 2;
a1[3] = 5;
int [][] a1 = new int[2][3];
int [][] a2 = {{1,2,3},{4,5,6}};
int a3 = new int[2][];
for(int i=0;i< a3.length;i++)
a3[i] = new int[3];
|
| Operators |
* / % + -
=
&& || !
& | ! << >> >>>
++ --
?:
< <= > >= == !=
|
| Decisions |
int x = Math.random();
String message ="";
if( x > 5 )
message = "More than five!";
else
message = "Less than five!";
message = (x > 5) ? "More than five!" : "Less than five!";
if( x == 5 )
{
System.out.println("Yep, we've got 5!");
System.out.println("Need {} for multiple statements in an if");
}
else if( x == 10 )
System.out.println("Ten");
else
System.out.println( "Something else");
char c = (char)(Math.random() * 26 + 'a');
switch(c)
{
case a : System.out.println("c is equal to 'a'"); break;
case b : System.out.println("c is equal to 'b'"); break;
default: System.out.println("Something else");
}
|
| Loops |
int i = 0;
while(i < 5)
i++;
for (int i = 0; i < 5; i++)
{
int num = i * 2;
System.out.println( "i*2=" + num);
}
do
{
i++;
System.out.println("i= " + i);
}
while(i < 5 );
Vector collection = new Vector();
for(int i = 0; i < 7; i++)
collection.addElement(i);
colection.addElement("string");
Enumeration e = collection.elements();
while(e.hasMoreElements())
((int)e.nextElement()).print();
for( int i = 0; i <= 20; i++ )
{
if( (i % 2) == 0 )
continue;
else if( (i%3) == 0 )
break;
}
|
| Casting & Conversion |
float floatNum = 9.9;
int intNum = (int) floatNum;
string stringNum = "100";
Integer stringInt = Integer.valueOf(stringNum);
|
| Enumeration |
Hashtable products = new Hashtable();
Enumeration enum = products.elements();
while (enum.hasMoreElements()){
System.out.println(enum.nextElement());
public class Bach extends EnumSyntax {
public static final Bach JOHANN_SEBASTIAN = new Bach(0);
public static final Bach WILHELM_FRIEDEMANN = new Bach(1);
public static final Bach CARL_PHILIP_EMMANUEL = new Bach(2);
public static final Bach JOHANN_CHRISTIAN = new Bach(3);
public static final Bach P_D_Q = new Bach(4);
}
Bach theComposer;
if (theComposer == Bach.JOHANN_SEBASTIAN) {
System.out.println ("The greatest composer of all time!");
|
| Namespaces |
|
| Classes & Structs |
public
protected
private
static
final
public class Base
{
}
public class Child extends Base
{
}
|
| Interfaces |
interface Vehicle
{
int i = 5;
void accelerate( int newSpeed );
void drive();
}
class Car implements Vehicle
{
void accelerate( int newSpeed );
}
|
| Functions |
public void DoSomething( int x, Object y)
{
y.setValue("someThing");
x = 5;
}
|
| Exception Handling |
throw new Exception("Exception message.");
try
{
y=0;
m=x/y;
}
catch( SomeException e)
{
}
catch( Exception e)
{
}
finally
{
}
|
| Constructors/Desctructors |
class Rock {
Rock() {
System.out.println("Creating Rock");
}
}
class Rock {
Rock(int i) {
System.out.println("Creating Rock number " + i);
}
}
|
| Properties |
private int mSize;
public int size
{
get{ return mSize; }
set{ mSize = (value > 0) ? value : 0;}
}
|
| Delegates |
|