|
Language Fundamentals
Class
declaration and java source file.
|
Only "one" top-level public class is allowed per
java source file.
|
|
The name of the java source file and the name of
the top-level public class MUST be same.
|
|
If there is no (not a single one) top-level
public class present in a java source file, then
the java source file's name can be
anything.
After compilation of such file, class files for
all top-level classes are produced.
example
|
Package,
import statements and Class declaration
|
Package statement, import statements and class
definition MUST appear in the order given.
example
|
|
Package declaration is optional. If package
declaration is absent, the classes are added in
the default package after compilation.
|
[More in ebook]
Keywords and
Identifiers
|
All java keywords are always in a lower case.
|
|
Some hard to remember keywords are const goto strictfp and
volatile.
|
|
Identifiers must start with either letter, $ or _
(underscore) and can have letter, $, _ or digits
in it.
|
|
An identifier should not be a java keyword or a
java reserved word.
Complete list of
keywords.
|
[More in ebook]
Primitive data
types
|
Data Type
|
Size(in bits)
|
value-range
|
|
byte
|
8
|
-27 to (27-1)
or
-128 to +127
|
|
char
|
16
|
0 to (216-1)
or
\u0000 to \uFFFF
|
|
short
|
16
|
-215 to (215-1)
|
|
int
|
32
|
-231 to (231-1)
|
|
long
|
64
|
-263 to (263-1)
|
|
|
Data Type
|
Size(in bits)
|
value-range
|
|
boolean
|
1
|
true, false
|
|
Data Type
|
Size(in bits)
|
Value - range
|
|
float
|
32
|
Float.MIN_VALUE to Float.MAX_VALUE
|
|
double
|
64
|
Double.MIN_VALUE to Double.MAX_VALUE
|
|
[More in ebook]
Literals
|
Boolean
Literals
There are 2 boolean literals true and false. Please note that they
are case sensitive.
boolean b = true; // valid
boolean b = True; // invalid
|
|
|
Character
Literals
Character literals are defined as a character in
single quotes.
char ch = 'w';
Character literals can also be represented using
hexadecimal values.
char ch = '\u4468';
where 4468 is hexadecimal value for some character.
Character literals can also be some special
characters
char ch = '\r';
Other special characters are '\n' '\\'.
|
|
Integral
Literals
By default, integral literals are of data type
int.
int a = 5; // valid.
Integral literals normally represent a decimal
value.
int aNumber = 28;
Integral literals can also be representing an
octal value.
int aNumber = 034;
Please note that an octal literal is identified when there
is 0 (zero) at the start of the literal.
Octal numbers are
represented with 0 to 7 digits similar to decimal system, which
uses 0-9 digits to represent numbers. Therefore, there is no
single digit above 7 in octal system.
int aNumber = 034; // valid.
int aNumber = 038; // invalid.
Integral literals can also represent the
hexadecimal value.
int aNumber = 0x1a;
Please note that hex literals are case
insensitive. Therefore
int aNumber = 0x1a; // valid.
int aNumber = 0x1A; // valid.
int aNumber = 0X1a; // valid.
int aNumber = 0X1A; // valid.
|
|
Floating
Point Literals
By default, they are of type double.
double d = 12.34; //valid
float f = 12.34; //invalid
float f = 12.34f; //valid
double d1 = 1.23 E+20; //valid
|
|
String
Literals
String literals are represented as a sequence of
characters enclosed in double quotes.
String s = "SampleString"; //valid
String s = 'SampleString'; //invalid
|
[More in ebook]
Arrays
|
Arrays
Declaration, Construction and
initialization.
Arrays are somewhat special objects in java as
array construction and declaration is a little
bit different.
An array is a fixed-sized ordered collection of
homogeneous data elements.
Following example shows array declaration at
compile time and array construction at runtime.
int[] ints; // array declaration
ints = new ints[25]; // array construction at runtime.
Now let us see how an array is declared,
constructed and initialized at the same time.
int[] ints = {1,2,3,4,5}; // array declaration, construction and initialization at the same time.
|
|
Array of
primitive data types
An array of primitive data type created using the
new keyword is
initialized. Each array element is initialized to
its default value.
For example,
char[] arrayOfChars = new char[10];
An array of 10 chars will be created with each element
initialized to '\u0000'. The default value for
char is
'\u0000'. For default values of other primitive
types, refer to the table.
|
|
Array of
object references
An array of object references created using the
new keyword is
also initialized. Each array element is
initialized to its default value, i.e.
null.
String[] names = new String[10];
An array of 10 string references will be created with each
element initialized to null.
|
|
Array index
Please note that array index starts at 0. So
array of 10 elements has element at 0th index to
9th index.
String[] names = new String[10];
Therefore, names[0] to names[9] will have value null.
names[10] does not exist and hence it is undefined.
Please note that length is a property of array
(and not a method).
int[] ints = {1,2,3,4,5};
System.out.println("Length of array ints is " + ints.length);
As java.lang.Object is the superclass of an
array, arrays understand all method calls of
java.lang.Object class.
|
[More in ebook]
Argument
passing during method calls.
Please note that always a copy of argument value is
passed to calling method.
|
Arguments of primitive data types
When arguments of primitive data types are
passed, first, a copy is made and then it is
passed. Therefore, the original copy (in caller
method) remains unaffected by the called method.
|
|
Object reference as an argument.
However, when argument of some object type is
passed, actually a copy of object reference is
passed. Therefore, even if it is a copy, it (copy
of object reference) still points to the same
object. Therefore calling method may change the
object whose reference is passed as an
argument.
|
|
main()
method.
|
[More in ebook]
|