1.Collections framework has something extra now, lets find out
a. Deque- if you expand it then it will be something like this
D + E +Que ==> Double + Ended + Queue =>Add or remove from either side,what an Idea sir ji :)
Its an interface known as double ended queue and extends Queue.
want to know more? ==>http://java.sun.com/javase/6/docs/api/java/util/Deque.html
b. BlockingDeque- This is little advanced Deque which will not allow deque to be
empty while retreiving data and look for space while adding data. Its again an
interface which extends Deque and BlockingQueue interfaces.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/concurrent/BlockingDeque.html
c. navigableSet: As the name suggest, it will allow you to navigate in a
sortedSet. Yes it is little extra for SortedSet,it will let you find near by
element for given search targets. It lets your traverse in ascending and
descending order.
want to know more? ==> http://java.sun.com/javase/6
/docs/api/java/util/NavigableSet.html
d. NavigableMap- Its same as above for SortedMap.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/NavigableMap.html
Note: LinkedList,TreeSet,TreeMap have been modified to accomodate new changes as Deque,NavigableSet and NavigableMap respectively.
2. Things which got added newly
a. ArrayDeque- A new dynamic array to support Deque,they can grow upto any size
and faster than stack and linked list if used as stack and linkedlist.
want to know more? ==> http://java.sun.com/javase/6
/docs/api/java/util/ArrayDeque.html
b. newSetFromMap(Map) - This is a powerfull method added in Collections utility
class,it lets you create one full flegded Set from a full fledged Map. No need
to iterate and do it manually.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/Collections.html#newSetFromMap (java.util.Map)
The Arrays utility class now has methods copyOf and copyOfRange that can efficiently resize, truncate, or copy subarrays for arrays of all types.There are other classes and interfaces which have been added but I noted only those what we might use often.
hope it helps :)
Saturday, January 16, 2010
Thursday, January 14, 2010
some Important and basic questions
Q: Can we write a constructor in servlet? if yes why and how?what are the
advantages and disadvantages?
A: Servlets are nothing but a java class with .java extension.As per java rules each class should have a constructor, if you dont have java will create one non arg constructor for you.You can always implement your constructor in your way.Servlets are different class than normal java classes as JVM will not have control over it, only a web container can control servlets, how ever a servlet will not be servlet until container calls it inits method and intilize it, so even though you have defined your constructor it will not have access to any servlet specific objects/functionality, so it will be useless to have constructor in servlet class.
Q.How to handle(increase/decrease) heap size of JVM?
A.You can use command line argument to do so.
java -Xms=256MB some argument
-Xms ==> initial heap size alloted to JVM
-Xmx ==> maximum heap size alloted to JVM
a tool named as jconsole in java 1.5 + series, with this tool you can find how much memory your program is taking and how many threads are running.
Q. Why every action in servlet programming goes to web.xml, where it is defined?
A.web.xml which is also known as deploy descriptor is the center point for a servlet programming as all mapping we do there only.There is file known as context.xml in conf directory, where we define path of web.xml and content goes like this..
Context
---WatchedResource
-- WEB-INF/web.xml
---/WatchedResource
/Context
Q.A man has a dog as his best friend. how to write this in java ?
A. This question lets you know, how much you knwo in java and this is best example to relate java with real life.
now lets write it in java...
if you watch this question carefully then what is the catchy word for you?
you got it right, "has a" is the catchy word in this..
there are two classes involved in this, one is Man and the other one is Dog, we are referring dog object as best friend of man.
lets write the code now...
public class man{
private Dog bestFriend;
}
how simple was it? its so easy to relate to real life. Man has a dog, referring as best friend and the code expalins it very well.
Q. What is initial capacity of an Array list?
A. 10
Q. What is initial capacity of hash map/table?
A. 16
advantages and disadvantages?
A: Servlets are nothing but a java class with .java extension.As per java rules each class should have a constructor, if you dont have java will create one non arg constructor for you.You can always implement your constructor in your way.Servlets are different class than normal java classes as JVM will not have control over it, only a web container can control servlets, how ever a servlet will not be servlet until container calls it inits method and intilize it, so even though you have defined your constructor it will not have access to any servlet specific objects/functionality, so it will be useless to have constructor in servlet class.
Q.How to handle(increase/decrease) heap size of JVM?
A.You can use command line argument to do so.
java -Xms=256MB some argument
-Xms ==> initial heap size alloted to JVM
-Xmx ==> maximum heap size alloted to JVM
a tool named as jconsole in java 1.5 + series, with this tool you can find how much memory your program is taking and how many threads are running.
Q. Why every action in servlet programming goes to web.xml, where it is defined?
A.web.xml which is also known as deploy descriptor is the center point for a servlet programming as all mapping we do there only.There is file known as context.xml in conf directory, where we define path of web.xml and content goes like this..
Context
---WatchedResource
-- WEB-INF/web.xml
---/WatchedResource
/Context
Q.A man has a dog as his best friend. how to write this in java ?
A. This question lets you know, how much you knwo in java and this is best example to relate java with real life.
now lets write it in java...
if you watch this question carefully then what is the catchy word for you?
you got it right, "has a" is the catchy word in this..
there are two classes involved in this, one is Man and the other one is Dog, we are referring dog object as best friend of man.
lets write the code now...
public class man{
private Dog bestFriend;
}
how simple was it? its so easy to relate to real life. Man has a dog, referring as best friend and the code expalins it very well.
Q. What is initial capacity of an Array list?
A. 10
Q. What is initial capacity of hash map/table?
A. 16
Java VS Real Life
we always treat java as programming languauge, but if you keenly watch, its same as our real life.
Lets take some example:
class Person {
private String name;
priavte int age;
private int height;
public boolean doEat(){
return true;
}
public boolean doSleep(){
return true;
}
public boolean doBreath(){
return true;
}
}
public class Manikesh extends Person{
}
Above example is a normal java class, now lets relate this to our real life.
for a human being, person would be most generic form, when we say person, it might be he or she or anything. So in java,a person would be super class which defines rules.
a Person should have states or attribute as name, age, height etc.... , and person should have behaviuor as well like jumping,eating, sleeping, talking etc...states will have some value and behavour will describe what he does or what he does not.
so now if you see the Person class, the member variables of the class acts as states of a person and all member methods acts as behaviour of Person.
Manikesh is a class which extends Person in simple words Manikesh Is A Person.
when Manikesh extends Person then he will have same states and behaviour.
name=manikesh
age= 28
height= 5.7
same will go for behaviour.so any class which extends Person is a human being and will have same states and behavious where values might differ.
so here Manikesh is one instance of Person.
There are two simple key words in java IsA and HasA .
IsA examples:
Manikesh IsA/extends Person
Mango IsA/extends Fruits
Car IsA/extends Vehicle
hasA examples:
Manikesh hasA Car/bike/nose etc...
Car hasA wheels/engine etc...
Wrong combinations for IsA and hasA
car extends garage
manikesh extends car
in other words
car isA garage |-- These two statements are wrong, they dont clear isA test.
manikesh isA car |
Lets take some example:
class Person {
private String name;
priavte int age;
private int height;
public boolean doEat(){
return true;
}
public boolean doSleep(){
return true;
}
public boolean doBreath(){
return true;
}
}
public class Manikesh extends Person{
}
Above example is a normal java class, now lets relate this to our real life.
for a human being, person would be most generic form, when we say person, it might be he or she or anything. So in java,a person would be super class which defines rules.
a Person should have states or attribute as name, age, height etc.... , and person should have behaviuor as well like jumping,eating, sleeping, talking etc...states will have some value and behavour will describe what he does or what he does not.
so now if you see the Person class, the member variables of the class acts as states of a person and all member methods acts as behaviour of Person.
Manikesh is a class which extends Person in simple words Manikesh Is A Person.
when Manikesh extends Person then he will have same states and behaviour.
name=manikesh
age= 28
height= 5.7
same will go for behaviour.so any class which extends Person is a human being and will have same states and behavious where values might differ.
so here Manikesh is one instance of Person.
There are two simple key words in java IsA and HasA .
IsA examples:
Manikesh IsA/extends Person
Mango IsA/extends Fruits
Car IsA/extends Vehicle
hasA examples:
Manikesh hasA Car/bike/nose etc...
Car hasA wheels/engine etc...
Wrong combinations for IsA and hasA
car extends garage
manikesh extends car
in other words
car isA garage |-- These two statements are wrong, they dont clear isA test.
manikesh isA car |
Sunday, January 10, 2010
Interface
Many people get confused with what is interface, many treat it as a class, some treat as a class which has only abstract method, which is correct though.
I give you some real time example to define interface.
Interface is basically a contract which any class signs when it implements that.
for example, when you join a company, you have to sign the contract after which only you will be treated as infosysian,wiporian or what ever it is.
so what is there in contract, its just the rules which is going to make you infosysian. Rules are declared there, not defined.
eg.
1. your working time will be 9-6.
2. your attire should be formal.
3.your salary will be this much etc...
so you see that all the rules are only declared in contract, which will be implemented by you when you sign it.You will be bound to implement all rules, declared there thats what signing of contract says.
Interface is also an contract which says, if you implement me, you have to implement all the rules declared inside.
We use interface for necessery and uncommon behaviour.
Neccessery because every one has to defined all the methods declared inside interface.
uncommon because each class might have there own definition.
for example.
if Human is a class then common and uncommon behaviour would be.
common.
doSleep()
doEat()
doBreath()
uncommon;
doTalk() --> he might be dumb
doSee() --> might be blind
see here you can see that its good to put uncommon behavoiur into interface as evey human has to deifne those methods but difinition might be diff.
lets see the code.
public interface Human{
public boolean doSee();
public boolean doSpeak();
}
public class Rakesh implements Human{
public boolean doSee(){
return true;
}
public boolean peak(){
return false;
}
}
I give you some real time example to define interface.
Interface is basically a contract which any class signs when it implements that.
for example, when you join a company, you have to sign the contract after which only you will be treated as infosysian,wiporian or what ever it is.
so what is there in contract, its just the rules which is going to make you infosysian. Rules are declared there, not defined.
eg.
1. your working time will be 9-6.
2. your attire should be formal.
3.your salary will be this much etc...
so you see that all the rules are only declared in contract, which will be implemented by you when you sign it.You will be bound to implement all rules, declared there thats what signing of contract says.
Interface is also an contract which says, if you implement me, you have to implement all the rules declared inside.
We use interface for necessery and uncommon behaviour.
Neccessery because every one has to defined all the methods declared inside interface.
uncommon because each class might have there own definition.
for example.
if Human is a class then common and uncommon behaviour would be.
common.
doSleep()
doEat()
doBreath()
uncommon;
doTalk() --> he might be dumb
doSee() --> might be blind
see here you can see that its good to put uncommon behavoiur into interface as evey human has to deifne those methods but difinition might be diff.
lets see the code.
public interface Human{
public boolean doSee();
public boolean doSpeak();
}
public class Rakesh implements Human{
public boolean doSee(){
return true;
}
public boolean peak(){
return false;
}
}
Subscribe to:
Comments (Atom)