Jump to content

Software Engineers Handbook/Language Dictionary/Java

From Wikibooks, open books for an open world

Here is the Java wikipedia entry.

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

Execution Entry Point

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

General Syntax

[edit | edit source]

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

a = b;

Comments

[edit | edit source]
// 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 | edit source]
int x = 9;
Integer y = new Integer(4);

Method Declaration/Implementation

[edit | edit source]
// 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 | edit source]

Scope is defined by curly braces.

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

Conditional Statements

[edit | edit source]

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 | edit source]

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 | edit source]
System.out.println( "Hello World!" );

Containers

[edit | edit source]

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

Algorithms

[edit | edit source]

The Collection class has algorithms like sort.

Garbage collection

[edit | edit source]

Garbage collection is automatic.

Physical Structure

[edit | edit source]

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

  • 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 | edit source]

Books and Articles

[edit | edit source]

paper references here