 |
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. |
 |
There are three top-level elements that may appear in a file. None of these elements is required. If they are present, they must appear in the following order:
- package declaration
- import statements
- class definitions
|
 |
Static method can't be overridden to non-static and vice versa. |
 |
The variables in Java can have the same name as a method or a class. |
 |
All the static variables are initialized when a class is loaded. |
 |
An interface can extend more than one interface, while a class can extend only one class. |
 |
The variables in an interface are implicitly final and static. If the interface, itself, is declared as public then the methods and variables are implicitly public. |
 |
A final class cannot have abstract methods. |
 |
All methods of a final class are automatically final. |
 |
While casting one class to another, subclass to superclass is allowed without any type casting.
For example, 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.
For example, 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 that a new instance is created.
But, the StringBuffer class is mutable. |
 |
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 that 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++; is that
in the first case y will be incremented first and then assigned to x while in second case first y will be assigned to x then it will be incremented. |
 |
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 reference(of any object) = null. |
 |
An overriding method may not throw a checked exception unless the overridden method also throws that exception or a superclass of that exception. |
 |
Interface methods can't be native, static, synchronized, final, private, protected, or abstract. |
 |
The String class is a final class, it cannot be subclassed. |
 |
The Math class has a private constructor, it cannot be instantiated. |
 |
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 |
 |
The various methods of Java.lang.Object are:
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, and wait. |
 |
Garbage collection in Java cannot be forced. The methods used to call garbage collection thread are System.gc() and Runtime.gc(). |
 |
Inner class may be private, protected, final, abstract, or static. |
 |
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 that of any of its enclosing classes. |
 |
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();
}
} |
 |
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. |
 |
Using the synchronized keyword in the method declaration requires that 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. |
 |
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 notify() method moves one thread, which 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. |
 |
The argument to switch can be either byte, short, char, or int. |
 |
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>;) continues execution with the next iteration of the labeled loop. |
 |
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 throw FileNotFoundException. In earlier versions of Java, FileOutputStream() threw an IOException when an output could not be created. |
 |
A static method can only call static variables or other static methods, without using the instance of the class. For example, main() method can't directly access any non-static method or variable, but using the instance of the class it can. |
 |
The if() statement in Java takes only boolean as an argument. Please note that if (a=true){}, provided a is of type boolean, is a valid statement and the code inside the if block will be executed. |
 |
(-0.0 == 0.0) will return true, while (5.0==-5.0) will return false. |
 |
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. |
 |
The default Layout Manager for Panel and Applet is Flow. For Frame and Window it's BorderLayout. |
 |
The FlowLayout always honors a component's preferred size. |
 |
The statement float f = 5.0; will give compilation error as default type for floating values is double and double can't be directly assigned to float without casting. |
 |
The equals() method in String class compares the values of two Strings while == compares the memory address of the objects being compared.
For example, String s = new String("test"); String s1 = new String("test");
s.equals(s1) will return true while s==s1 will return false. |
 |
The example of array declaration along with initialization - int k[] = new int[]{1,2,3,4,9}; |
 |
The octal number in Java is preceded by 0 while the hexadecimal by 0x (x may be in small case or upper case)
For example:
octal :022
hexadecimal :0x12 |
 |
A constructor cannot be native, abstract, static, synchronized, or final. |