Avatar of BofADev
BofADev

asked on 

Irritating problem with a easy easy concept

Take a look at this code

    private boolean ParseResponse(String strResponse) throws InvalidResponseException{
        strResponse = strResponse.toUpperCase();
        if (strResponse == "Y" || strResponse == "N"){
            if (strResponse == "Y"){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            System.out.println(strResponse);
            throw new InvalidResponseException();
        }
    }

When the method is called I pass it a string with the value of y.  The method then converts it to upper case Y.  Why is this statement failing and going to the else? if (strResponse == "Y" || strResponse == "N")

Thanks for your help.
Java

Avatar of undefined
Last Comment
Mayank S
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of BofADev
BofADev

ASKER

Now it makes sense!  I was thinking last night...how stupid of Java.  But I remember now that the memory address of a string just holds the memory address of the actual text.  That would make sense why it isn't evaluating the way I think it should.
Avatar of Mayank S
Mayank S
Flag of India image

>> ...how stupid of Java.

Its not stupid :) its useful.

https://www.experts-exchange.com/questions/20898574/Java-Objects.html#10458980
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> But I remember now that the memory address of a string just holds the memory address of the actual text.

== compares the memory address of the object, not the address of the text. they are different things really
Avatar of Mayank S
Mayank S
Flag of India image

Somehow that recommendation was not followed.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo