NASKAR

Monday, June 05, 2006

Java Notes:

1. The singly rooted hierarchy in Java
The advantage s are:
1. All objects in a singly rooted hierarchy have interfcase in common.
2. The singly rooted hierarchy makes it easier to implement garbage collector.
3. A singly rooted hierarchy, along with creating all objects on the heap, greatly simplifies argument passing..... don't kow how though


2. Where storage of object lives
It’s useful to visualize some aspects of how things are laid out while the program is running—in particular how memory is arranged. There are six different places to store data:

a. Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the processor. However, the number of registers is severely limited, so registers are allocated by the compiler according to its needs. You don’t have direct control, nor do you see any evidence in your programs that registers even exist.

b. The stack. This lives in the general random-access memory (RAM) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to registers. The Java compiler must know, while it is creating the program, the exact size and lifetime of all the data that is stored on the stack, because it must generate the code to move the stack pointer up and down. This constraint places limits on the flexibility of your programs, so while some Java storage exists on the stack—in particular, object references—Java objects themselves are not placed on the stack.

c. The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The nice thing about the heap is that, unlike the stack, the compiler doesn’t need to know how much storage it needs to allocate from the heap or how long that storage must stay on the heap. Thus, there’s a great deal of flexibility in using storage on the heap. Whenever you need to create an object, you simply write the code to create it by using new, and the storage is allocated on the heap when that code is executed. Of course there’s a price you pay for this flexibility. It takes more time to allocate heap storage than it does to allocate stack storage (if you even could create objects on the stack in Java, as you can in C++).

d. Static storage. “Static” is used here in the sense of “in a fixed location” (although it’s also in RAM). Static storage contains data that is available for the entire time a program is running. You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage.

e. Constant storage. Constant values are often placed directly in the program code, which is safe since they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM), in embedded systems.

f. Non-RAM storage. If data lives completely outside a program, it can exist while the program is not running, outside the control of the program. The two primary examples of this are streamed objects, in which objects are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects are placed on disk so they will hold their state even when the program is terminated. The trick with these types of storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a regular RAM-based object when necessary. Java provides support for lightweight persistence, and future versions of Java might provide more complete solutions for persistence.

3. "==" operator in Java compares Object reference and if we really wants to compare the actual content of the object we should use "equals()".

4. A class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class itself must be qualified as abstract.

5. Inheritance vs Composition

6. Difference between abstract and interface.
a. Declare an interface using the keyword interface.
b. An interface may extend zero or more interfaces if it likes, but it cannot extend a class, because then it would inherit functionality, and interfaces cannot have functionality. They can only talk about it.
c. Interface methods cannot be static.
d. Interface methods are implicitly abstract. For that reason, you cannot mark them final (duh), synchronized, or native because all of these modifiers tell how you're going to implement the method, and you're voluntarily giving up that ability when you write the method in an interface.
All interface methods are implicitly abstract and public, regardless of what you write in the interface definition! It is true. The interface method declaration void biteIt(int times), despite its apparent access level of default, actually has public access. Try it. If you write a class in another package beyond the visibility of default access, and include the seemingly legal implementation of
void biteIt(int times) { ; },
the compiler will tell you that you cannot reduce the visibility of the method from public to default. They're all abstract; they're all public.
An interface can define variables. But all variables defined in an interface must be declared public, static, and final. Many Java programmers have adopted the practice of defining only variables within an interface and putting constants in it. This works to get at shared constants, but it is a workaround and is no longer necessary if you're using J2SE SDK 5.0. It features a new static import facility that allows you to import constants just as you would a class or package.
It should be obvious by now that an interface cannot implement another interface or a class.
You may modify your methods using the keyword abstract if you like, but it will have no effect on compilation. Methods in an interface are already abstract, and the Java Language Specification says that its use in interfaces is obsolete.

Likewise, the interface itself is already abstract. So you can do this if you want: public abstract interface Chompable {}. But there's no point; it's redundant.
Interfaces have default access by default (!). So this is legal: interface Chompable { }. But if you want your interface to have public access, use that modifier in the interface declaration.
You cannot declare an interface as having private access. It doesn't make any sense. No one could implement it. So private interface Chompable { } gets you a compiler error for your trouble.
public, static, and final are implicit on all field declarations in an interface.
There are some weird things to keep in mind.
Interfaces can be declared private or protected if they are declared nested inside another class or interface. The following will compile, though its usefulness is dubious at best.
public class interface test
{
private interface myinterface{ }
}

Only an inner class of the class of which the interface is declared can implement the interface.

7: . Can an anonymous class be declared as implementing an interface and extending a class?
Answer: An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.
8. What's the purpose of the Runtime class?
9. What's the purpose of the system class?
10. Difference between Hashtable and Hashmap?

11. Which class should you use to obtain design information about an object?
The Class class is used to obtain information about an object's design.

12. Difference between equals() and == operator in Java
The equals() method compares the characters that make up String objects. It performs this comparison by putting the characters of the String objects into two char arrays and comparing the two arrays. The method returns true if all the elements of the char arrays match and false otherwise.
The == operator compares two object references to see whether they refer to the same instance. A pool of strings, initially empty, is maintained privately by the class String. All literal strings and string-valued constant expressions are interned there. You can also add your own strings to this pool by calling the intern() method. If the pool already contains a string equal to your String object, as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, your String object is added to the pool and a reference to it is returned. It follows that, for any two strings s and t, s.intern()==t.intern() is true if and only if s.equals(t) is true.
To sum up: The equals() method determines whether two strings have the same characters, whereas the == operator determines if two operands refer to the same String object. == actually tests to see if the references in the variables point to the same memory address.

13. Why is String immutable and final?
the contents of the String class cannot be changed. any method that you might expect to modify the contents actually returns a new instance.
if you don't like this behaviour you might think of sub-classing String to provide methods that do alter the contents. but this isn't possible because the class is "final".
to see why, i think you have to look at how java passes arguments. machine-level values - ints, floats and the like - are passed by value. if you call foo.bar(i) then modifying the passed integer within the bar method won't alter the value of i (of course!).
however, if we pass an object into a method, where the object's contents are changed, then the effect will be seen outside the call. this is passing by reference:
class Foo { void changeStack( Stack inside ) { inside.pop( ) ; } }
Stack s ; s.push( new Object( ) ) ; System.out.println( s.size( ) ) ; // here s has size 1 Foo f = new Foo( ) ; f.changeStack( s ) ; System.out.println( s.size( ) ) ; // here s has size 0
again, this is obvious, but note that it is different to the way in which the machine-level values behave.
so how does this apply to String? unlike ints, literal strings (sequences of characters in quotes) are not machine-level values - they are String objects. so, in theory, passing "a quoted string" to a method might result in the string changing value!
class FooFoo { void changeString( String inside ) { inside.append( " world" ) ; // pretend this changes the String... } }
String s = "hello" ; Foo f = new Foo( ) ; f.changeString( s ) ; System.out.println( s ) ; // ...then this would be "hello world"...
f.changeString( "strange" ) ; // ...and what happens here?!
to avoid strange things like this last value (where a quoted string would change value) the java designers would either have to have make "quoted strings" machine-level values or introduce the idea of const objects (similar to C++).
instead, the pragmatic solution is to make sure that the Sting class can't change its contents - hence it is immutable and final.
and because String is immutable and final, it behaves as though it is passed by value, not reference.
one final comment: classes like Integer and Float behave in the same way. this avoids confusion, but also suggests that the compiler could make int and Integer equivalent (in the same way a "quoted string" and String are equivalent). this would make the language cleaner - i don't understand why this doesn't happen (i understand the performance advantages, but couldn't the compiler hide these from view, making int a compiler optimisation for the Integer class)?
Question: What is the Set interface?Answer: The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.

14. What is the List interface?
The List interface provides support for ordered collections of objects.

15. What is the difference between the File and RandomAccessFile classes?
The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.

16. Collection classes in Java? Ordered and unordered and collection and duplicate items
17. util.concurrent package in java
18. Inheritance and constructor calling order
19. Exception handling in java
20. What is Class loader in Java
21. Reflection in Java