Java Code Templates

From PeacockWiki

Jump to: navigation, search

Contents

Naming Conventions

Class names start with caps

Class MyClass
{
}

Variable names start with lowercase

MyClass myVariable;

Method Names start with lowercase

public void myMethod()
{
}

Objects

Creating an Object

= new ();

eg.

String myString = new String("MyStringValue");

Calling a method of a object

.();

Arrays

Creating and instantiating an array

[];
= new [];

Loops

For Loops

for(assignment;comparator;incrementer)
{
  do something here;
}
for( = ; [<] ; ++)
{
  do something here;
}

this for loop is equivilant to the following while loop

=
While( [<] )
{
  do something here.
  ++
}

Pre-Tested

While is a "pre-tested" loop. The condition is tested before the code block is executed. As a result, it is possible that the code block will never execute.

while(condition)
{
}

Post-Tested

Do is a "post-tested" loop. The condition is tested after the code block is executed. As a result, the code block will always execute at least once.

do
{
}
while(condition)
Personal tools