Quick Revision Tips
SECTION 1: DECLARATIONS AND ACCESS CONTROL
1. An identifier in java must begin with a letter, a dollar sign($), or an underscore (_); subsequent
characters may be letters, dollar signs, underscores, or digits.
2. All the keywords in java are comprised of lower case characters only.
3. There are three top-level elements that may appear in a file. None of these elements is required.
If they are present, then they must appear in the following order:
-package declaration
-import statements
-class definitions
4. A Java source file (Java file) cannot have more than one public class, interface or combination of
both.
5. The variables in an interface are implicitly public, final and static. If the interface, itself, is
declared as public the methods and variables are implicitly public.
6. Variables cannot be synchronized.
7. The variables in Java can have the same name as method or class.
8. A transient variable may not be serialized.
9. The transient keyword is applicable to variables only.
10. The native keyword is applicable to methods only.
11. The final keyword is applicable to methods, variables and classes.
12. The abstract keyword is applicable to methods and classes.
13. The static keyword is applicable to variables, methods or a block of code called static initializers.
14. A native method cannot be abstract but it can throw exception(s).
15. A final class cannot have abstract methods.
16. An abstract class might not have any abstract methods.
17. All methods of a final class are automatically final.
18. Interfaces cannot be final and should not be declared abstract.
19. The visibility of the class is not limited by the visibility of its members. I.e. A class with the
entire members declared private can still be declared public.
20. Interface methods cannot be native, static, synchronized, final, private, protected or abstract.
They are implicitly public, abstract and non-static.
21. The statement float f = 5.0; will give compilation error as default type for floating values is
double and double cannot be directly assigned to float without casting. But f=5.0f is ok.
22. A constructor cannot be native, abstract, static, synchronized or final.
23. Be careful for static and abstract keyword in the program.
24. Also be careful for private keyword in front of a class as top level classes or interfaces cannot be
private.
25. friendly is not a java keyword. This is often used as an alternate word in java literature to
indicate the default access level by placing no modifier like public, private or protected in front of
members of classes and interfaces.
26. A local variable, already declared in an enclosing block and therefore visible in a nested block,
cannot be redeclared in the nested block.
27. Array in Java can be declared and defined like ---
int[] count = null; int []count = null; int count[] = null;
int count[] = new int[50]; int count[] = {5,10,15,20,25}
SECTION 2: FLOW CONTROL, ASSERTIONS, AND EXCEPTION HANDLING
28. Three types of statements regarding flow controls are used in Java:
* Selection statements ' if…else, switch…case, try…catch…finally
* Iteration statement ' while, do…while, for
* Transfer statement ' return, break, continue
29. The argument to switch can be either byte, short, char or int. Be careful about long, float,
double or boolean as argument to switch.
30. The expression for an if and while statement in Java must be a boolean.
31. Breaking to a label (using break ;) 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
;) continues execution with the next iteration of the labeled loop.
32. The if() statement in Java takes only boolean as an argument. Note that if (a = true){},
provided a is of type boolean is a valid statement then code inside the if block will be
executed otherwise skipped.
33. The (-0.0 == 0.0) will return true, while (5.0 == -5.0) will return false.
Print
34. An assertion is a conditional expression that should evaluate to true if and only if your
code is working correctly. If the expression evaluates to false, an error is signaled. Assertions
are like error checks, except that they can be turned completely off, and they have a simpler
syntax.
34. AssertionError is the immediate subclass of java.lang.Error.
35. assert is a java keyword from JDK1.4. So it cannot be used as an identifier from JDK1.4 or
later. But as other JDK versions (JDK1.3 or earlier) had no keyword named assert, an
interesting backward compatibility problem arises for those programs that used assert as a
java identifier.
36. Assertion checks can be turned on and off at runtime. By default, assertion mechanism is
turned off. When assertions are off, they don't use system resources.
37. The command for compiling a source using Java's new assertion feature is javac -source 1.4
filename.java. But if -source argument is not mentioned like javac filename.java, it will be
assumed like javac -source 1.3 filename.java so that existing code compiles correctly even if
it uses assert as a regular identifier.
38. Remember that Assertions can be enabled and disabled for specific packages as well as
specific classes. For example, assertions can be enabled in general but disabled for a
particular package.
39. One of the most common uses of assertions is to ensure that the program remains in a
consistent state. Assertions are not alternative to exception handling rather complementary
mechanism to improve discipline during development phase.
40. Assertions may be used in the situations like:
* to enforce internal assumptions about aspects of data structures.
* to enforce constraints on arguments to private methods.
* to check conditions at the end of any kind of method.
* to check for conditional cases that should never happen.
* to check related conditions at the start of any method.
* to check things in the middle of a long-lived loop.
41. Assertions may not be used in the situations like:
* to enforce command- line usage.
* to enforce constraints on arguments to public methods.
* to enforce public usage patterns or protocols.
* to enforce a property of a piece of user supplied information.
* as a shorthand for if ( something) error();
* as an externally controllable conditional.
* as a check on the correctness of your compiler, operating system, or hardware, unless
you have a specific
42. reason to believe there is something wrong with it and is in the process of debugging it.
43. An overriding method may not throw a checked exception unless the overridden method also
throws that exception or a superclass of that exception.
44. The java.lang.Throwable class has two subclasses: Exception and Error.
45. An Error indicates serious problems that a reasonable application should not try to catch.
Most such errors are abnormal conditions.
46. The two kinds of exceptions in Java are: Compile time (Checked) and Run time (Unchecked)
exceptions. All subclasses of Exception except the RunTimeException and its subclasses are
checked exceptions.
Examples of Checked exception: IOException, ClassNotFoundException.
Examples of Runtime exception: ArrayIndexOutOfBoundsException, NullPointerException,
ClassCastException, ArithmeticException, NumberFormatException.
47. The unchecked exceptions do not have to be caught.
48. A try block may not be followed by a catch but in that case, finally block must follow try.
49. While using multiple catch blocks, the type of exception caught must progress from the most
specific exception to catch to the superclass(es) of these exceptions.
50. More than one exception can be listed in the throws clause of a method using commas. e.g.
public void myMethod() throws IOException, ArithmeticException.
51. Dividing an integer by 0 in Java will throw ArithmeticException.
SECTION 3: GARBAGE COLLECTION
52. Setting the object reference to null makes the object a candidate (or eligible) for garbage
collection.
53. Garbage collection mechanism in java is implemented through a low priority thread, the
behavior of which may differ from one implementation to another. Nothing can be guaranteed about
garbage collection except the execution of finalize().
54. Garbage collection in Java cannot be forced. The methods used to call garbage collection thread
are System.gc() and Runtime.gc().
55. To perform some task when an object is about to be garbage collected, you can override the
finalize() method of the Object class. The JVM only invokes finalize() method once per object. The
signature of finalize() method of Object class is : protected void finalize() throws Throwable.
SECTION 4: LANGUAGE FUNDAMENTALS
56. const and goto are two keywords in Java that are not currently in use.
57. null, true, false are reserved literals in Java.
58. null statement may be used after any statement in any number, in Java if the statement is
not unreachable (like after return statement) without any effect in the program. Like --- if
(i<50)>
59. NULL is not a reserved-word, but null is a reserved-word in Java. Java is a case-sensitive
language.
60. 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 deference (of any object) = null.
61. A static method cannot refer to "this" or "super".
62. A final variable is a constant and a static variable is like a global variable.
63. A static method can only call static variables or other static methods, without using the
instance of the class. e.g. main() method cannot directly access any non static method or
variable, but using the instance of the class it can.
64. instanceof is a Java keyword not instanceOf.
65. The following definition of main method is valid:
static public void main(String[] args)
66. The main() method can be declared final.
67. this() and super() must be the first statement in any constructor and so both cannot be used
together in the same constructor.
68. The example of array declaration along with initialization - int k[] = new int[]{1,2,3,4,9};
69. The size of an array is given by arrayname.length.
70. The local variables (variables declared inside method) are not initialized by default. But the
array elements are always initialized wherever they are defined be it class level or method
level.
71. In an array, the first element is at index 0 and the last at length-1. Please note that length is
a special array variable and not a method.
72. The octal number in Java is preceded by 0 while the hexadecimal by 0x (x may be in small
case or upper case)
e.g. octal :022 and hexadecimal :0x12
SECTION 5: OPERATORS AND ASSIGNMENTS
73. Bit-wise operators - &, ^ and | operate on numeric and boolean operands.
74. 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.
75. Understand the difference between x = ++y; and x = y++; In the first case y will be
incremented first and then assigned to x. In the second case first y will be assigned to x then
it will be incremented.
76. Please make sure you know the difference between << , >> and >>>(unsigned rightshift)
operators.
SECTION 6: OVERLOADING, OVERRIDING, RUNTIME TYPE AND OBJECT
ORIENTATION
77. A static method cannot be overridden to non-static and vice versa.
78. A final class cannot be subclassed.
79. A final method cannot be overridden but a non final method can be overridden to final
method.
80. All the static variables are initialized when the class is loaded.
81. An interface can extend more than one interface, while a class can extend only one class.
82. Instance variables of a class are automatically initialized, but local variables of a function
need to be initialized explicitly.
83. An abstract method cannot be static because the static methods cannot be overridden.
84. An abstract class may not have even a single abstract method but if a class has an abstract
method it has to be declared as abstract.
85. A method cannot be overridden to be more private. E.g. a public method can only be
overridden to be public.
86. 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.
87. Abstract classes cannot be instantiated and should be subclassed.
88. Abstract classes can have constructors, and it can be called by super() when it's subclassed.
89. Both primitives and object references can be cast.
90. Polymorphism is the ability of a superclass reference to denote objects of its own class and
its subclasses at runtime. This is also known as Dynamic Method Lookup.
91. According to Method Overloading Resolution, most specific or matching method is chosen
among the available alternative methods that do not directly match the argument lists but
matches after automatic casting. Ambiguity is shown when compiler cannot determine which
one to choose between two overloaded methods.
92. Constructors are not inherited so it is not possible to override them.
93. A constructor body can include a return statement provided no value is returned.
94. A constructor never returns a value. If you specify a return value, the JVM (Java virtual
machine) will interpret it as a method.
95. If a class contains no constructor declarations, then a default constructor that takes no
arguments is supplied. This default constructor invokes the no-argument constructor of the
super class via super() call (if there is any super class, except java.lang.Object).
96. 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.
97. To refer to a field or method in the outer class instance from within the inner class, use
Outer.this.fieldname.
98. Inner classes may not declare static initializers or static members unless they are compile
time constants i.e. static final var = value;
99. A nested class cannot have the same name as any of its enclosing classes.
100. Inner class may be private, protected, final, abstract or static.
101. 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();
}
}
102. Classes defined in methods can be anonymous, in which case they must be instantiated at
the same point they are defined. These classes cannot have explicit constructor and may
implement interface or extend other classes.
103. One class in Java cannot reside under two packages. If no package declaration is made, a
class is assumed to be the member of default or unnamed package.
SECTION 7: THREADS
104. The Thread class resides in java.lang package and so need not be imported.
105. A Java thread scheduler can be preemptive or time-sliced, depending on the design of the
JVM.
106. The sleep and yield methods of Thread class are static methods.
107. 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.
108. There are two ways to provide the behavior of a thread in Java: extending the Thread class
or implementing the Runnable interface.
109. The only method of Runnable interface is "public void run();".
110. New thread takes on the priority of the thread that spawned it.
111. Using the synchronized keyword in the method declaration, requires a thread obtain the lock
for this object before it can execute the method.
112. A synchronized method can be overridden to be not synchronized and vice versa.
113. In Java terminology, a monitor (or semaphore) is any object that has some synchronized
code.
114. Both wait() and notify() methods must be called in synchronized code.
115. The notify() method moves one thread, that is waiting on this object's monitor, into the
Ready state. This could be any of the waiting threads.
116. The notifyAll() method moves all threads, waiting on this object's monitor into the Ready
state.
117. Every object has a lock and at any moment, that lock is controlled by at most one single
thread.
118. 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 ({}).
119. Deadlock a special situation that might prevent a thread from executing. In general terms, if
a thread blocks because it is waiting for a condition to arise and something else in the
program makes it impossible for that condition to arise, then the thread is said to be
deadlocked.
SECTION 8: FUNDAMENTAL CLASSES IN THE JAVA.LANG PACKAGE
120. java.lang package is automatically imported in every java source file regardless of explicit
import statement.
121. The String class is a final class, it cannot be sub classed.
122. The Math class has a private constructor, it cannot be instantiated.
123. The random() method of Math class in Java returns a random number, a double, greater than
or equal to 0.0 and less than 1.0.
124. 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.
125. The + and += operators are the only cases of operator overloading in Java and is applicable
to Strings.
126. The various methods of java.lang.Object are
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString and wait.
127. The Java.lang.System is a final class and cannot be sub classed.
128. A String in Java is initialized to null, not empty string and an empty string is not same as a
null string.
129. The equals() method in String class compares the values of two Strings while == compares
the memory address of the objects being compared.
e.g. String s = new String("test"); String s1 = new String("test");
s.equals(s1) will return true while s==s1 will return false.
130. The valueOf() method converts data from its internal format into a human-readable form. It
is a static method that is overloaded within String class for all of Java's built-in types, so that
each type can be converted properly into a string.
131. All the wrapper class objects are immutable. Wrapper classes are final also.
132. java.lang.Character has only a single constructor that takes a char (character) as an
argument.
133. hashCode() produces a hash code of type int for an object that is typically used as an offset
from the start of the memory that has been allocated to that object
SECTION 9: THE COLLECTIONS FRAMEWORK
134. Collections in Java (sometimes known as bag or multiset) can only hold object references not
primitive values. But arrays in Java can hold both primitive values and object references.
135. Collection is an Interface whereas Collections is a helper class.
136. Collection imposes no order nor restrictions on content duplication.
137. The main difference between Vector and ArrayList is that Vector is synchronized while the
ArrayList is not.
138. A Set is a collection, which cannot contain any duplicate elements and has no explicit order to
its elements.
139. A List is a collection, which can contain duplicate elements, and the elements are ordered.
140. Maps use unique keys to facilitate lookup of their contents through key/value pairs.
141. Set and List extend java.util.Collection interface, but Map has no super interface. So Map
does not implement the Collection interface though it is a part of the Java's collection
framework.
142. A Hashtable object is thread-safe (like Vector and unlike HashMap) and do not accept null as
a key.
143. TreeSet class implements SortedSet interface (sub interface of Set) that holds its elements
sorted in an order. In the same way, TreeMap class implements SortedMap interface (sub
interface of Map) that keeps its elements sorted in an order.