Free SCJP ebook!

Free SCJP Mock exams!( Registration required)
The free SCJP ebook
Witscale Test Center

Previous Page Next Page Contents Index

Valid XHTML 1.0!

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


Primitive data type boolean can take boolean literals, true or false, as values.
Primitive data types can also be classified as signed integrals and unsigned integrals.
byte, short, int, long are signed integrals.
char represents unsigned integrals.
Signed integrals represent signed numbers (positive as well as negative). They are represented in 2's complement form.( example)
Default Values
Each primitive data type has a default value specified. Variable of primitive data type may be initialized.
Only class member variables are automatically initialized. Method variables need explicit initialization.

Primitive Data type Default Value
byte 0
short 0
int 0
long 0L or 0l
float 0.0f
double 0.0d
boolean false
char '\u0000'


Local Variables

Local variables (also known as automatic variables) are declared in methods and in code blocks. The automatic variables are not automatically initialized.
A java programmer should explicitly initialize them before first use. These are automatically destroyed when they go out of scope.



[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.
  • main() is the entry point of any java application.
  • Following are some valid main() method declarations.

     public static void main(String[] args) {}
     static public void main(String[] args) {}
     public static void main(String args[]) {}
    

  • main() method takes String array as argument. The java runtime system can pass command line parameters to the application through this array.
  • The first command line parameter is passed as the first argument and is referenced by args[0].
    For example,
     java MyCalculator 12 50 add
    
    args[0] will be 12, args[1] will be 50 and so on.



[More in ebook]

Related SCJP Objective

Section 4 : Language Fundamentals
  1. Identify correctly constructed package declarations, import statements, class declarations (of all forms including inner classes) interface declarations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers.
  2. Identify classes that correctly implement an interface where that interface is either java.lang.Runnable or a fully specified interface in the question.
  3. State the correspondence between index values in the argument array passed to a main method and command line arguments.
  4. Identify all Java programming language keywords. Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants.
  5. State the effect of using a variable or array element of any kind when no explicit assignment has been made to it.
  6. State the range of all primitive formats, data types and declare literal values for String and all primitive types using all permitted formats bases and representations.


Previous Page Next Page Contents Index Top of this page

Valid CSS!