Search More About Above Topic Here

Custom Search

What are the differences between String, StringBuffer, and StringBuilder? *

Links to this post
The String class is immutable, however StringBuffer and StringBuilder classes are not immutable. There's no reason to use a StringBuffer or StringBuilder unless you're planing on changing the contents.
  • The String class overrides the default equals() method, but StringBuffer and StringBuilder do not override the default equals() method in Object class. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). For example, 
    • public class Program {
        public static void main(String [] args) {
          String s1 = new String("Hello World");
          String s2 = new String("Hello World");
          System.out.println("s1.equals(s2):" + s1.equals(s2));
          StringBuffer sb1 = new StringBuffer("Hello World");
          StringBuffer sb2 = new StringBuffer("Hello World");
          System.out.println("sb1.equals(sb2):" + sb1.equals(sb2));
          StringBuilder sd1 = new StringBuilder("Hello World");
          StringBuilder sd2 = new StringBuilder("Hello World");
          System.out.println("sd1.equals(sd2):" + sd1.equals(sd2));
        }
      }
    The output is

Is it possible to call one constructor from another?

Links to this post
Yes, by using this() syntax. E.g.

public Pet(int id) {
this.id = id; // "this" means this object
}

public Pet (int id, String type) {
this(id); // calls constructor public Pet(int id)
this.type = type; // "this" means this object
}


How to use HashTable?

Links to this post
Hashtable maps one KEY with ONE value.

like
Hashtable numbers = new Hashtable();

numbers.put("1", new Integer(1));
numbers.put("2", new Integer(2));
numbers.put("3", new Integer(3));
numbers.put("4", new Integer(4));

My problem is -- how to map multiple value with singlt KEY

numbers.put("1", new Integer(1));
numbers.put("1", new Integer(2));
numbers.put("1", new Integer(3));
numbers.put("2", new Integer(4));
numbers.put("2", new Integer(1));
numbers.put("2", new Integer(2));
numbers.put("3", new Integer(3));
numbers.put("4", new Integer(4));

How to keep the multiple value with singl KEY... if any one has some code

What are the uses of Java Packages?

Links to this post
It helps resolve naming conflicts when different packages have classes with the same names. This also helps you organize files within your project.

For example:
java.io - package do something related to I/O and
java.net - package do something to do with network and so on.

If we tend to put all .java files into a single package, as the project gets bigger, then it would become a nightmare to manage all your files. You can create a package as follows with package keyword, which is the first keyword in any Java program followed by import statements. The java.lang package is imported implicitly by default and all the other packages must be explicitly imported.

package com.xyz.client ;
import java.io.File;
import java.net.URL;

What do you need to do to run a class with a main() method in a package?

Links to this post
Example: Say, you have a class named "Pet" in a project folder "c:\myProject" and package named com.xyz.client, will you be able to compile and run it as it is?

package com.xyz.client;

public class Pet {

public static void main(String[] args) {

System.out.println("I am found in the classpath");

}

}

To run �� c:\myProject> java com.xyz.client.Pet
Custom Search
Related Posts with Thumbnails