Hosted By:
Lunarpages.com Web Hosting
Choose Language:
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
                    //Single line Comment
                    
                    /*Multi
                      line Comment*/

                     /** Javadoc comment
                     * Shows authors information.
                     * @author someman
                     * @author http://www..somemancom
                     * @version 1.0
                     */ 
				
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;            //New array: 2,2,3
                    a1[3] = 5;            //Thows exception
                    
                    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
                
                    //Arithmetic
                    * / % + - 
                    
                    //Assignment
                    = 
					
                    //Logical
                    && || ! 
                    
                    //Bitwise
                    & | ! << >> >>>     

                    //Increment/Decrement
                    ++ --
					
                    //Conditional operator
                    ?:         

                    //Comparison
                    < <= > >= == != 
                
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
                //Pre-test
                int i = 0;
                while(i < 5)
                    i++;
                    
                for (int i = 0; i < 5; i++)
                {
                    int num = i * 2;
                    System.out.println( "i*2=" + num);
                }
                
                //Post-test
                do
                {
                    i++;
                    System.out.println("i= " + i);
                }
                while(i < 5 );
                
                //Collection iteration
                //Collection in Java is interface for List,Set,Vector,Map etc.
                Vector collection = new Vector();
                for(int i = 0; i < 7; i++)
                    collection.addElement(i);
                // Not a problem to add a string to int's:
                colection.addElement("string");
                Enumeration e = collection.elements();
                while(e.hasMoreElements())
                    ((int)e.nextElement()).print();                    
				
                //Loop control
                for( int i = 0; i <= 20; i++ )
                {
                    if( (i % 2) == 0 )
                        continue;       //Skip to next iteration
                    else if( (i%3) == 0 )
                        break;          //Exit loop
                }
                
Casting & Conversion

                    float floatNum = 9.9;
                    int intNum = (int) floatNum;    //decimal is truncated, intNum is set to 9
                    
                    string stringNum = "100";
                    Integer stringInt = Integer.valueOf(stringNum);    //Convert string to int 
				
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
                    //All of your files automatically live in their
                    //own namespaces, and each class within a file must have a unique
                    //identifier. So you do not need to learn special language features to solve
                    //this problem, the language takes care of it for you.
                    
				
Classes & Structs
                    //Accessibility
                    public
                    protected
                    private
                    static
                    final
					                    
                    //Inheritance
                    public class Base
                    {
                    }
                    
                    public class Child extends Base
                    {
                    }                    
                
Interfaces
                    //Interface definition
                    interface Vehicle
                    {
                        int i = 5; // static & final
                        // Cannot have method definitions:
                        void accelerate( int newSpeed );
                        void drive(); // Automatically public
                    }
                    
                    class Car implements Vehicle
                    {
                        void accelerate( int newSpeed );//Must have method definitions:
                    }
                
Functions
                    //Arguments are passed by value and reference
                    public void DoSomething( int x, Object y)
                    {
                        y.setValue("someThing"); //Changes are visibly after returning from  
                                                 //function to arguments passed by reference
                        x = 5; //Changes are not visibly after returning from function 
                               //to arguments passed by value
                    }                    
               
Exception Handling
                    //Throwing an exception
                    throw new Exception("Exception message.");
                    
                    //Catching an exception
                    try
                    {
                        y=0;
                        m=x/y;
                    }
                    catch( SomeException e)
                    {
                        //Handle the exception here
                    }
                     catch( Exception e)
                    {
                        //Handle the exception here
                    }
                    finally
                    {
                        //This is always executed, whether exception is caught or not
                    }
                
Constructors/Desctructors
                    // Demonstration of a simple constructor.
                    class Rock {
                        Rock() { // This is the constructor
                            System.out.println("Creating Rock");
                        }
                    }

                    // Constructors can have arguments.
                    class Rock {
                        Rock(int i) {
                            System.out.println("Creating Rock number " + i);
                        }
                     }            
                    // Destructors do not exist in Java.
                    //For deleting object Java uses Garbage Collector.
                    //He starts automaticly and we can't predict when he is going to be started.
                    					    
                
Properties
                    private int mSize;                    
                    //get and set are optional
                    //leave get out for write only
                    //leave set out for read only
                    
                    public int size
                    {
                        get{ return mSize; }
                        set{ mSize = (value > 0) ? value : 0;}
                    }
                
Delegates
                    //There is no such thing in Java