String handling

can any one explain string handling concept in java?iam not able to understand immutable concept in it.

When you define a String object you cannot modify it after that.

Say:

String s = “ABC”;

s object will always be ABC.

if you do

String s = “ABC”;

s += “DE”;

s will be ABCDE but it will be an other object the old reference of s is lost and will be GC’ed.

For mutable Strings use StringBuffer class.

hi
StringBuffer is used to handle run time changes.
u can add new string with existing value.but the stringbuffeer reffers same memory address and u can get also previous alue

StringBuffer sb=new StringBuffer();
sb.append(“value1”);
//now sb contains value1

sb.append(“value2”);

now sb contains value1,value2

I am having one doubt. What is the difference between Generic Servlet & Http Servlet.?

Strings are constant; their values cannot be changed after they are created. Because String objects are immutable they can be shared.

For example:

 String str = "abc"; 
 
 is equivalent to: 

 char data[] = {'a', 'b', 'c'};
 String str = new String(data);

A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified.

I have doubt . what is difference b/n String and stringBuffer.

Chandru,

The difference between GenericServlet and HttpServlet is that GenericServlet is protocol-independent. Whereas HttpServlet’s are more suited for web sites.

By the way, do feel free to start another thread instead of posting into threads which are not related to your question. Thanks.

Hope this helps.

Martyn

In short GenericServlet is protocol independent, whereas HttpServlet is protocol dependent

In GenericServlets you cannot use Cookies or HttpSession.
Session tracking is not possible, redirection is not possible.
Http servlets overrides doGet and doPost methods. Generic servlet overides service method.

A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).

Both these classes are abstract.

hello i have a doubt abt how to get the field info. in a particular page in to a service method of servlet.

how can i find out ascii value of a char in java

In short GenericServlet is protocol independent, whereas HttpServlet is protocol dependent

GenericServlets is used for small data transfer whereas httpservlets is used for huge data transfer.

In GenericServlets you cannot use Cookies or HttpSession.
Session tracking is not possible, redirection is not possible.
Http servlets overrides doGet and doPost methods. Generic servlet overides service method.

A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).

Both these classes are abstract.

Any particular example where GenericServlet is used and HttpServlets can not be used?

Hi shubhada,

A quote from jGuru on the exact same question:

"GenericServlet is for servlets that might not use HTTP, like for instance FTP servlets. Of course, it turns out that there’s no such thing as FTP servlets, but they were trying to plan for future growth when they designed the spec. Maybe some day there will be another subclass, but for now, always use HttpServlet. "

Martyn

Hi,
Please tell be what is the difference between doGet,doPost,Service Method and when should use for better response.

Hi Hari,

There are plenty of resources on google which explain this architecture. A good article is here:

Hope this helps.

Martyn

class Ascii
{
public static void main(String args)
{
int x = ‘a’;
System.out.println("Ascii value of a = " + x);
}
}
Ascii.java

public class str1 {

/** Creates a new instance of str1 */
public str1() {
}

public static void main(String a[])
{
    String str = "Stanford  ";
    System.out.println(str);
 str += "Lost!!";

System.out.println(str);

}

}

i have tried this code but iprevious reference was not lost so what is actual immutable

I think by concatnating string using s += “aa”, internally, it will create a new string object, which contains the old value cancatnated with new valus.
Pls see this exampls
class JavaCls
{
public static void main(String[] args)
{
String s = “aaaa”;
System.out.println(s.hashCode());
s += “bbbb”;
System.out.println(s.hashCode());

	StringBuffer bu = new StringBuffer("yyyy");
	System.out.println(bu.hashCode());
	bu.append("gggg");
	System.out.println(bu.hashCode());
}

}

In String’s case the hashCode() will be different after concatnation.
In StringBuffer’s case, hashCode() will be same.
In JDK API, it is said that hashCode is almost equal to internal address.
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#hashCode()(“This is typically implemented by converting the internal address of the object into an integer”)

So we can assume that += in String will not change the value of current object(immutable), but StringBuffer can change value of current object(mutable)
Pls currect me if i am wrong.