Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: String comparison


Message #1 by "Takuya Matsumoto" <takuya@e...> on Sat, 31 Mar 2001 12:55:23 +0100
The way == works with Strings is to check whether they are the same object,
not whether their contents are the same, for which you would normally use
equals(). There is an interesting way around this though - String's intern()
method. There's more info on intern() in the Javadoc for String, but this
should give you an idea of what you can do with it :

class Test {
  static void main(String[] args) {

	String a = "qwerty";
	String b = "qwerty";

	System.out.println(a==b); // true

	String c = new String("qwerty");
	String d = new String("qwerty");

	System.out.println(c==d); // false

	System.out.println(c.equals(d)); // true

	c = d;

	System.out.println(c==d); // true

	String e = new String("qwerty");
	String f = new String("qwerty");

	System.out.println(e==f); // false

	e = e.intern();
	f = f.intern();

	System.out.println(e==f); // true
  }
}



---
Danny Ayers
http://www.isacat.net

<- -----Original Message-----
<- From: ravi kiran [mailto:nagamkiran@y...]
<- Sent: 03 April 2001 12:36
<- To: Pro_JavaServer_Pages
<- Subject: [pro_jsp] Re: String comparison
<-
<-
<- Hai
<-
<- If you want to string comparision there is a method
<- "equals" in String class.
<-
<- so you can use as follows.
<-
<- if ( str_EnteredPassword.equals(str_CorrectPassword) )
<- ....
<- ...
<-
<- bye
<-
<- ravi
<-
<-
<- --- Takuya Matsumoto <takuya@e...> wrote:
<- > Hello,
<- >
<- > I have been doing ASP for some time, and am now
<- > starting JSP.
<- > I am at the moment writing a simple login page,
<- > however the string
<- > comparision for some reason just wouldn't work for
<- > me.
<- >
<- > When they are output at the end of the page, they
<- > look identical.  I also
<- > made sure that they are the same variable type by
<- > storing both in String.
<- > However it just keeps giving me "0".
<- >
<- > I would very much appreciate if someone could have a
<- > look.
<- >
<- > Thanks very much.
<- >
<- > Takuya Matsumoto
<- >
<- >
<- > <%
<- > String str_EnteredPassword 
<- > request.getParameter("str_Password");
<- > String str_CorrectPassword = "test";
<- >
<- > if ( str_EnteredPassword == str_CorrectPassword )
<- > {
<- > 	out.println("1<BR>");
<- > 	session.setAttribute("bit_Pass", "1");
<- > }
<- > else
<- > {
<- > 	out.println("0<BR>");
<- > }
<- > out.println(str_EnteredPassword+"<BR>");
<- > out.println(str_CorrectPassword+"<BR>");
<- > %>
<- >
<- >
<-
<- Bye from nagam until next mail, till then watch at
http://nagamkiran.tripod.com/


  Return to Index