piątek, 9 września 2011

ASSIGNMENTS

1.
    class X {}
    class Y{}

    X x = new X();

    System.out.println((x instanceof Y)); - does not compile. There is no possibility x to be instance of Y class
    The instanceof compile time error exists only in case of class.
---------------------------------------------------------------------------------------------------------------
2.

   class X {}
   interface Y{};

   X x = new X();
   System.out.println((x instanceof Y)); - compile. It is possible that at some point X can implement Y interface.

Exactly the same behavior (as in 1 and 2) is in case of == operator.
---------------------------------------------------------------------------------------------------------------
3.

int i = 3, n = -4;
System.out.println(-i + " " + -i); it is correct, the output: -3 4
so: -i is the same as: i = -1*i

---------------------------------------------------------------------------------------------------------------
4.

The result of Integer.parseInt("011")  is 11 not 9! The reason is that the radix is decimal.
The result of Integer.parseInt("011", 8) is 9.


---------------------------------------------------------------------------------------------------------------
5.

float f = 10;    //correct
float f = 10.0  //incorrect, it must be float f = 10.0f


floating point numbers are by default double. If you want to assing double to float the explicit cast is required, e.g.:  float f = (float)10.0



---------------------------------------------------------------------------------------------------------------
6. Local variables must be initialized before use, look at the below example:

    void method(){
        String s;
        if (s == null) {            //compile time error
            s = "s";
        }
    }


---------------------------------------------------------------------------------------------------------------
7. It is not allowed to compare (using ==) primitive and null;

e.g.
int i = 3;
if (i == null) {    /compile-time error
...
}


Brak komentarzy:

Prześlij komentarz