Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:05:48+00:00 2026-06-08T11:05:48+00:00

Possible Duplicate: Java String.equals versus == I thought this would be a neat way

  • 0

Possible Duplicate:
Java String.equals versus ==

I thought this would be a neat way of structuring a picker method but the output is not going to the first two if statements and only outputs the last

    public int myPickerMethod(){

        System.out.println("please select from the options ");
        System.out.println("please select 1 for option 1 ");
        System.out.println("please select 2 please select 2 for option 2");
        String input = keyboard.readLine();
        System.out.println("input = " + input);     

        if(input=="1"){

                return 1;
        }
        else if(input=="2"){
            return 2;
        }
        else{
            return 42;
        }
   }

Here is my result from the terminal:

   please select from the options 
   please select 1 for option 1 
   please select 2 please select 2 for option 2
   1
   input = 1
   response = 42

Same goes if I put 2 in. the “response” print statement is the output from the method from a print statement in the main class.

I have not tried this way before but I figured it should work. I don’t really get why it isn’t. Anyone able to clear this up? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:05:50+00:00Added an answer on June 8, 2026 at 11:05 am

    For testing Object (non-primitive type) equality, use Object.equals().

    if(input.equals("1")) {
    
        return 1;
    }
    

    The == operator checks whether the references to the objects are equal. A test for reference equality is done within the String.equals() method, among other checks. Below is the Java source for the String.equals() method:

    public boolean equals(Object anObject) {
         if (this == anObject) {      // Reference equality
             return true;
         }
         if (anObject instanceof String) {
             String anotherString = (String)anObject;
             int n = count;
             if (n == anotherString.count) {  // Are the strings the same size?
                 char v1[] = value;
                 char v2[] = anotherString.value;
                 int i = offset;
                 int j = anotherString.offset;
                 while (n-- != 0) {
                     if (v1[i++] != v2[j++])        // Compare each character
                         return false;
                 }
                 return true;
             }
         }
         return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java String.equals versus == I know it' a dumb question but why
Possible Duplicate: Java String.equals versus == I am using an if statement in java
Possible Duplicate: Java: Global Exception Handler I thought of something like this: public static
Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: In Java, is there a way to write a string literal without
Possible Duplicate: Java: convert List<String> to a join()d string Having this: List<String> elementNames =
Possible Duplicate: How would you convert a String to a Java string literal? Is
Possible Duplicates: Java String.equals versus == whats the difference between ".equals and ==" public
Possible Duplicate: Java string comparison? I was trying to do this: boolean exit =
Possible Duplicate: Difference Between Equals and == String comparison and String interning in Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.