Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 10th, 2004, 01:42 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default JSP 2.0 p459 errata Chapter 12

Hi-

The code on 459 is:

      if(user.getUserName() == temp.getUserName()
         && user.getPassword() == temp.getPassword())

when I changed it to:

      if (user.getUserName().equals(temp.getUserName())
        && user.getPassword().equals(temp.getPassword()) )

the test suceeded.

Here is an excerpt from a faq from javacoffeebreak.com that explains why what is written in JSP 2.0 doesn't work:

When we compare primitive data types, such as two ints, two chars, two doubles, etc. we can use the == operator. We can also use the == operator to compare two objects. However, when used with an object, the == operator will only check to see if they are the same objects, not if they hold the same contents.

This means that code like the following will not correctly compare to strings :

if ( string1 == string2 )
{
    System.out.println ("Match found");
}
This code will only evaluate to true if string1 and string2 are the same object, not if they hold the same contents. This is an important distinction to make. Checking, for example, to see if
aString == "somevalue", will not evaluate to true even if aString holds the same contents.

To correctly compare two strings, we must use the .equals method(). This method is inherited from java.lang.Object, and can be used to compare any two strings. Here's an example of how to correctly check a String's contents :

if ( string1.equals("abcdef") )
{
    System.out.println ("Match found");
}
This is a simple, and easy to remember tip that will safe you considerable time debugging applications. Remember - never use the == operator if you only want to compare the string's contents.

Cheers,

Newtonian






Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP 2.0 p459 logic errata Chapter 12 newtonian JSP Basics 0 March 11th, 2004 01:18 AM
JSP 2.0 p457 errata Chapter 12 newtonian JSP Basics 0 March 10th, 2004 02:04 PM
JSP 2.0 p458 errata Chapter 12 newtonian JSP Basics 0 March 10th, 2004 01:53 PM
JSP 2.0 p484 errata newtonian JSP Basics 3 February 23rd, 2004 10:56 PM
JSP 2.0 p492 errata newtonian JSP Basics 0 July 24th, 2003 12:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.