references: Just Java, van der
Linden
'Java Certification Exam Guide',
Barry Boone
'Java 1.1 The Complete Reference',
Schildt &Naughton
latest revision April 17 / 2001
The characters we use to communicate, whether in a spoken or computer
language, belong
to character sets which are defined
as world standards. Java was designed to use Unicode.
World Standards for Character Sets
ASCII, American
Standard Code for Information Interchange
uses
7 of 8 bits of a byte
to describe each of it's 128 characters.
ASCII is the standard character set computer devices
like keyboards and screens read and write. It is also the standard
character set for the classic
Internet protocols.
ISO 8859-1 is
ASCII extended to use all 8 bits of a byte. It's 256 characters included
various
non-English characters. ASCII makes up the first
128 characters of ISO 8859-1.
Unicode is a 16-bit character set
able to support 65,536 characters. Currently over 30,000
are in use.(~21,000 support Han, the ideograms the Chinese, Japanese
and Korean languages.)
ISO 8859-1 make up the first 256 characters of
the Unicode character set. Java uses Unicode
by default. It is currently understood that even Unicode will not be
able to accomodate a truly
global character set. In the future, Unicode may be revised to access
other 'planes' of ISO's
BMP 10646.
UCS-2 Unicode
is synonymous with the first part of an even larger character set map deviced
by the ISO/IEC organization and referred to as
the BMP 10646 (Basic
Multilingual Plane) .
The characters described in the first plane,
(what can be represented in 2 bytes) is called
UCS-2. ( UCS is short for Universal
Character Set ). This is synonomous with
Unicode.
UCS-4 is the whole BMP 10646 described using 4 bytes.
UTF, UCS Transformation
Formats, short for Universal Character
Set Tranformation
Formats) or UTF for short, is an
extension technique that permits accessing characters beyond
the ~64K characters defined in Unicode. There are different competing
versions of UTF formats,
including UTF-8 is a variable
byte length format which uses 1 to 6 bytes to describe it's
characters. UTF-16 uses 2 to 4
bytes.
Common Data Formats
ASCII | American Standard Code for
Information Interchange |
7-bits, [one byte] | 128 characters mostly humanly readable |
ISO 8859-1 | 256 ISO character code | 8-bits, [one byte] | includes many non-English characters |
Unicode | synonymous with UCS-2 | 16 bits, [two bytes] | most of the world's characters |
UCS-2 | Universal Character Set
two byte encoding |
16 bits, [two bytes] | 1st plane of ISO/IEC 10646
in two bytes (0 to 64K) |
USC-4 | Universal Character Set
four byte encoding |
32 bits, [four bytes] | Full ISO/IEC implementation
in 4 bytes |
UTF-8 * | UCS Transformation Format
versatile but complex |
[one to six bytes] | if bit 1 is 0,-->1 byte ASCII
if 1st bits are 110,-->2 bytes if 1st bits are1110,->3 bytes etc. |
UTF-16 | extended variant of UCS-2 | [2 to 4 bytes] | |
binary | data transfer in numeric form | [1 to 8 bytes] | for java chars, binary is Unicode |
objects | streaming java objects | [variable length] | the serialization process |
Java uses Unicode (which uses 16 bits) to represent each character. If the top nine bits of a Unicode byte are all zeros then the character represented is from the ASCII character set. Java's char data type uses Unicode encoding. The String class encapsulates a collection of char type values. |
Rule Identifiers
cannot be keywords. They may start with a letter, underscore or dollar
sign. Subsequent positions can be digits.
(Note
a letter can be any Unicode letter)
1) // to the end of line comment
2) / * start and end type comment */
The above two comment styles have no effect on code execution and will
not appear anywhere
except in the source text. They provide a way of supplying additional
information to a person
reading the source code.
3) /** javadoc comment */
The javacode document style works like the above comments with the additional
facility of
adding comments to a HTML document produced by invoking the javadoc
tool. The
documentation available for the JDK API is generated using the javadoc.
Javadoc
defines a
number of tags to display such things as the author or version of a
source code work. This
tool also permits the insertion of other HTML tags to enhance code
documentation.
In taking a first look at keywords it's a bit confusing. Learning the
keywords inevitably acts as a
tour of the core of java language and presents a dilemna. To learn
Java you have to understand
the keywords however to understand the keywords you have to know Java.
(It's not that extreme
but you get the idea.) The key is to learn the keywords and go on faith
that they will become more
meaningful to you as you proceed.
Perhaps a good approach would be to introduce a few words at a time,
but it's hard to keep the
others from showing up. Besides it is too tempting just to give them
all in a table like the one below.
The Java Keywords in a Table //
a key observation, all keywords are 100% lowercase
abstract | const | finally | int | public | throw | boolean |
continue | float | interface | return | throws | break | default |
for | long | short | transient | byte | do | goto |
native | static | try | case | double | if | new |
super | void | catch | else | implements | package | switch |
volatile | char | extends | import | private | synchronized | while |
class | final | instanceof | protected | this | . | . |
Words reserved for values | true | false | null |
Most authors who first wrote books on java spent a chapter briefly describing
each of the
keywords. The keywords are typically categorized by function. We use
Van der Lindens
categorizations below.
For Primitive Types
These keywords represent characters, (in the case
of char), true and false values (in the case
of boolean) and integers and floating point
numbers of different sizes. They are covered in detail
at the end of this note so we for now we just
list them.
boolean
byte
short
char
int
long
float
double
In expressions
new | The new operator creates a new instance of a class
(or an array). It preceeds the class
constructor in an expression and creates a unique copy of the class in memory. (The class acts as a template.) example SomeClass sc = new SomeClass( ); |
this | this is a special reference used inside
methods or constructors to refer to the current object.
Example class
ThisT{
|
super | super is a 'magic' reference like this
except
it provides a means of referencing the
immediate superclass of an object. Example class
ThisT2 extends ThisT{
|
In Statements
Selection | |
if | if is used in the form if ( boolean ) to execute an action if the boolean value is true |
else | else provides an alternate course of action in the case an if statement proves false |
switch | switch is a construct used to allow several
different courses of action to be taken
for a given condition or case The switch part of the construct tests a variable. |
case | case is used to execute a code block for a given value tested in switch |
break | break is used in a switch construct to prevent
other cases being inadvertently
executed. It is also used to break out of for loops. |
default | in a switch, default provides a course of action in the event that no cases match |
Iteration | |
for | for is used to begin a loop in the form for(int i=0;i<10;i++) { // do something } |
continue | continue provides a way to break out of one of
the iterations of loop (and
optionally, have control resume at a different block of code) |
do | do is used with while to create a loop that will always execute a least once. |
while | Example
class doT{
public static void main(String[] args){ boolean flag=false; do{int x=0; x=x+1; System.out.println(x); }while (flag); //x will always at least be set to 1 } } |
return | return is used inside
methods to return a value from the method. It is also used by
itself to exit a method (before completion) based on some condition. Example 1
class Do{
Example 2
/* a method that appends a name to a phrase */
|
Exceptions | try, catch, & finally work together to handle exceptions in java |
try | try precedes a block
of code containing a method which may throw an
exception. Every try block must have a catch or finally block following it. |
catch | the catch block handles the exception that
may be thrown and provides
an appropriate response. |
finally | finally tags an optional block which is
executed whether or not an
exception is thrown. Example try {Thread.sleep(3000);} //sleep for 3 seconds catch(InterruptedException ie){// not doing anything } finally{System.out.println("Had to get up anyway"); . |
Threads | |
synchronized | synchronized is used in a multithreaded
environment to ensure data that is
accessible to several concurrent threads, is acted upon one thread at a time. |
Modifiers | |
private | private is an access modifier that restricts to the access to within a class |
protected | protected is an access
modifiier that restricts access to within a package
and to subclasses inside & outside the package |
no modifier | No access modifier restricts access to within the package |
public | The public access modifier provides
unrestricted access allowing
class members to be accessed from anywhere, in or outside the package |
static | The static modifier fixes the definition
of a variable, a block of code or
method with the class rather than the instantiations of that class. |
abstract | A class may be marked abstract if it has zero or
more abstract methods. An
abstract class cannot be instantiated. If an abstract class containing abstract methods is subclassed, the subclass must provide an implementation for any of the parents abstract methods or it too must be marked abstract. An abstract method has no body (a scope defined by two curly braces) |
final | A class marked final cannot be subclassed A variable
marked final cannot be
changed once it is assigned a value. |
Others | |
class | A class is a structure whose scope binds
data (in the form of variables) and
actions (in the form of methods) to form a template that can be used to create new unique copies (objects or instances) of the class. Classes can be subclassed, passing on their collective characteristics and behaviour to their offspring. // When you hear 'state', 'attributes' or 'properties' think variables. When you hear 'operations', 'functions', 'actions' or 'behaviour' think methods |
instanceof | instanceof returns a boolean when it tests
if a class is an instance of another
class or a subclass of another class. Example if (class_ref instanceof Object) will return true in the if statement as all classes descend from Object class |
throws | throws is used with a method signature to
indicate a method has the potential
to throw a particular exception. Example public void sleep(int milliseconds) throws InterruptedException |
native | native is used to describe non-java code that is native to the underlying platform |
transient | data marked transient
will
not be serialized. This is used to protect sensitive data
from being transmitted over the network |
volatile | this keyword when applied to a variable signals
the compiler not to do certain
optimizations with it in a multithreaded environment as the data could be changed unpredictably by another thread |
void | A method must have a return type. void is
used by a method to signify it doesn't
return any value, or in other words, returns nothing. |
Outside the Class | |
extends | Java's object-oriented design incorporates an inheritance
mechanism.
A class can extend or subclass another class using the extends keyword, thereby inheriting the parent's non-private characteristics and behaviour. |
interface | Java doesn't have multiple inheritance. It allows
adding behaviour via
the interface construct. The interface is a reference type (like the class and the array) It's only members are abstract methods and/or constants. |
implements | implements is the keyword used by a class
to declare it implements an
interface. i.e. class Responder implements ActionListener {// code } |
package | The package keyword is used to create
a new, directory level container
or library in the java language. Packages are what you import to provide the libraries of classes you need for your programs. |
import | import is the keyword
you use to bring a package into your program so
you can access it's classes. |
Reserved Keywords | // strictfp is no longer reserved |
const | const is reserved. In C it is used to make
a variable of data type
unalterable. In java by the keyword final does this job. |
goto | goto became a symbol of poor programming.
The storey goes it
was reserved to ensure it would not be reincarnated in Java. |
widefp | widefp has been dropped // Sept. 2000 |
strictfp | strictfp has been adopted as of JDK1.2 // Sept. 2000 |
Reserved Values | // these words are categorized as reserved values rather than keywords |
true | boolean value // in Java, boolean values have no numerical counterparts |
false | boolean value |
null | this is the value a reference takes when it doesn't reference (or point to) something |
Primitive Types
Type | Use |
boolean | represents truth values, true and false |
int, long, byte, short | Integer arthmetic , { 1 , 2 , 3 , . . .} |
double, float | Real number arithmetic |
char | represents character data |
In Java the size of data types is strictly specified and is the same
regardless of platform. This is
what is referred to by strong typing.
Type | Size | Range | Literals |
boolean | 8-bits (one byte) | false & true | true & false |
byte | 8-bit, signed,
two's compliment quantity*. |
-128 to 127
[-2 7 to (2 7 - 1)] |
There is no 'real' byte literal,
only int literals that fall into byte's range |
short | 16-bit signed,
two's complement number |
-32,768 to 32,767
[-215 to (215 -1 )] ~ +/- 32,000 |
" " |
int | 32-bit, signed,
two's compliment number |
~ -2 billion to 2 billion
[-231 to (231 - 1)] ~ +/- 2.1 billion |
|
long | 64-bit, signed,
two's complement number |
~ -9 billion,billion to
+ 9billion,billion [-263 to (263 - 1)] +/- 9.2 billion,billion |
must be suffixed with an L
(use upper case for clarity) |
float | 32-bit floating point number
as per IEEE *standard #754 |
~ -3.4E38 to 3.4E38
340 billion, billion, billion, billion |
must be suffixed with an F
otherwise defaults to double |
double | 64-bit floating point number
as per IEEE *standard #754 |
~ -1.7E308 to 1.7E308** | |
char | 16 bit | 0 to 65,535 [ 0 to 216 ] | character literals use single
quotes char c = ' Q ' ; |
Re: boolean
Rule:
Boolean
cannot be cast or converted to any other type. (also,
boolean is not
integer-based so can't be added, incremented or decremented)
Re: byte
The byte type is useful for handling legacy data (ASCII)
and to economically store large
quanitities of such values.
Re: floating point types
* IEEE -the Institute of Electrical an Electronic Engineers
On today's cpu's using float is not faster than double.
However there is still a savings to be
had in storage if a large number of floats vs. doubles
need to be stored.
**The number of cubic centimeters in know universe is conjectured to be E85
NaN, not a number,
-error report on a situation that is not well defined i.e dividing zero
by zero 0/0 Non numerical situations arising from operations
on floats and double are
covered by values defined in Float and Double classes.
[i.e. Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY
&
Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY]
Re: char type
char never takes a negative value and is designed
to hold character data as described by
a single character like 'c'.('c' is a char literal).
-an escape sequence,
i.e. ' \ n ' for newline or ' \ \ ' for backslash
-an octal escape sequence i.e. ' \ nnn '
where nnn is one to three octal digits
-a Unicode escape sequence i.e. ' \uxxxx
' where xxxx is four hexadecimal digits
-i.e. '\u0041' is the character A
String class Preview
String class is included here as we often need a string of characters
to name or represent
something. In Java when you need a string of characters in your programs,
you use an
instance of the String class. (We look at String class a little closer
later in the course.) String
objects hold a series of adjacent characters in a way similar to array.
However, where with
an array you would reference an element of the array to get a character,
with String you use
one of the class methods to manipulate the string.
String literals
When zero or more characters are enclosed in double quotes you have a string literal..
Example" Here's a string literal "
String literals are normally used in some context. For instance as an argument to a method.
Example setText(" string literal as a method argument " );
or in an assignment.
String string = " A String literal being assigned to the variable string. ";
Because String is a class, an object of String class can be created
using a special method
called a constructor.
Example String s = new String("A string" );
Strings are said to be immutable.
Practically speaking this term really only has 'behind the
scenes' significance. It means that once a string object has been created,
it cannot be changed.
But when a method is called on a String object it appears to be changed!
What actually occurs
is a new String instance is created containing the user's modifications.
The new String object
replaces the old ones place in the original reference.The original
String object, now without a
reference, is discarded and will eventually be picked up and destroyed
by the garbage collector.
(The garbage collector is a background process that takes care of freeing
up memory that is
occupied but no longer being used by the program.)
There is one case of overloading
in Java with the addition operator. (Overloading in this context
is when a symbol is able to do more than one kind of operation in a
language.) The '+' symbol is
overloaded to do concatenation. If one of the operands to +
is a String object, the other type,
whether primitive or object will have toString( ) called on it and
the two objects will concatenated
example System.out.println(" x is " + x + "y is " + y );
" " + i, where i is a primitive, causes the primitive value represented
by i to be written in a string
form.
i.e. 010101 binary 21 --> invert --> 101010
--> add 1 -->101011 -21 repr. in 2's-compl.
101011
" -21 --> invert --> 010100 --> add 1 --> 010101
back to positive 21