Java Glossary : Java Cheat Sheet

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : J words : Java Cheat Sheet.

Java Cheat Sheet
This document is a quick summary of the Java language syntax. It is not complete. It just contains the things you may be likely to forget.

I have most of these common patterns built in as SlickEdit macros so I don't have to remember the patterns. I just click a custom icon for the pattern I want. Unfortunately, I thus don't remember them. Without my editor, I would be at a loss to remember where the parentheses and semicolons go.

Control Structures

// I F / E L S E
if ( a > b )
   {
   System.out.println( a );
   }
else
   {
   System.out.println( b );
   }
// I F / E L S E : is x in range low .. high?
if ( low <= x && x <= high )
   {
   System.out.println( "in range" );
   }
else
   {
   System.out.println( "out of range" );
   }
// S W I T C H / C A S E
switch ( n )
   {
   case 1: System.out.println( "one" );
      break;
   case 2: System.out.println( "two" );
      break;
   default: System.out.println( "something else" );
   }// end switch (n)

Loops


view

The variables used in the while (boolean) clause can't be declared inside the while loop. The inside of the loop is considered a separate inner block.

Try/Catch/Throw

public class Test extends StandardTest
   {
   public static void main (String [] args)
      {
      try
         {
         dangerMethod();
         }
      catch ( StrangeException e )
         {
         System.out.println( "oops" + e.getMessage() );
         }
      } // end main

   void dangerMethod() throws StrangeException
   {
      if ( unexpected() ) throw new StrangeException ( "oh oh" );
   } // end dangerMethod
   } // end class Test

To help you remember the syntax of catch, think of it as like a method, that gets passed an exception as a parameter.

Try/Catch/Finally

try
   {
   somethingDangerous();
   }
catch ( IOException e )
   {
   System.out.println( "oh oh" );
   throw new BadDataException();
   }
finally
   {
   file.close(); // always executed
   }

Literals

A literal is an explicit number or string constant used in Java programs. Here is a list of all the variant forms I have found Java to support: Primitive variables include boolean, char, byte, short, int, long, float and double. Strings, arrays and Objects are not considered primitives.
Type Signed? Bits Bytes Lowest Highest Mnemonic
boolean n/a 1 1 false true zero/one
char unsigned Unicode 16 2 '\u0000' [0] aka Character.MIN_VALUE '\uffff' [216-1] aka Character.MAX_VALUE Unicode chars are twice as big as C's.
byte signed 8 1 -128 [-27] aka Byte.MIN_VALUE +127 [27-1]aka Byte.MAX_VALUE Bytes are signed, so half the usual 255 range.
short signed 16 2 -32,768 [-215] aka Short.MIN_VALUE +32,767 [215-1] aka Short.MAX_VALUE 32K
int signed 32 4 -2,147,483,648 [-231] aka Integer.MIN_VALUE +2,147,483,647 [231-1] aka Integer.MAX_VALUE 2 gig
long signed 64 8 -9,223,372,036,854,775,808 [-263] aka Long.MIN_VALUE +9,223,372,036,854,775,807 [263-1] aka Long.MAX_VALUE 9 exabytes, or 9 billion gig
float signed exponent and mantissa 32 4 ±1.40129846432481707e-45 ±3.40282346638528860e+38
with 6 to 7 significant digits of accuracy.
rough float
double signed exponent and mantissa 64 8 ±4.94065645841246544e-324 ±1.79769313486231570e+308
with 14 to 15 significant digits of accuracy.
high precision float

Primitives vs Immutable Wrapper Objects

Contrast that table of primitives, with this table of basic Java types:
Mutable Primitives Immutable Objects
boolean Boolean
ordinary signed byte Byte
unsigned byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
char[] String

Precedence

Keywords

Java Keywords
abstract do import public throws
boolean double instanceof return transient
break else int short try
byte extends interface static void
case final long strictfp volatile
catch finally native super while
char float new switch  
class for package synchronized  
continue if private this  
default implements protected throw  
Reserved keywords (not currently in use)
const goto      
Reserved Literals
null true false    

JavaDoc

/** FormattedTextField.java
 * @author Roedy Green
 * @version 1.34 1998 January 18
 * @deprecated Noreplacement
 * @deprecated Replaced by otherMethod(int)
 * @see otherMethod
 * @see #otherMethod
 * @see java.awt.Component#repaint
 * @see <a href="http://mindprod.com/gloss.html">Java & Internet Glossary</a>
 * @see "design patterns by Gamma et. al"
 * @param x <b>pixels</b> right of the origin.
 * @return number of oranges.
 * @exception java.beans.PropertyVetoException when mask is invalid
 * @since JDK1.1
 */


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 39036.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/jcheat.html J:\mindprod\jgloss\jcheat.html