Professional Documents
Culture Documents
This means you need not follow any indentation rules. You can even write an ent
ire program on one line, and still execute it successfully.
*A Java program is a free-format sequence of characters that is tokenized by the
compiler (broken into a stream of tokens) for further analysis.
*They should not begin with numbers.
- AvgTemp, count, a4, $test, _tmp --> Valid
- 2count, high-temp, Not/ok --> Not Valid
*Abstraction
Essential element of an object oriented programming.
You can manage complexity thorough an abstraction.
*Encapsulation
Mechanism that binds together the code and data it manipulates, and keeps both
safe from outside interference and misuse.
*Inheritance
Inheritance is the process by which one object acquires the properties of anoth
er object.
Thus in a class hierarchy, two classes are formed - superclass and a subclass.
*Polymorphism
Polymorphism (In Greek Poly means many and morphos means forms) is the ability
of different objects to respond to the same message in different ways.
Polymorphism helps us to design extensible software as we can add new objects t
o the design without rewriting existing procedures.
Polymorphism in Java is achieved by Overloading and Overriding.
*Super is used to invoke the super class constructor, method or variable from su
b class.
*Super must be the first statement in the sub class constructor or method.
*Second form of Super is used to refer to superclass members.
*Method overloading is one of the ways in which Java implements Static Polymorph
ism.
In Java, it is possible to overload two or more methods, only if below statement
s are true: -
The methods should have different parameters.
The methods may or may not have different return type.
Methods should have same name.
All the methods should be in the same scope
*this():
It can be used to call one constructor from the other. The number of parameters,
determines which constructor should be called.
this() must be the first statement in the constructor.
Remember this() and super() can not be in the same constructor.
*Method overriding is the way in which java implements Dynamic Polymorphism.
Method in a sub class should have same name and signature (return type and argu
ments) as the method in the super class.
When an overridden method is called within the sub class, it will always refer
to the version of that method defined by the subclass. The version of the method
defined by the super class will be hidden.
Method overriding occurs only when names and type signature of two methods are
exactly identical otherwise two methods are simply overloaded not overridden.
*When, member is declared as static, it can be accessed, before any objects of t
hat class are created.
Both methods and variables can be declared as static.
main () method is static because it is called before any object exists.
*When objects of its class are declared, no copy of static variables is made. In
stead, all instances of the class share the same static variables.
*All the static variables are initialized to their default values.
*Static methods cannot be overridden,
although they can be re-declared / redefined by a subclass.
So even though static methods can sometimes appear to be overridden,
polymorphism rules do not apply.
Type of a reference variable decides the method call.
*Static Variables
Variables are declared as static essentially global variables.
When objects of its class are declared, no copy of static variables is made. In
stead, all instances of the class share the same static variables.
All the static variables are initialized to their default values.
*Static Methods
They can call only other static methods.
They must access only static data.
They cannot refer to super or this.
*The keyword final has 3 uses - final variable, final method and final class
*Final method
Final method cannot be overridden.
Final method can be overloaded.
Since final methods are not overridden, call to such methods is resolved at com
pile time (Early binding)
*Final class
Final class cannot be inherited.
Final class can be instantiated.
Declaring a class as final implicitly declares all its methods as final too.
*Interface Variables:
public static final int x = 1; // Exactly what you get implicitly
*Order of Execution
Static variable initialization
Static initializer blocks execution (in the order of declaration if multiple bl
ocks are present)
Constructor header (super implicit / explicit)
Instance variables initialization / instance initializer blocks execution.
Rest of the code in the constructor
*Garbage Collection:
Storage for objects is allocated in a designated part of memory called the heap
. The size of the heap is finite.
Garbage collection is a process of managing the heap efficiently;
that is, reclaiming memory occupied by objects that are no longer needed and ma
king the heap memory available for new objects.
*The automatic garbage collector calls the finalize() method in an object that i
s eligible for garbage collection before actually destroying the object.
*The finalize() method is defined in the Object class.
protected void finalize() throws Throwable
* Since there is no guarantee that the garbage collector will ever run, there is
also no guarantee that the finalizer will ever be called.
*A class that is declared within another class or interface, is called a nested
class. Similarly, an interface that is declared within another class or interfac
e, is called a nested interface.
*A top-level class or a top-level interface is one that is not nested.
*There are four categories of nested classes and one of nested interfaces, defin
ed by the context these classes and interfaces are declared in:
-static member classes and interfaces
-non-static member classes |
-local classes |--> inner classes
-anonymous classes |
*They differ from non-inner classes in one important aspect:
that an instance of an nested class may be associated with an instance of the e
nclosing class.
*The instance of the enclosing class is called the immediately enclosing instanc
e.
An instance of an inner class can access the members of its immediately enclosi
ng instance by their simple name.
*Static member class / interface
A static member class or interface is defined as a static member in a class or
an interface.
Such a nested class can be instantiated like any ordinary top-level class, usin
g its full name.
No enclosing instance is required to instantiate a static member class.
Note that there are no non-static member, local, or anonymous interfaces. Inter
faces are always defined either at the top level or as static members.
*Non-static member classes
Non-static member classes are defined as instance members of other classes, jus
t like fields and instance methods are defined in a class.
An instance of a non-static member class always has an enclosing instance assoc
iated with it.
*Local Classes
Local classes can be defined in the context of a block as in a method body or a
local block, just as local variables can be defined in a method body or a local
block.
*Anonymous Classes
Anonymous classes can be defined as expressions and instantiated on the fly.
An instance of a local (or an anonymous) class has an enclosing instance associ
ated with it, if the local (or anonymous) class is declared in a non-static cont
ext
*String
String is a class in Java and not a data-type. (Note S is capital)
Most of the programming languages treat String as array of characters but in Ja
va, Strings are objects.
String objects are immutable
The String class is final It can not be extended and its methods can t be overrid
den.
*StringBuffer
Like String, StringBuffer is also final class.
It provides some additional methods for string operations, which are not presen
t in String class.
StringBuffers are mutable - they can change without creating a new object.
In StringBuffer class, equals() is not overridden; it doesn t compare values.