JavaEnterprise JavaSJCP Exam Preparation: Language Fundamentals, Part Two

SJCP Exam Preparation: Language Fundamentals, Part Two

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Objectives

You are going to be asked to answer exam questions in exam regarding the following subjects.
So be sure that you understand everything. After reading each article,
I suggest you to write and compile small Java programs.
I believe that practice is always better than theory. At the exam, you will be
asked about the following topics [2]. 

  • Identifiers and Keywords

  • Primitive Types
  • Literals
  • Arrays
  • Variable Types and Initialization

If you have any questions after studying this document, do not hesitate to
drop me an e-mail, and I will try to clarify
things as best as I can.

Koray Guclu

You can read more information about SCJP certification from the

SJCP Web page
[2], and you can download the .pdf file called “Sun Certified Programmer
for Java 2 Platform

Success Guide
” [3].

Identifiers and Keywords

Identifiers are used by programmers to give meaningful names used by the Java language
to uniquely identify classes, methods, and variables.
An identifier must begin with a letter, an underscore ‘_’, or a dollar sign ‘$’.
Subsequent characters can be any of these plus digits between 0 and 9. An identifier
cannot begin with a digit; doing so will cause a compile-time exception.
Identifiers are case sensitive and cannot have the same name as keywords or reserved words.

















  • myInformation


  • // legal

  • $myVariable


  • // legal

  • _anotherIdentifier


  • // legal

  • 3identifier


  • // illegal

  • while


  • // illegal. This is a keyword.

  • While


  • // legal (Java is case sensitive)

  • book


  • // legal

  • u0062ook


  • // legal.

  • %percent


  • // illegal.


    A Unicode character, which can appear anywhere, can be used in a Java source file.
    Since u0062 is a valid Unicode character, the given example u0062ook is a valid Java
    identifier. The Unicode letter u0062 substitutes for the letter ‘b’.




    Keywords and reserved words are predefined names used by the Java language, which
    we cannot use as identifiers. The following table shows the list of Java keywords
    and reserved words.




    Java Keywords and Reserved Words

    abstract

    boolean

    break

    byte

    case

    catch

    char

    class

    const 1

    continue

    default

    do

    double

    else

    extends

    final

    finally

    float

    for

    goto 1

    if

    implements

    import

    instanceof

    int

    interface

    long

    native

    new

    package

    private

    protected

    public

    return

    short

    static

    strictfp

    super

    switch

    synchronized

    this

    throw

    throws

    transient

    try

    void

    volatile

    while

    true 2

    false 2

    null 2


    1 ‘goto’ and ‘cost’ are reserved words but they do not have any use
    in Java. Since ‘goto’ and ‘cost’ are reserved words, they cannot be used as identifiers.

    2 ‘true, ‘false’, ‘null’ might appear to be keywords but they are technically
    literals [1][’3.10.3]. ‘true, ‘false’ are Boolean literals and ‘null’ is the null literal
    (they cannot be used as identifiers).

    Primitive Types and their Wrapper Classes


    Java has eight primitive types; each primitive type has a matching wrapper class.
    A variable declared as type primitive cannot store an object reference.
    The primitive types are used to store primitive values.
    All numeric types are signed, except ‘boolean’ and ‘char’. The ‘boolean’ literal has
    exactly two values: ‘true’, and ‘false’.
    There are two kinds of numeric types: the integral and the floating-point types.
    The integral types are:


    • byte

    • short

    • int

    • long

    • char

    The floating-point types, complying with IEEE 754 specification, are:


    • float

    • double

    Float and Double are wrapper classes of the float and double primitive types.
    Three constant values -NaN, POSITIVE_INFINITY, and NEGATIVE_INFINITY- have been defined in each wrapper class

    in order to define results of undefined mathematical operations (non-numerical values).
    The following table shows these values


    • Float.NaN

    • Float.POSITIVE_INFINITY

    • Float.NEGATIVE_INFINITY

    • Double.NaN

    • Double.POSITIVE_INFINITY

    • Double.NEGATIVE_INFINITY

    The primitive types are passed to methods by value, but Object types are passed by reference.
    Below you will find each of these primitive types with detailed information.

    I suggest you studying the following table. You may be asked to answer questions
    coming out of the following table.
    For instance, a question might be about the size or the range of a primitive type.

    Primitive Types and Their Properties

    Primitive Type Size
    (in bits)
    Default
    Value
    Wrapper
    class
    Minimum Value Maximum Value
    boolean N/A false Boolean false true
    byte 8 0 Byte -128
    -27
    (Byte.MIN_VALUE)
    127
    27-1
    (Byte.MAX_VALUE)
    short 16 0 Short -32,768
     -215
    (Short.MIN_VALUE)
    32,767
    215-1
    (Short.MAX_VALUE)
    char 16 ‘u0000’ Char 0
    (Character.MIN_VALUE)
    65,535
    216-1
    (Character.MAX_VALUE)
    int 32 0 Integer -2,147,483,648
    -231
    (Integer.MIN_VALUE)
    2,147,483,647
    231-1
    (Integer.MAX_VALUE)
    long 64 0L Long -9,223,372,036,854,775,808
     -263
    (Long.MIN_VALUE)
    9,223,372,036,854,775,807
    263-1
    (Long.MAX_VALUE)
    float 32 0.0F Float 1.40129846432481707e-45
    (Float.MIN_VALUE)
    3.40282346638528860e+38
    (Float.MAX_VALUE)
    double 64 0.0 Double 4.94065645841246544e-324
    (Double.MIN_VALUE)
    1.7976931348623157e+308
    (Double.MAX_VALUE)

    Literals

    A literal is a value, which can be assigned to a primitive type or a String. For
    example:





































    Variable definition Literal definition
    String myVariable = “literal”; // a String literal
    char my_char = ‘c’; // a char literal
    boolean isWriting = true; // a boolean literal
    short my_short = 28; // an Integral literal
    double my_double = 3.14; // a Floating-point literal

    ‘null’ and ‘boolean’ literals

    The ‘null’ literal can only be assigned to a reference type (Object).
    It is not allowed to assign a primitive type to ‘null’. The ‘null’ literal indicates that
    the Object reference has no value.
    We can only assign boolean literals to boolean primitive types.
    There are exactly two boolean literal values: ‘true’ and ‘false’.
    Examples are:






































    Variable definition Literal definition
    String myVariable = null; // a null literal
    char my_char = null; // Illegal! char is not an object
    // it is a primitive type
    boolean isSchool = true; // a boolean literal
    boolean isBook = “true”; // Illegal! “true” is not a boolean literal
    // it is an object ( reference type )
    double my_double = 3.14; // a Floating-point literal

    ‘char’ literals

    We can define a char literal as a single letter, an escape sequence, a digit,
    or a Unicode letter enclosed in single quotes.
    We cannot directly use a single quote, or a backslash in single quotes.

    We can indirectly use those characters by using character escape sequences.
    Examples are:
























































    Variable definition Literal definition
    char my_char = ‘c’; // legal
    char my_unicode_char = ‘u0062’; // legal
    char my_tab_char = ‘t’; // legal
    char my_backslash_char = ”; // legal
    char my_backslash_char1 = ”; // Illegal
    char my_single_queto_char = ”’; // legal
    char my_single_queto_char1 = ”’; // Illegal
    char my_double_queto_char = ‘”‘; // legal



    The following list shows the list of character escape sequences.






























    Character Escape Sequences
    b backspace
    t tab
    n linefeed
    f form feed
    r carriage return
    double quote
    single quote
    backslash

    As you can see, those character escape sequences — except double-quote, single-quote,
    and the backslash character — don’t have a graphical representation.

    String literals

    We can define a String literal as a set of char literal. It is simply a set of char
    literals enclosed in double-quotes.
    It is allowed to define single-quotes, but it is not allowed to define double-quotes
    or the backslash character directly.
    We can use character escape sequences to define those values indirectly.
    Examples are:
























































    Variable definition Literal definition
    String my_String_literal = “Basic examples”; // legal
    String my_Str_literal = “u0062asic examples”; // legal
    String my_Str = “t Examples”; // legal
    String my_Str_with_quetos = “”Examples””; // legal
    String my_Str_with_s_quetos = “‘”; // legal
    String my_Str_with_bs_quetos = “”; // Illegal
    String my_Str_with_d_quetos = “””; // Illegal
    String my_Str_with_d_quetos1 = “””; // legal

    Integral literals

    We can define an integral literal as octal, decimal, or hexadecimal integers.
    The default is decimal.
    An octal litarel value is preceded by 0, and subsequent digits can be any between 0 and 7
    (inclusive).
    A decimal value must begin with a digit between 1 and 9, subsequent digits can be
    between 0 and 9.
    A hexadecimal value must have 0x, or 0X prefix subsequent characters can be a
    digit or a letter.
    The digits are between 0 and 9, and the letters can be a, b, c, d, e, or f (case intensive).


    An integral literal is decimal and its type is 32 bit ‘int’ by default. For that reason,
    the compiler will throw an exception if we define an integral literal exceeding the
    range of ‘int’ primitive type.
    The ‘long’ primitive type is defined by an ‘l’ or ‘L’ suffix of the literal. For example,
    254L or 254l is a long literal.
    Examples are:
































    Variable definition Literal definition
    int my_literal = 512; // legal
    long my_long_literal = 512L; // legal
    int my_octal_literal = 054; // legal
    int my_hex_literal = 0xdF; // legal

    Floating-point literals

    As mentioned before, a floating-point literal complies with the IEEE 754
    floating-point specification.
    We can define a floating-point literal by adding ‘f’, ‘d’, a decimal point, or an
    exponential letter ‘e’ to the
    literal value. The letters ‘f’ and ‘d’ specify the type of the literal. That is to say, if
    we use ‘f’, the literal
    will be identified as a float value by the compiler. For example, 3.14f literal is
    type of float. The default value is
    double. Namely, if we do not mention a letter (‘d’ or ‘f’) the literal will be
    identified as type of double.

    It is also possible to specify the ‘double’ type explicitly by adding a ‘d’ or ‘D’ suffix.
    We can define float-point literals, which are type of ‘float’, by adding a ‘f’ or ‘F’ suffix.
    For example, both 3.14f, and 3.14F are valid floating-point literals.
    Floating point literals can also have an exponential part. The exponential part has an ‘
    e’ or ‘E’ prefix and optional ‘+’ or ‘-‘ signs followed by digits. For example,
    0.23E-2 is a valid floating-point number with exponential part.
    The following table shows the four possible floating-point representations.
































    Example Rule
    1.) 314.

    3.14

    3.14f

    314.e-2

    314.d

    one or more digits, and ‘.’

    followed by optional part, which can be digits,
    an exponential part,
    or a floating-point suffix
    2.) .314

    .314f

    .314f

    .314e-3

    .314e-3d

    one ‘.’ and one or more digits,

    followed by optional part, which can be
    an exponential part,
    or a floating-point suffix
    3.) 314e-2

    314e-2f

    314e-2d

    one or more digits, and one exponential part

    followed by optional floating-point suffix
    4.) 314e-2f

    314f

    314d

    314e2d

    one or more digits, and one optional exponential part

    followed by mandatory floating-point suffix

    Arrays


    An array is a storage place of a list of items of the same type.
    An array can be either a collection of primitive types or object references.
    We cannot define two different type in an array.
    Every element of an array must be of the same type.
    A Java array is type Object. Therefore, methods of type Object can be called on
    an array. The size of an array cannot be changed
    once the array is created. The elements of an array can be accessed by
    an index value that is greater or equal to zero.
    The index value is type ‘int’ and cannot be a negative number.
    If the array size is specified by n then the
    elements of the array can be accessed by an index value between the range of 0 and n-1.
    It is possible to create an array with zero elements. Once an array is declared and allocated,
    array elements are automatically initialized to a default value, wherever the array is defined.
    It does not matter whether the array is declared at class level (member) or method level (local),
    the elements of the array will always be initialized to the default value of the element.
    Reference variables are initialized to ‘null’ and primitive types are initialized to the default values.
    However, there will not be an automatic initialization if the array is only declared (not allocated).
    Arrays should be declared, allocated, and initialized. The following table shows a couple of array examples
    that have been declared, allocated, and set to either a specific or a default value.























    Declaration Allocation Initiliazation
    int []a; a = new int[ 5 ]; // Every element is implicitly
    // initialized to 0
    String str[]; str = new String[ 3 ]; // Every element is implicitly
    // initialized to null
    double []d; d = new double[ 5 ]; // Every element is implicitly
    // initialized to 0.0

    As you can see, we do not specify the size of the array in the declaration part.
    Alternatively, it is possible to declare, allocate, and define a list of custom elements in one step.
    That is done as follows.


    Example

























    All in one step
    int []a = {1,2,3,4,5}; // legal
    int []a[] = {new int[]{2,3,4},{1,2}}; // legal
    int a[][] = {new int[3],{1,2}}; // legal
    int [5]a = {1,2,3,4,5}; // illegal. definning array size is
    // never allowed at the first half
    int []a = new int[5]{1,2,3,4,5}; // illegal. Cannot define dimension
    // expressions when an array
    // initializer is provided

    Variable Types and Variable Initialization


    We can classify variables according to their lifetimes. In this respect, there are two
    types of variables : member and method local. Member variables are declared at class level
    (like methods).
    A member variable is automatically initialized to a default value. The list of default
    values is mentioned and a
    table is given in the previous section.
    A member variable is implicitly initialized to null if declared as an Object reference,
    and initialized to a default value if declared as a primitive type.
    Method local variables are declared in a method. Method local variables are not
    initialized to a default value;
    therefore, they must be initialized to a value explicitly.









    Member and local variables


    class myClass {
    // Member variables
    int k; // Implicitly set to default value 0
    double d; // Implicitly set to default value 0.0
    public void myMethod(){
    // Method local variable
    int local_t = 0;
    // we must initialize
    // ‘local_t’ explicitly
    }
    }

    Sample Code


    You can use the following code to practice on this topic.








    Sample Code


    final class Singleton {
    // M e m b e r V a r i a b l e s
    // Instance variables
    private int defaultint;
    private double defaultdouble;
    private char defaultchar;
    private String defaultString;
    // Static ( Class ) variables
    private static Singleton singleton;
    private static String ARRAYFACTORY = “ArrayFactory”;
    private static String NUMBERFACTORY = “NumberFactory”;
    // C o n s t r a c t o r
    private Singleton() {
    };
    // M e t h o d s
    // Static method
    public static Singleton getReference() {
    if (singleton == null)
    singleton = new Singleton();
    return singleton;
    }
    public void test() {
    // Method local variable
    String factory_msg = ” is called”;
    println(NUMBERFACTORY + factory_msg);
    // default values
    println(” int = “ + defaultint);
    println(” double = “ + defaultdouble);
    println(” char = “ + defaultchar);
    println(” String = “ + defaultString);

    println(ARRAYFACTORY + factory_msg);
    // int array
    int sequence[] = {1, 2, 3, 4, 5};
    println(” Array size of ‘sequence’ is ”
    + sequence.length);
    for (int k = 0; k < sequence.length; k++) println(” Array element sequence[” + k + “] = “
    + sequence[k]);
    // double array
    double defaultvalue[] = new double[3];
    println(” Array size of ‘defaultvalue’ is “
    + defaultvalue.length);
    for (int k = 0; k < defaultvalue.length; k++) println(” Array element defaultvalue[” + k + “] = “
    + defaultvalue[k]);
    }
    public static void println(String str) {
    System.out.println(str);
    }
    // M a i n M e t h o d
    public static void main(String[] args) {
    Singleton.getReference().test();
    }
    }


    Questions




    1. Which of the followings are valid array declarations? (Select one or more answers)

      a.) int k[][] = new int[5,3];

      b.) int i[] = new i[];

      c.) int int[] = new int[];

      d.) int [][]i= new int[3][4];

      e.) int []a = {1,2,3,4,5};

    2. What is the hexadecimal representation of 12 as a valid Java literal?

      a.) 12

      b.) C

      c.) c

      d.) 0C

      e.) 0xc

    3. What are the legal identifers in Java? (Select one or more answers)

      a.) %factory

      b.) $factory

      c.) object-factory

      d.) for

      e.) _byte

      f.) 3rdfactory

      g.) Int

    4. What is the output of the following program?

      class Example{
      static int sequence[] = {1, 2, 3, 4, 5};
      public static void main(String args[]){
      System.out.println(” The value of sequence[4] is ”
      + sequence[4]);
      }
      }

      a.) Null Pointer exception is raised

      b.) Runtime error, because the class is not instantiated

      c.) The value of sequence[4] is 0

      d.) The value of sequence[4] is 5

      e.) The value of sequence[4] is null

    5. What is the range of an int?

      a.) -2-32 , 232

      b.) -2-31 , 231

      c.) -2-31-1 , 231

      d.) -231 , 231-1

      e.) -2-31 , 231-1

      f.) 0, 232


    Answers

    I suggest you compile and try other possible combinations by yourself. Practical
    work is important for the exam. Instead of just looking at the answers, try to compile
    the code and learn from the results you receive.



    1. d,e

    2. e

    3. b,e,g

    4. d

    5. d


    References



    [ 1 ]. Java
    Language Specification (Second Edition) by James Gosling, Bill
    Joy, Guy Steele



    [ 2 ]. Information
    about SCJP Certification



    [ 3 ]. Sun
    Certified Programmer for Java 2 Platform Success Guide



    [ 4 ]. SJCP
    Exam Preparation: Top-level and Inner Classes



    [ 5 ]. http://www.unicode.org


    About the Author



    Koray Guclu holds a Computer Engineering degree. He works as a software developer and
    freelance author. You can reach him at korayguclu@yahoo.com.
    His Web site is http://www.geocities.com/korayguclu/.



    Get the Free Newsletter!

    Subscribe to Developer Insider for top news, trends & analysis

    Latest Posts

    Related Stories