DOC

Mynote_list

By Jesus Nichols,2014-10-29 07:31
108 views 0
Mynote_list

    lMynote lists

    Mynote_1:

     Array

     Modifiers

     Constructors

Mynote_2:

     if, swich

     for, while, do

     try/catch/finish and throw exception

Mynote_3:

     gc

Mynote_4:

     Fundamentals

     Interfaces

     Keywords

     Primitive data type

Mynote_5:

     Operators

     Convert and casting

Mynote_6:

     OO

     Overloading and overridden

    Inner classes

Mynote_7:

     Threads

Mynote_8:

     AWT layout manager

     Event handler

     AWT components

     Applet and HTML

Mynote_9:

     Java.util

     Math

     String and StringBuffer classes

Mynote_10:

     Collections

Mynote_11

     I/O

     1

1) Declarations and Access Control

    Objective 1

    Write code that declares, constructs and initializes arrays of any base type

    using any of the permitted forms, both for declaration and for initialization.

    1. Arrays are Java objects. (An object is a class instance or an array.) and may be assigned to variables

    of type Object. All methods of class Object may be invoked on an array. If you create an array of

    5 Strings, there will be 6 objects created. 2. Arrays should be

    1. Declared. (int[] a; String b[]; Object []c; Size should not be specified now)

    2. Allocated (constructed). ( a = new int[10]; c = new String[arraysize] )

    3. Initialized. for (int i = 0; i < a.length; a[i++] = 0)

    3. The above three can be done in one step. int a[] = { 1, 2, 3 }; or int a[] = new int[] { 1, 2, 3 }; But

    never specify the size with the new statement. 4. Java arrays are static arrays. Size has to be specified at compile time. Array.length returns array‟s size,

    cannot be changed. (Use Vectors for dynamic purposes). 5. Array size is never specified with the reference variable, it is always maintained with the array object.

    It is maintained in array.length, which is a final instance variable. Note that arrays have a length field

    (or property) not a length() method. When you start to use Strings you will use the string, length

    method, as in s.length();

    6. Anonymous arrays can be created and used like this: new int[] {1,2,3} or new int[10]

    7. Arrays with zero elements can be created. args array to the main method will be a zero element array if

    no command parameters are specified. In this case args.length is 0.

    8. Comma after the last initializer in array declaration is ignored.

     int[] i = new int[2] { 5, 10}; // Wrong

    int i[5] = { 1, 2, 3, 4, 5}; // Wrong

    int[] i[] = {{}, new int[] {} }; // Correct

    int i[][] = { {1,2}, new int[2] }; // Correct

    int i[] = { 1, 2, 3, 4, } ; // Correct

    int i[][] = new int [10][]; //Correct, i.length=10.

     int [] i, j[] == int i[], j[][];

    9. Array indexes start with 0. Index is an int data type.

    10. Square brackets can come after datatype or before/after variable name. White spaces are fine. Compiler

    just ignores them.

    11. Arrays declared even as member variables also need to be allocated memory explicitly.

    static int a[];

    static int b[] = {1,2,3};

    public static void main(String s[]) {

     System.out.println(a[0]); // Throws a null pointer exception

     System.out.println(b[0]); // This code runs fine

     System.out.println(a); // Prints „null‟

     System.out.println(b); // Prints a string which is returned by toString

    }

    12. Once declared and allocated (even for local arrays inside methods), array elements are automatically

    initialized to the default values.(0 for numeric arrays, false for boolean, '\0' for character arrays, and

    null for objects).

    13. If only declared (not constructed), member array variables default to null, but local array variables will

    not default to null(compile error).

    14. Java doesn‟t support multidimensional arrays formally, but it supports arrays of arrays. From the

    specification - “The number of bracket pairs indicates the depth of array nesting.” So this can perform

    as a multidimensional array. (no limit to levels of array nesting)

     2

    15. Arrays must be indexed by int values; short, byte, or char values may also be used as index

    values because they are subjected to unary numeric promotion and become int values. An attempt to

    access an array component with a long index value results in a compile-time error.

    16. All array accesses are checked at run time; an attempt to use an index that is less than zero or greater

    than or equal to the length of the array causes an ArrayIndexOutOfBoundsException to be

    thrown.

    17. Every array implements the interfaces Cloneable and java.io.Serializable.

    18. Array Store Exception

    If an array variable v has type A[], where A is a reference type, then v can hold a reference to an instance of any array type B[], provided B can be assigned to A.

    Thus, the example:

    class Point { int x, y; }

    class ColoredPoint extends Point { int color; }

    class Test {

     public static void main(String[] args) {

     ColoredPoint[] cpa = new ColoredPoint[10];

     Point[] pa = cpa;

     System.out.println(pa[1] == null);

     try {

     pa[0] = new Point();

     } catch (ArrayStoreException e) {

     System.out.println(e);

     }

     }

    }

    produces the output:

    true

    java.lang.ArrayStoreException

    Testing whether pa[1] is null, will not result in a run-time type error. This is because the element of the

    array of type ColoredPoint[] is a ColoredPoint, and every ColoredPoint can stand in for a

    Point, since Point is the superclass of ColoredPoint. On the other hand, an assignment to the array pa can result in a run-time error. At compile time, an

    assignment to an element of pa is checked to make sure that the value assigned is a Point. But since pa

    holds a reference to an array of ColoredPoint, the assignment is valid only if the type of the value

    assigned at run-time is, more specifically, a ColoredPoint. if not, an ArrayStoreException is

    thrown.

19. Every element of an array must be of the same type The type of the elements of an array is decided

    when the array is declared. If you need a way of storing a group of elements of different types, you can

    use the collection classes.

Objective 2

    Declare classes, inner classes, methods, instance variables static, variables

    and automatic (method local) variables, making appropriate use of all

    permitted modifiers (such as public final static abstract and so forth). State

    the significance of each of these modifiers both singly and in combination

    and state the effect of package relationships on declared items qualified by

    these modifiers.

    1. Two types of variables.

    1. Member variables

    ? Accessible anywhere in the class.

    ? Automatically initialized before invoking any constructor.

     3

    ? Static variables are initialized at class load time.

    ? Can have the same name as the class.

    2. Automatic variables(method local)

    ? Must be initialized explicitly. (compiler will catch it when using, but doesn‟t catch it if no

    using) Object references can be initialized to null to make the compiler happy.

    ? Can have the same name as a member variable, resolution is based on scope.

    ? Can only be final. Not other modifiers.

2. Modifiers are Java keywords that provide information to compiler about the nature of the code, data and

    classes. The visibility modifiers are part of the encapsulation mechanism for Java. Encapsulation allows

    separation of the interface from the implementation of methods.

3. Access modifiers public, protected, private

    ? Only applied to class level variables. (Method variables are visible only inside the method.)

    ? Can be applied to class itself (only to inner classes declared at class level, no such thing as protected or

    private top level class)

    ? Can be applied to methods and constructors.

    ? If a class is accessible, it doesn‟t mean, the members are also accessible. But if the class is not

    accessible, the members are not accessible, even though they are declared public. ? If no access modifier is specified, then the accessibility is default package visibility. All classes in the