Language Fundamentals


9) Identify correctly constructed package declarations import statements class declarations (of all forms) including inner classes) interface declarations and implementations (for java.lang.Runnable or other interface described in the test) method declarations (including the main method that is used to start execution of a class) variable declarations and identifiers.

package, import, classes (including inner classes ) :

Package is a collection of grouped classes. eg, java.lang, java.util

  • The fully qualified name of a class consists of the package name and the class name, separated by "."
  • Every class is a member of some package.
  • Interpreter and compiler will look for a class such as Math.class in the directory \java\lang\
  • The root of the search is a directory on the CLASSPATH.
  • so to import all classes under the directory \java\lang\ your import statement should look like

  • import java.lang.*;

    Ordering of Java source file elements:

    eg,
    //This is just an example package
    package myPack;
    import java.lang.*;

    /* main class
    Source filename : Test.java
    */
    public class Test {
    ............
    }
    class myTest extends Test{
    ............
    }
    class Test2 {
    ............
    }

    Interface declarations and implementations :


    creating an interface :
    public interface test {
    public void print();
    }

    Note :There is no curley braces after the print method.

    This code promises that any class that implements the test interface will have a print method that will take a test object regardless of whether or not its superclass promises the same. All descendants of such a class would implement test.
    To implement test in your class,
    class A extends B implements test {
    public void print() {
    System.out.println("test");
    }
    }

    test t = new A();

    Remember :

    Abstract Class Interface
    must not be instantiated must not be instantiated
    may contain static and final data variables are implicitly static and final
    abstract class can have non-abstract methods but, abstract method should be inside an abstract class methods are implicitly abstract. Therefore, all methods should be implemented in the subclass which implements it. no method implementation strictly in the interface.
    abstract method should not contain any of these keywords - private, final, static, native, synchronized [pfsns] methods in interface should not contain any of these keywords - protected, private, final, static, native, synchronized [ppfsns]
    methods are not implicitly public methods are implicitly public even if not specified (be careful when overriding)
    is an incomplete class specification or prescription for behavior
    can extend only one parent class can implement several interfaces atonce
    can have constructors (should contain body) interfaces can't have constructors


    method declarations (including main method):

    signature of main() :
    valid declarations :
    public static void main(String argv[]) {
    //method body
    }

    public static void main(String[] argv) {
    //method body
    }

    static public void main(String[] arg) {
    //method body
    }

    invalid declaration : This will not compile
    public void static main(String[] argv) {
    //method body
    }

    variable declarations :
    Local variables :

    Member variables :
    public class test {
    int a; //Member variable
    public static void main(String arg[]) {
    int b;
    int c=0; //correct way of declaring a local variable
    System.out.println(b);//Local variable. Gives error while compiling "variable b may not have been initialized"
    }
    }

    identifiers :

    eg,
    legal identifiers :

    test1
    $test
    _test
    _234
    $$

    illegal identifiers :

    2test
    my test
    my#test



    10) State the correspondence between index values in the argument array passed to a main method and command line arguments.

    public static void main(String[] arg) { ..... }

    The "main" function receives an array of String as command line arguments.


    java test a b c

    where,
    test is the filename (test.java)
    arg[0] will contain a
    arg[1] will contain b
    arg[2] will contain c

    Remember : In java, arrays start numbering from 0.



    11) Identify all Java programming language keywords.
    abstract default goto(reserved) null (reserved) synchronized
    boolean do if package this
    break double implements protected throw
    byte else import private throws
    case extends instanceof public transient (reserved)
    catch final int return try
    char false (reserved) interface short void
    class finally long static volatile
    const (reserved) float native super true(reserved)
    continue for new switch while



    12) State the effect of using a variable or array element of any kind when no explicit assignment has been made to it.

    Automatic (or) local variables :
    are not initialized by the system; must be explicitly initialized before being used.
    public class test {
    public static void main(String arg[]) {
    int i;
    System.out.println("i : " + i);
    }
    }

    Compile Error: Variable i may not have been initialized.
    System.out.println("int : " + i);

    Member (or) Instance variables :
    are assigend an initial value automatically.
    public class initial {
    char c;
    int in;
    short s;
    byte b;
    long l;
    float f;
    double d;
    boolean bool;
    String str;

    public static void main(String arg[]) {
    initial i = new initial();
    System.out.println("char : " + i.c);
    System.out.println("int : " + i.in);
    System.out.println("short : " + i.s);
    System.out.println("byte : " + i.b);
    System.out.println("long : " + i.l);
    System.out.println("float : " + i.f);
    System.out.println("double : " + i.d);
    System.out.println("boolean : " + i.bool);
    System.out.println("string : " + i.str);
    }
    }

    ouput :
    char :
    int : 0
    short : 0
    byte : 0
    long : 0
    float : 0.0
    double : 0.0
    boolean : false
    string : null

    Element of an array :

    Array element will be automatically set to default value both inside class and method.
    public class arr {
    int i[] = new int[5];
    public static void main(String str[]) {
    arr newarr = new arr();
    String a[]=new String[10];
    System.out.println("String : " + a[0]);
    System.out.println("int : " + newarr.i[2]);
    }
    }

    output :
    String : null
    int : 0



    13) State the range of all primitive data types and declare literal values for String and all primitive types using all permitted formats bases and representations.

    Range of primitive data types :
    Data type
    bits
    range (base 2)
    range (decimal)
    byte 8 -27 to 27 - 1 -128 to 127
    short 16 -215 to 215 - 1 -32768 to 32767
    int 32 -231 to 231 - 1 -2147483648 to 2147483647
    long 64 -263 to 263 - 1 -9223372036854775808 to 9223372036854775807
    char 16 0 to 216 - 1 0 to 65535 (or) '\u0000' to '\uffff'

    Data type bits
    boolean 1 (true/false)
    float 32
    double 64

    literal values for String and all primitive types :

    Converting from Decimal to Binary, Octal, Hexadecimal
    eg,
    Decimal to Binary :
    18 in Decimal
    2|18
    ----
    2|9-0
    ----
    2|4-1
    ----
    2|2-0
    ----
    1-0

    10010 in binary

    converting negative decimal numbers to binary :
    This is by Kathy Kozel in Java Study Group


    The left-most bit is the sign bit. If the left most bit is 1 then the number is negative else the number is positive.
    if you start with a negative binary number...
    1111 1010 // and you want to know the value
    1. Flip the bits
    0000 0101
    2. Add 1
    0000 0101
    +
    0000 0001
    ------------
    0000 0110 // this is 6, so the value was -6.
    By the way, you WILL NOT, repeat WILL NOT be asked to take some big number and represent it negatively in binary. That's what calculators or code are for. But small numbers you may be asked to represent in binary, octal, and hex.



    Decimal to Octal :
    8|18
    ----
    2-2

    22 in octal

    Decimal to HexaDecimal :
    16|26
    ----
    1-A

    1A in Hexadecimal

    Converting from Binary, Octal, Hexadecimal to Decimal


    10010 in binary
    0*2^0 + 1*2^1 + 0*2^2 + 0*2^3 + 1*2^4
    0+2+0+0+16 = 18 in decimal

    22 in octal
    2*8^0 + 2*8^1
    2+16=18 in decimal

    1A in hexadecimal
    A's equivalent in decimal is 10 + 1*16^1 =10 + 16=26 in decimal




    Copyright © 1999-2000, Jyothi Krishnan
    All Rights Reserved.