chapter 1: Language Fundamentals
IMPORTANT:PLEASE REPORT ANY bugs at anil_kuchana@yahoo.com
|
Objective illustrated:
|
| Identify correctly constructed source
files, package declarations, import statements, class declarations,
interface declarations and implementations, variable declarations
and identifiers |
Comment Styles:
- Three styles of comment notation are
used in java. They are
| 1] |
Any text between
'//' and the end of line is a comment. |
| 2] |
Text starting
with '/*' and terminating with '*/' make up multiple line comments. |
| 3] |
Special form starting
with '/**' and terminating with '*/' is used to create comments
that can be processed by the javadoc program to produce HTML
formatted documentation.
|
About
javadoc comments:
- A javadoc comment will only be recognized
if it is placed immediately before a class, interface, method or
field declaration with which it will become associated.
- javadoc will process comment documentation
for only public and protected members.
- comments for private are ignored.
- When you browse the javadoc information
for a package the list of classes are arranged alphabetically.
|
The
fields, constructors and methods have color coding
|
| purple dot |
instance variable |
| blue dot |
static field (class variable) |
| yellow dot |
constructor |
| red dot |
instance method (non static method) |
| green dot |
class method (static method) |
Package
declaration & order:
-
java allows us to group
classes in a collection called packages.
-
packages help us organize
classes and maintain uniqueness.
-
If no explicit package
is defined, java adds the classes in default package.
The sequence of appearance of package declarations
and class definations are :
| 1] |
package name
comes first
|
| 2] |
import statements
if any
|
| 3] |
public/non
public classes
|
| 4] |
main method
|
TABLE SHOWING
THE SEQUENCE OF PACKAGE,IMPORT CLASS DECLARATIONS
|
Objective illustrated:
|
| State the correspondence between
index values in the argument array passed to a main method
and command line arguments. |
main()
method and it's signature:
-
java interpreter
starts by calling the public class's main() method.
-
main() method takes
an array of String as an argument
-
main() method must
be declared public, static and not return a value (void).
Signature of the
main() method can be any of these:
|
public static void
main(String args[] )
|
|
public static void
main (String [] args )
|
|
static public void
main (String [] args)
|
note: args can instead be any valid identifier
like "anything"
- args is a String array. This array
helps the runtime system pass information to the application from
the command line.
- Argument index of the String array args
starts from 0. Therefore first argument is referenced by
args(0).
|
Objective illustrated:
|
| Identify all Java programming language
keywords and correctly constructed identifiers. |
Identifying the Java keywords:
| abstract |
do |
import |
public |
transient |
| boolean |
double |
instanceof |
return |
try |
| break |
else |
int |
short |
void |
| byte |
extends |
interface |
static |
volatile |
| case |
final |
long |
super |
while |
| catch |
finally |
native |
switch |
|
| char |
float |
new |
synchronized |
|
| class |
for |
package |
this |
|
| continue |
if |
private |
throw |
|
| default |
implements |
protected |
throws |
|
- const, goto are reserved keywords not
in use.
- null, true, false are reserved literals
in java.
Legal identifiers
-
Identifiers are used to
denote classes, methods and variables.
-
Identifiers may contain
only letters, dollar signs, numbers or underscore( _ ).
-
The first character cannot
be a number.
-
Identifier cannot be the
same as any keyword or the boolean or null literals( true, false,
null ).
-
Java is case sensitive,
hence "anil" and "ANIL" are different.
|
Objective illustrated:
|
| State the effect of using a variable
or array element of any kind when no explicit assignment has been
made to it |
| State the range of all the primitive
data types and declare literal values for for String and all primitive
types using all permitted formats, bases, and representations. |
Java data
types:(literals):
- Java literals are of integer, floating
point, character, boolean types.
- Integer literals:
These are comprised of int, long, byte and short.
- int is the default type.
- to specify it as long add suffix L(or
l)
- Floating point literals:
float and double are floating point literals.
- double in the default type.
- A floating point can be explicitly default
by suffixing F (or f).
- floating point can also be specified
in scientific notation (as an example) 5E-1 or 5e-1.
- Boolean literals:
true or false are the reserved literals under Boolean.
- false is the default type
- Character literals:
These are enclosed in single quotes( ' ).
- These are also defined by quoting the
unicode values.
All the primitives are summarized
in the table below.
|
Type
|
Size
|
Range
|
| byte |
8 bits |
-128 to 127 |
| short |
16 bits |
-32768 to 32767 |
| char |
16 bits |
\u0000 to \uFFFF |
| int |
32 bits |
-2^31 to 2^31-1
|
| long |
64 bits |
-9223372036854775808L
to +9223372036854775807L |
| float |
32 bits |
±1.40129846432481707e-45,
to ±3.40282346638528860e+38 |
| double |
64 bits |
±4.94065645841246544e-324,
to ±1.79769313486231570e+308 |
| boolean |
16 bits |
true or false |
Note: Tricky questions seem to appear testing
the range of primitives
-
java.lang package provides
standard library classes that are closely related to primitives.
-
These classes are Byte,
Short, Character, Integer, Long, Float, Double and Boolean. These
are known as Wrapper classes.
-
Except for int, Integer
and char, Character these classes differ from the primitives only
in the initial upper case letter.
-
These classes provide various
methods.
-
These classes serve as
utility functions, constants and object encapsulation of primitive
values.
-
All wrapper objects are
immutable. Once created the contained value can't be changed.
-
Wrapper classes are final
and can't be subclassed
-
These override equals()
method.
-
In addition to decimal
number system, integer literals can also be specified in octal(base
8) and hexadecimal(base 16) number systems.
-
Octal numbers are prefixed
with '0' (only 0-7 digits are used).
-
Hexa decimal numbers are
prefixed with '0x' or '0X'. (only 0-9 and a-f are used).
|
examples of conversions
will be added shortly
|
Default values:
|
Primitive
|
Default
|
|
byte
|
0
|
|
short
|
0
|
|
int
|
0
|
|
long
|
0L
|
|
float
|
0.0f
|
|
double
|
0.0d
|
|
boolean
|
false
|
|
char
|
'\u0000'
|
-
Local variables are those
that are declared in methods and in blocks. These are known as Automatic
variabes.
-
These are automatically
destroyed when they go out of scope.
-
The automatic initialization
does not apply to the automatic variables. These should be explicitly
initialized before they are used.
-
Certain escape sequences
define special character values.
These escape sequences
can be single quoted to define character literals.
eg: '\t' and '\u0009' are
equivalent.
|
Escape Sequence
|
Unicode value
|
Character
|
|
\b
|
\u0008
|
Backspace
|
|
\t
|
\u0009
|
Horizontal Tabulation
|
|
\n
|
\u000a
|
Line feed
|
|
\f
|
\u000c
|
Form feed
|
|
\r
|
\u000d
|
Carriage return
|
|
\'
|
\u0027
|
Apostrophe
|
|
\"
|
\u0022
|
Quotation mark
|
|
\\
|
\u0055
|
Backslash
|
|
\u####
|
\u####
|
Hex equivalent
|
-
java doesn't have a built
in string type. Instead standard java library contains a predefined
class called String .
String literals are expressed
by a series of characters inside double quotes
Ex. "this is a String
literal"
Escape Sequences as well
as unicode values can appear in String literals.
eg: "\" String
literals are double quoted \t\"".
Strings are constant their
values cannot be changed after they are created. (i.e, String objects
are immutable).
-
Classes, Interfaces and
Arrays are all reference variables types.
-
Reference variables are
declared in statements that define the type of object to which particular
variable can refer.
eg: String s;
In above example you have
created only a reference not an object.
Every object is created
with the 'new' keyword.
eg: String s=new String("sample");
No limitations exists on
the number of variables that can refer to the same object.
The default value (i.e.,
value without explicit declaration) is the special 'null' literal.
-
An array is a data structure
which defines an ordered collection of a fixed number of homogeneous
data elements.
eg: int totals[] = new
int[20];
-
in java arrays are objects
-
When array is created its
size is fixed and cannot be changed for the lifetime of that array
object. (like in other languages)
-
Array indices starts with
zero.
-
Java handles multidimentional
arrays as arrays of one dimensional arrays.
-
All arrays of primitives,
as created by a new statement are initialized to a default value
of the type of primitive and all references are initialized to special
null value.
The legal syntax are:
|
int anarray[];
|
|
int[] anarray;
|
|
int [] anarray;
|
|
int[] anarray[];
|
|
int[][] anarray;
|
|
int anarray[][];
|
|
int []anarray[];
|
take
an exam on this topic here
|
Please
submit your ideas on this topic here
|
|
|
|