These are some basic java stuff - very basic. Some definitions also. Hey, plz don't get scared. I personally feel that these things should not be ignored. Most of us use these concepts extensively but not sure wht exactly they are meant for and how exactly they work. At some points, u can practice RC - just read them.
- A class--the basic building block of an object-oriented language such as Java--is a template that describes the data and behavior associated with instances of that class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods.
- When the System class is loaded into the application, it instantiates PrintStream and assigns the new PrintStream object to the out class variable. Now that you have an instance of a class, you can call one of its instance methods:
System.out.println("Hello World!");
- The argument to java (while running the program) is the name of the class that you want to use, not the filename.
- final int aFinalVar = 0; You may, if necessary, defer initialization of a final local variable. Simply declare the local variable and initialize it later, as follows. final int blankfinal; . . . blankfinal = 0; A final local variable that has been declared but not yet initialized is called a blank final. Again, once a final local variable has been initialized, it cannot be set again.
- The unlabeled form of the break statement is used to terminate the innermost switch, for, while, or do-while statement; the labeled form terminates an outer statement, which is identified by the label specified in the break statement. Similar is the case with both labelled and unlabelled continue stmt which skips the current iteration.
- public Rectangle(int w, int h) {
this(new Point(0, 0), w, h);
} - I haven't ever used this.
The most common reason for using this is that a member variable is hidden by an argument to the method or the constructor.
- String — for immutable (unchanging) data. StringBuffer — for storing and manipulating mutable data. This class is safe for use in a multi-threaded environment. StringBuilder — A faster, drop-in replacement for StringBuffer, designed for use by a single thread only.
- If you are passing a primitive data type (such as a char) into a method that expects a wrapper object (such as a Character) the Java compiler automatically converts the type for you. This feature is called autoboxing or unboxing if the conversion goes the other way.
- private access specifier in a constructor's declaration Only this class can use this constructor. Making a constructor private, in essence, makes the class final — the class can't be subclassed, except from within. If all constructors within a class are private, the class might contain public class methods (called factory methods) that create and initialize an instance of this class. Other classes can use the factory methods to create an instance of this class.
- Variable number of arguments can also be passed to a method(introduced in J2SE 5.0). public static Polygon polygonFrom(Point... listOfPoints) { ... } for example, the printf method: public PrintStream printf(String format, Object... args) can then be called like this: System.out.printf("%s: %d, %s%n", name, idnum, address);
Now suppose that you have a method declared to return a Number: public Number returnANumber() { ... } The returnANumber method can return an ImaginaryNumber but not an Object. ImaginaryNumber is a Number because it's a subclass of Number. However, an Object is not necessarily a Number — it could be a String or another type. You can override a method and define it to return a subclass of the original method, like this: public ImaginaryNumber returnANumber() { ... } This technique, called covariant return type (introduced in release 5.0), means that the return type is allowed to vary in the same direction as the subclass. You also can use interface names as return types. In this case, the object returned must implement the specified interface..
- The overriding method has the same name, number and type of arguments, and return value as the method it overrides. (In fact the return type of a subclass can be a subclass of the return type of its superclass.) The overriding method can have a different throws clause as long as it doesn't specify any types not specified by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more but not less access than the overridden method. For example, a protected method in the superclass can be made public but not private.
- If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass. The distinction between hiding and overriding has important implications. For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called. For instance methods, the runtime system invokes the method defined in the runtime type of the reference on which the method is called.
Animal super class public class Animal {
public static void hide() {
System.out.format("The hide method in Animal.%n");
} public void override() {
System.out.format("The override method in Animal.%n");
}
}
The second class, a subclass of Animal, is called Cat:
public class Cat extends Animal {
public static void hide() {
System.out.format("The hide method in Cat.%n");
}
public void override() {
System.out.format("The override method in Cat.%n");
} public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
//myAnimal.hide(); //BAD STYLE
Animal.hide(); //Better! Cat.hide();
myAnimal.override();
}
}
The hide method in Animal --- decided at compile time
The hide method in Cat --- decided at compile time
The override method in Cat --- decided at run time (at compile time it is Animal class' override(), but at runtime it changed to Cat classes' override() method).
Defining a Method with the Same Signature as a Superclass's Method:
An instance method cannot override a static method and vice versa. Superclass Instance Method Superclass Static Method
Instance Method Overrides (return type must be a Generates a compile-time error
subtype of the return type of the
superclass's method) Static Method Generates a compile-time error Hides
--Karteeek