Java Quick Revision
Java Quick Revision
The static keyword is applicable to variables, methods or a block of code called static
initializes.
A native method can't be abstract but it can throw exception(s).
A final class cannot have abstract methods.
A class can't be both abstract and final.
A transient variable may not be serialized.
All methods of a final class are automatically final.
While casting one class to another subclass to superclass is allowed without any type
casting.
e.g.. A extends B , B b = new A(); is valid but not the reverse.
The String class in java is immutable. Once an instance is created, the string it contains
cannot be changed.
e.g. String s1 = new String("test"); s1.concat("test1"); Even after calling concat()
method on s1, the value of s1 will remain to be "test". What actually happens is a new
instance is created.
But the StringBuffer class is mutable.
The + and += operators are the only cases of operator overloading in java and is
applicable to Strings.
Bit-wise operators - &, ^ and | operate on numeric and boolean operands.
The short circuit logical operators && and || provide logical AND and OR operations on
boolean types and unlike & and | , these are not applicable to integral types. The
valuable additional feature provided by these operators is the right operand is not
evaluated if the result of the operation can be determined after evaluating only the left
operand.
The difference between x = ++y; and x = y++;
In the first case y will be incremented first and then assigned to x. In second case first y
will be assigned to x then it will be incremented.
Please make sure you know the difference between << , >> and >>>(unsigned
rightshift) operators.
The initialization values for different data types in java is as follows
byte = 0, int = 0, short = 0, char = '\u0000', long = 0L, float = 0.0f, double = 0.0d,
boolean = false,
object referenece (of any object) = null.
Setting the object reference to null makes it a candidate for garbage collection.
An overriding method may not throw a checked exception unless the overridden method
also throws that exception or a superclass of that exception.
per object. The signature of finalize() method of Object class is : protected void finalize()
throws Throwable.
The parent classes of input and output streams : InputStream and OutputStream class
are abstract classes.
File class can't be used to create a new file. For that any other output stream class such
as FileOutputStream or filter streams like PrintWriter should be used. Please note that
java will create a file only when the file is not available.
For the RandomAccessFile constructor, the mode argument must either be equal to "r" or
"rw".
In java, console input is accomplished by reading from System.in .
System.in is an object of type InputStream; System.out and System.err are objects of
type PrintStream.
Both FileInputStream and FileOutputStream classes throws FileNotFoundException. In
earlier versions of java, FileOutputStream() threw an IOException when an output could
not be created.
System.in is an object of type InputStream; System.out and System.err are objects of
type PrintStream.
A method can't be overridden to be more private. e.g.. a public method can only be
overridden to be public.
Both primitives and object references can be cast.
If inner class is declared in a method then it can access only final variables of the
particular method but can access all variables of the enclosing class.
To refer to a field or method in the outer class instance from within the inner class, use
Outer.this.fieldname .
Inner classes may not declare static initializers or static members unless they are
compile time constants i.e. static final var = value;
A nested class cannot have the same name as any of its enclosing classes.
Inner class may be private, protected, final, abstract or static.
An example of creation of instance of an inner class from some other class:
class Outer
{
public class Inner{}
}
class Another
{
public void amethod()
{
Outer.Inner i = new Outer().new Inner();
}
}
Classes defined in methods can be anonymous, in which case they must be instantiated
at the same point they are defined. These classes can't have explicit constructor and
may implement interface or extend other classes.
The Thread class resides in java.lang package and need not be imported.
The sleep and yield methods of Thread class are static methods.
The range of Thread priority in java is 1-10. The minimum priority is 1 and the maximum
is 10. The default priority of any thread in java is 5.
There are two ways to provide the behavior of a thread in java: extending the Thread
class or implementing the Runnable interface.
The only method of Runnable interface is "public void run();".
New thread take on the priority of the thread that spawned it.
Using the synchronized keyword in the method declaration, requires a thread obtain the
lock for this object before it can execute the method.
A synchronized method can be overridden to be not synchronized and vice versa.
In java terminology, a monitor is any object that has some synchronized code.
Both wait() and notify() methods must be called in synchronized code.
The notify() mthod moves one thread, that is waiting on this object's monitor, into the
Ready state. This could be any of the waiting threads.
The notifyAll() method moves all threads, waiting on this object's monitor into the Ready
state.
Every object has a lock and at any moment that lock is controlled by, at most, one single
thread.
There are two ways to mark code as synchronized:
a.) Synchronize an entire method by putting the synchronized modifier in the method's
declaration.
b.) Synchronize a subset of a method by surrounding the desired lines of code with curly
brackets ({}).
The argument to switch can be either byte, short , char or int.
The expression for an if and while statement in java must be a boolean.
Breaking to a label (using break <labelname>
means that the loop at the label will be
terminated and any outer loop will keep iterating. While a continue to a label (using
continue <lablename>
A static method can only call static variables or other static methods, without using the
instance of the class. e.g. main() method can't directly access any non static method or
variable, but using the instance of the class it can.