Software Engineers Handbook/Language Dictionary/Object Oriented/Java

From Wikibooks, open books for an open world
Jump to: navigation, search

Java[edit]

Here is the Java wikipedia entry.

Type[edit]

Java is a full, procedural, object-oriented language.

Execution Entry Point[edit]

public static void main(String args[])
{
    // some functionality here
}

General Syntax[edit]

The typical statement is completed by a semi-colon. For the assignment of b to a use:

a = b;

Comments[edit]

// this is an inline comment.  Everything after the // is a comment.

Block comments are specified by a starting /* and ending */ They can span multiple lines.

/*
 * this is a block comment 
 */

Variable Declarations[edit]

int x = 9;
Integer y = new Integer(4);

Method Declaration/Implementation[edit]

// declaration
private return_type class_name::function_name(argument_1_type arg_1_name, 
                          argument_2_type arg_2_name, 
                          default_argument_type default_arg_name)
{ // implementation
    // work with arg_1_name, arg_2_name, and default_arg_name
    // depending on the argument types the variables are passed by 
    //   value, reference, or are constant
    // don't forget to return something of the return type
    return 36;
}

Scope[edit]

Scope is defined by curly braces.

{ // this the the beginning of a scope
    // the scope is about to end
}

Conditional Statements[edit]

If and only if A is equal to B assign C to D, otherwise, assign E to F.

if( A == B )
{
    D = C;
    // more code can be added here.  It is used if and only if A is equal to B
}
else
{
    F = E;
    // more code can be added here.  It is used if and only if A is not equal to B
}

or

if( A == B ) 
    D = C; //more lines of code are not permitted after this statement
else
    F = E;

Alternatively, a switch statement can be used for multiple choice operations. This sample converts a number input to text.

switch( number_value )
{
    case 37:
        text = "thirty-seven";
        break; // this line prevents the program from writing over this value with the
               //   following code
    case 23:
        text = "twenty-three";
        break;
    default: // this is used if none of the previous cases contain the value
        text = "unknown number";
}

Looping Statements[edit]

This code counts from 0 to 9, adding up the contents of the array.

int i = 0;
for( int index = 0; index < 10; index = index + 1 )
{
    i = array[index];
}

This code repeats until the number 4 is found. If this runs off of the end of the array, there could be a problem.

int index = 0;
while( 4 != array[index] )
{
    index = index + 1;
}

This code increments the counter before the check is made, so that it starts with element 1.

int index = 0;
do
{
    index = index + 1;
}
while( 4 != array[index] );

Output Statements[edit]

System.out.println( "Hello World!" );

Containers[edit]

Containers inherit from the Collection class. See the java.util package for specific containers including List, LinkedList, Queue, Stack, Dictionary and HashMap.

Algorithms[edit]

The Collection class has algorithms like sort.

Garbage collection[edit]

Garbage collection is automatic.

Physical Structure[edit]

Code is generally kept in files with a .java extension. It is compiled into Java byte code into files with .class extensions.

Tips[edit]

  • Classes in the Java packages are capitalized, methods are not.
  • Everything is a pointer. Use a clone method to avoid operating on the original element of a Collection.
  • Arrays start with index 0.
  • Don't confuse these two:
=  // assignment
== // comparison, is equal to

Often using the one you don't want will compile, and will produce results you did not expect.

Web References[edit]

Books and Articles[edit]

paper references here