While performing concatenation of strings, is it OK to use StringBuffer or String.
Which of the following syntaxes is more efficient ? Why ?
StringBuffer sbName=new StringBuffer();
sbName.append(fName);
sbName.append(lName);
(OR)
String sName=new String();
sName=sName+fName;
sName=sName+lName;
ANS) Using StringBuffer is much more efficient than using '+' to
concatenate strings because the '+' operator creates a new String
instance each.
Just remember that if you have the following statement...
String s = "a" + "b" + "c";
...the compiler will actually optimize this code segment for you and
use StringBuffer.append() automatically. The compiler will only do
this for a single statement at a time; it will not combine statments.
StringBuffer will always be quicker than '+' because it's a method
invocation as opposed to a new object instantiation.
Try it out yourself. Create a loop that appends a string to itself 100
times using '+'. Then do the same thing using StringBuffer.append()
and compare the times. You'll be surprised how much faster append() is.
Ask Rajul
Friday, February 4, 2011
What is callback and how it is achieved in the java?
A Callback function is a method that is passed into a method, and
called at some point by the method to which it is passed.
This is very useful for making reusable software. For example, many
operating system APIs (such as the Windows API) use callbacks heavily.
For example, if you wanted to work with files in a folder - you can
call an API function, with your own routine,
and your routine gets run once per file in the specified folder. This
allows the API to be very flexible.
Callback mechanisms are often implemented via function pointers,
functor objects, or callback objects.
Java's support of interfaces provides a mechanism by which we can get
the equivalent of callbacks.
The trick is to define a simple interface that declares the method we
wish to be invoked.
Here is as sample program which :
http://javawithrajul.googlegroups.com/web/CallbackTest.java
Regards
Rajul konkar
called at some point by the method to which it is passed.
This is very useful for making reusable software. For example, many
operating system APIs (such as the Windows API) use callbacks heavily.
For example, if you wanted to work with files in a folder - you can
call an API function, with your own routine,
and your routine gets run once per file in the specified folder. This
allows the API to be very flexible.
Callback mechanisms are often implemented via function pointers,
functor objects, or callback objects.
Java's support of interfaces provides a mechanism by which we can get
the equivalent of callbacks.
The trick is to define a simple interface that declares the method we
wish to be invoked.
Here is as sample program which :
http://javawithrajul.googlegroups.com/web/CallbackTest.java
Regards
Rajul konkar
Difference between HashMap and HashTable? Can we make
1. The HashMap class is roughly equivalent to Hashtable, except that
it is unsynchronized and permits nulls. (HashMap allows null values as
key and value whereas Hashtable doesn’t allow nulls).
2. HashMap does not guarantee that the order of the map will remain
constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. Iterator in the HashMap is fail-safe while the enumerator for the
Hashtable isn't.
Note on Some Important Terms
1) Synchronized means only one thread can modify a hash table at one
point of time. Basically, it means that any thread before performing
an update on a hashtable will have to acquire a lock on the object
while others will wait for lock to be released.
2) Fail-safe is relevant from the context of iterators. If an iterator
has been created on a collection object and some other thread tries to
modify the collection object "structurally”, a concurrent modification
exception will be thrown. It is possible for other threads though to
invoke "set" method since it doesn’t modify the collection
"structurally”. However, if prior to calling "set", the collection has
been modified structurally, "IllegalArgumentException" will be thrown.
HashMap can be synchronized by Map m = Collections.synchronizeMap
(hashMap);
it is unsynchronized and permits nulls. (HashMap allows null values as
key and value whereas Hashtable doesn’t allow nulls).
2. HashMap does not guarantee that the order of the map will remain
constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. Iterator in the HashMap is fail-safe while the enumerator for the
Hashtable isn't.
Note on Some Important Terms
1) Synchronized means only one thread can modify a hash table at one
point of time. Basically, it means that any thread before performing
an update on a hashtable will have to acquire a lock on the object
while others will wait for lock to be released.
2) Fail-safe is relevant from the context of iterators. If an iterator
has been created on a collection object and some other thread tries to
modify the collection object "structurally”, a concurrent modification
exception will be thrown. It is possible for other threads though to
invoke "set" method since it doesn’t modify the collection
"structurally”. However, if prior to calling "set", the collection has
been modified structurally, "IllegalArgumentException" will be thrown.
HashMap can be synchronized by Map m = Collections.synchronizeMap
(hashMap);
Is really multiple inheritance supported by Java ?
Is java really supports multiple inheritance using interfaces ? Because if we implement multiple interfaces in a class then we are inheriting nothing from parent class, because nothing concrete in interface. So can we consider it as inheritance in pure OO term ?
------------
Inheritance is a mechanism to achieve the following:
1. Type inheritance - the derived class IS-A base class
2. Behavior inheritance - the derived class exposes at least the same behavior as the base class. It can exhibit more behavior.
3. State inheritance - the derived class has at least the same state states as the parent
Inheritance using interfaces allows only the first two.
So, in pure OO terminology, java doesnt have multiple inheritance.
Just in case you were wondering about the first sentence, Mixin is another approach at achieving points 2 and 3 leaving out 1.
------------
Inheritance is a mechanism to achieve the following:
1. Type inheritance - the derived class IS-A base class
2. Behavior inheritance - the derived class exposes at least the same behavior as the base class. It can exhibit more behavior.
3. State inheritance - the derived class has at least the same state states as the parent
Inheritance using interfaces allows only the first two.
So, in pure OO terminology, java doesnt have multiple inheritance.
Just in case you were wondering about the first sentence, Mixin is another approach at achieving points 2 and 3 leaving out 1.
Subscribe to:
Posts (Atom)