Software Engineers Handbook:Language Dictionary:Object Oriented:Java
From Wikibooks, the open-content textbooks collection
[edit] Java
Here is the Java wikipedia entry.
[edit] Type
Java is a full, procedural, object-oriented language.
[edit] Execution Entry Point
public static void main(String args[])
{
// some functionality here
}
[edit] General Syntax
The typical statement is completed by a semi-colon. For the assignment of b to a use:
a = b;
[edit] Comments
// 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 */
[edit] Variable Declarations
int x = 9; Integer y = new Integer(4);
[edit] Method Declaration/Implementation
// 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;
}
[edit] Scope
Scope is defined by curly braces.
{ // this the the beginning of a scope
// the scope is about to end
}
[edit] Conditional Statements
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";
}
[edit] Looping Statements
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] );
[edit] Output Statements
System.out.println( "Hello World!" );
[edit] Containers
Containers inherit from the Collection class. See the java.util package for specific containers including List, LinkedList, Queue, Stack, Dictionary and HashMap.
[edit] Algorithms
The Collection class has algorithms like sort.
[edit] Garbage collection
Garbage collection is automatic.
[edit] Physical Structure
Code is generally kept in files with a .java extension. It is compiled into Java byte code into files with .class extensions.
[edit] Tips
- 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.
[edit] Web References
- Java wikibooks page
- The Sun java page has the API online as well as code examples.
[edit] Books and Articles
paper references here

