java softwares
Search This Blog
Friday, February 10, 2012
Monday, February 6, 2012
drfrence between hashset set
A Set
represents a generic "set of values". A TreeSet
is a set where the elements are ordered, a HashSet
is a set where the elements are not ordered.
A HashSet
is typically a lot faster than a TreeSet
.
A TreeSet
is typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree - I've not validated the actual implementation of sun/oracle's TreeSet
), whereas a HashSet
uses Object.hashCode()
to create an index in an array. Access time for a red-black tree is O(log(n))
whereas access time for a HashSet
ranges from constant-time to the worst case (every item has the same hashCode) where you can have a linear search time O(n)
.
defrence between list and linked list
Another difference will be seen when you iterate through the containers. |
defrence between vector and arry
Difference between Vector and ArrayList in java?
java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.
All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.
Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.
Vector or ArrayList? Which is better to use in java?
In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used.
Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.
When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.
When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.
Defrent type of creation of object
CreateObjectDemo
program creates an object and assigns it to a variable: Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100);
The first line creates an object of the Point
class, and the second and third lines each create an object of the Rectangle
class.
Each of these statements has three parts (discussed in detail below):
- Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
- Instantiation: The new keyword is a Java operator that creates the object.
- Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Declaring a Variable to Refer to an Object
Previously, you learned that to declare a variable, you write:
type name;
This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.
You can also declare a reference variable on its own line. For example:
Point originOne;
If you declare originOne
like this, its value will be undetermined until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object. For that, you need to use the new
operator, as described in the next section. You must assign an object to originOne
before you use it in your code. Otherwise, you will get a compiler error.
A variable in this state, which currently references no object, can be illustrated as follows (the variable name, originOne
, plus a reference pointing to nothing):
Main method overriding
actually when i am reding a tutorial i got this doubt. i browse for this. i get tht u can overload the main methond. but i didnot get the correct answere for overriding main method. some body saying it is possible with example and some body saying not possible because it is static. plz check this example and clear my doubt.
class Checkmain
{
public static void main(String args[])
{
System.out.println("helloCheckmain string main ");
}
}
class Checkmain1 extends Checkmain
{
public static void main(String args[])
{
System.out.println("helloCheckmain1 string main ");
}
}
public class Main
{
public static void main(String args[])
{
String S[]=new String[10] ;
System.out.println("******string main**********");
int q=10;
main(q);
Checkmain.main(S);
Checkmain1.main(S);
}
public static void main(int a
{
String S[]=new String[10] ;
System.out.println("000000000 int main 0000000000");
Checkmain.main(S);
Checkmain1.main(S);
}
}
First clear my doubt after i will think abt use of this feature.
Main method overloading
Each method has a signature, which comprises the name of the method and the types and order of the parameters in the formal
parameter list. Several method implementations may have the same name, as long as the method signatures differ. Since overloaded methods have the same name, their parameter lists must be different.
so if your method is
public static void main(String args[]){
}
public static int main(int i){
}
both methods signature is different.