Java - Quick Reference - Part - I
Java - Quick Reference - Part - I
Same class
Same pack. subclass
Same pack. non-subclass
Diff. pack. subclass
Diff. pack. non-subclass
Y
N
N
N
N
No
Modifier
Y
Y
Y
N
N
ProtectedPublic
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Documentation Comments
Comment Insertion
Packages
Public classes and interfaces
Public and protected methods
Public and protected fields
Class Comments
/** A Card object represents a playing card */
public class Card
{
. . .
}
Method Comments
@param variable description
@return description
@throws class description
/** Raises the salary of an employee.
@param byPercent the percentage by which to raise the
salary (e.g. 10 = 10%)
@return the amount of the raise
*/
public double raiseSal(double byPercent)
{
double raise=salary*byPercent/100;
salary += raise;
return raise;
}
Field Comments
/**
* The "Hearts" card suit
*/
public static final int HEARTS = 1;
General Comments
@author name
@since text
@see reference
Comment Extraction
@version text
@deprecated text
Java Buzzwords
Simple
Object-oriented
Architecture-neutral
Distributed
Portable
Multithreaded
High performance
Methods
Secure
Robust
Interpreted
Dynamic
Data Types
Integers
Name
long
Width
64
int
short
byte
32
16
8
Range
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
2,147,483,648 to 2,147,483,647
32,768 to 32,767
128 to 127
Approximate Range
4.9e324 to 1.8e+308
1.4e045 to 3.4e+038
char
boolean
16 bit
- true/false
Declaring a Variable
type identifier [=value][,identifier [=value]...] ;
Access Control Modifier
public
private
protected
Arrays
One-Dimensional Arrays
type var-name[ ];
array-var = new type[size];
default
Multidimensional Arrays
int twoD[][] = new int[4][5];
Alternative Array Declaration Syntax
type[ ] var-name;
int al[] = new int[3];
int[] a2 = new int[3];
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];
int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
Strings
Declaring Objects
class-var = new classname( );
String API
Methods
General Form of a Method
type name(parameter-list) {
// body of method
}
Anonymous Array
smallPrimes = new int[] { 17,19,23,29,31,37 };
Packages
Class Importation
java.util.Date today = new java.util.Date();
(or)
import java.util.*;
Date today = new Date();
import java.util.*;
import java.sql.*;
Date today;
// ERROR--java.util.Date or java.sql.Date?
import java.util.*;
import java.sql.*;
import java.util.Date;
java.util.Date deadline = new java.util.Date();
java.sql.Date today = new java.sql.Date(...);
Static Imports
import static java.lang.System.*;
out.println("Goodbye, World!");
// i.e., System.out
exit(0); // i.e., System.exit