Tuesday, October 9, 2012

Ramblings of a confused Java programmer

javac -d path_to_classes_dir name of java file

The -cp option can be used to set the class path for each application individually

for example
-cp /pgjc/work:/top/bin/pkg:.
Here  path-separator character ':' for Unix platforms to separate the entries, and also included the current directory (.) as an entry.
For windows it is ';' used as path separator

One cannot call method of base class using super keyword from static method.
Given below is example
   class SuperCalc {
                 protected static  int multiply(int a, int b) { return a * b;}
    }
    class SubCalc extends SuperCalc{
                 public  static  int multiply(int a, int b) {
                 int c =super.multiply(a, b);   // wrong !! cannot use super from inside static
                 return c;
     }
 }

Class names and method names exist in different namespaces.


Collections.sort() // can only be used for List or its subclasses
Arrays.sort() // can only be used for object arrays or long[]

Default modifier is narrower than protected modifier . Method having default access modifier can be declared as protected in its  sub class.
Default member  accessibility is more restrictive than protected member accessibility.

A base class reference cannot be assigned to sub class reference . ( it will give compile time error ) if
type casted then will give runtime class cast exception.
but a subclass ref can be assigned to base class ref

Need to go through Locale / Date / Date format / Localization


An anonymous inner class can extend either one subclass or implement one
interface. Unlike non-anonymous classes (inner or otherwise), an anonymous
inner class cannot do both. In other words, it cannot both extend a class and
implement an interface, nor can it implement more than one interface.

this is not only a keyword its a constructor !

class Base1{
               private int  score;
               private int goal;
   
              Base1()
              {
                 this(20);   
              }
              Base1(int score)
              {
               //this.score=score;   valid
               this(score, 2*score);
              }
              Base1(int score, int goal)
              {
               this.score=score;
               this.goal=goal;
               }
               void printScore()
              {
                 System.out.println(" Score ="+score + "goal = "+goal);
              }
}

addAll() , retainAll(), removeAll () are destructive operations .!! be safe while playing .

Generic Type classes cann't be instantiated !
nor Arrays can be formed of Generic Type .




The && and || operators are short circuit operators. A short circuit operator is one that doesn't
necessarily evaluate all of its operands. Take, for example, the operator &&. What happens when Java
executes the following code?
if (0 == 1 && 2 + 2 == 4) {
out.println("This line won't be printed.");
}


Realize that the condition (2 + 2 == 4 || whatever) must be true, no matter what
the whatever condition happens to be.



Field of an object is accessed using a reference, it is the type of the reference, not the class of the
current object denoted by the reference at runtime  .  It is called Field Hiding .

Interfaces are by default static , irrespective of their declaration.
Non static Inner classes can't have static  members/ interfaces / static  methods .
Only inner classes can be declared as static  .

Interfaces cannot be static unless they are inside a class.

No comments: