Posts

Showing posts from 2009

Language Fundamental

The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember " b ig  f arms  n eed r ed  t ractors".  Variables declared inside of a block or method are called local variables; they are not automatically initialized. The compiler will generate an error as a result of the attempt to access the local variables before a value has been assigned.  

Arrays

An uninitialised Object that is an array element will be set to null by default. If we write: String s[][] = new String[2][2]; System.out.println(s[1][1]); System.out.println(s[1][2]); The second call to println because all arrays start from 0, so an array with an element of size 2 has elements 0 and 1 but no element 2.

Operators

Short circuited operator- If the first operand of the (or) operator returns true Java sees no reason to evaluate the second. Whatever the value of the second the overall result will always be true. Ex: boolean b1 = true; if((b1 ==true) place(true)){ Here the method called place is never called.

Collections

Image
Typical Collections Hierarchy is as below In the Collections Framework, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The typical application of map is to provide access to values stored by keys. When designing software with the Collection Framework, it is useful to remember the following hierarchical relationship of the four basic interfaces of the framework. The Collection interface is a group of objects, with duplicates allowed. Set extends Collection but forbids duplicates. List extends Collection also, allows duplicates and introduces positional indexing. Map extends neither Set nor Collection Interface Implementation Historical Set HashSet TreeSet List ArrayList LinkedList Vector Stack Map HashMap Treemap Hashtable Properties How to sort an Arraylist? How to make it synchronized? Collections.sort(arrayList); For syncronizing arraylist we can convert arraylist to V...

Garbage Collection

How objects are stored: The JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program.  Initiating garbage collection: You can directly run the garbage collector whenever you want to and it will be schedule in due time.. by calling Runtime.gc() . The garbage collector informs your object when it is about to be garbage collected. The garbage collector runs in low-memory situations. Eligibility for garbage collection: An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static refrences in other words you can say that an object becomes eligible for garbage collection if its all references are null.  Cyclic dependencies:  Cyclic dependencies are not counted as reference so if Ob...

AWT

When you invoke repaint() for a Component, the AWT package calls  update()  for the Component, which in invokes  paint()  in its default behavior. What does the following line of code do? TextField tf = new TextField(30); TextField defines a constructor that takes the number of columns, as shown in the example. TextField objects can have their text updated at any time, including long after they're created. Which LayoutManager arranges components left to right, then top to bottom, centering each row as it moves to the next? FlowLayout  A component can be resized horizontally, but not vertically, when it is placed in which region of a BorderLayout? North and South only can resize a component horizontally, to the width of the Container How can you place three Components along the bottom of a Container? Set the Container's LayoutManager to be a BorderLayout; add each Component to a different Container that uses a FlowLayout, and then add that...

Exception Handling

Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() methodA checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method. Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be, as they tend to be unrecoverable In the given scenario class First { void test() throws Exception1, Exception2 { . . . } } class Sec...

Strings

Write a line of code to use the String’s substring() method to obtain the substring "lip" from a String instance named s that is set to "tulip". "s.substring(2, 5)" or "s.substring(2)" or "s.substring(2, s.length())"; Only + and += are overloaded for String objects The method substring() starts at the first index, inclusive, with 0 being the first character), and ends at the end index -1 (that is, exclusive of the end index). The StringBuffer class does not override equals(). Hence, the code below returns false when passed two different objects. StringBuffer sb1 = new StringBuffer("hello"); StringBuffer sb2 = new StringBuffer("hello"); if (sb1.equals(sb2)) System.out.println("equal"); else System.out.println("not equal");

Applets

Q- What method name can you use from the applet to read a String passed to an applet via the tag? A -getParameter Q- How to embed an applet class named Q56 into a Web page? A - The tag requires three keywords: code, width, and height  applet code=Q56.class width=100 height=100 Q -How can you retrieve a circle’s radius value that’s passed to an applet? A - public void init() { String s = getParameter("radius");doSomethingWithRadius(s); }

DataTypes

casting a boolean to an int is illegal. An octal number in Java is preceded by a 0. The method with all double parameters is actually the only version of test() that the Java Virtual Machine can legally coerce the numbers to. The reason the other versions of test() are not invoked is that at least one of the parameters would have to be automatically coerced from a type with greater accuracy to a type with less accuracy, which is illegal. class Test { public static void main(String[] args) { Test t = new Test(); t.test(1.0, 2L, 3); } void test(double a, double b, short c) { System.out.println("1"); } void test(float a, byte b, byte c) { System.out.println("2"); } void test(double a, double b, double c) { System.out.println("3"); } void test(int a, long b, int c) { System.out.println("4"); } void test(long a, long b, long c) { System.out.println("5"); } } 3 will be printed You cannot assign an integer to...

Files

RandomAccessFile public RandomAccessFile(File file,String mode) throws FileNotFoundExceptionCreates a random access file stream to read from, and optionally to write to, the file specified by the File argument. A new FileDescriptor object is created to represent this file connection. The mode argument specifies the access mode in which the file is to be opened. The permitted values and their meanings are: "r" - Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown. "rw" - Open for reading and writing. If the file does not already exist then an attempt will be made to create it. "rws" -Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device. "rwd" -Open for reading and writing, as with "rw", and also require that every update to the fi...

Tips n tricks for Threads

Multithreading means multiple lines of a single program can be executed at the same time. The operating system is treating the programs as two separate and distinct processes.  Two ways of creating threads are : implementing an interface and extending a class.  The run() method is where all the work of theCounter class thread is done.  Only classes that implement the Runnable interface (and so are of type Runnable) can be targets of threads. The call to start() will call the target's run() method. The call to start() will return right away and the thread will start executing at the same time. Once a thread is stopped, it cannot be restarted with the start()command, since stop() will terminate the execution of a thread. Instead you can pause the execution of a thread with the sleep()method or temporarily cease the execution using suspend() method and resume() it at later point. Scheduling threads example .  Which exception might wait() throw? "Inter...

Tips to crack the Java classes

Abstract classes: An abstract class is defined using the keyword abstract in front of the class keyword and almost always defines at least one abstract method. An abstract class does not have to define an abstract method. If there are no abstract methods in an abstract class, then any subclasses of the abstract class can be instantiated (as long as they are not, in turn, defined using the abstract keyword). Assigning a subclass type to a superclass type is perfectly legal and will run fine at runtime. You cannot assign an object to a sibling object reference, even with casting. Eg: class Superclass { } class Subclass1 extends Superclass { } class Subclass2 extends Superclass { } Superclass a = new Superclass();  Subclass1 b = new Subclass1(); Subclass2 c = new Subclass2(); b = (Subclass1)c; will not compile. What will happen when you attempt to compile the following code? protected class Wmid{  private Wmid(String sName){   System.out.print...