Question

Why is the answer "a is something else!" and NOT "a is 0!" when running the...

Why is the answer "a is something else!" and NOT "a is 0!" when running the program?

String a = "";
a += "0";
if (a == "0") {
    System.out.println("a is 0!");
} else if (a == "1") {
    System.out.println("a is 1!");
} else if (a == "a") {
    System.out.println("a is a!");
} else {
    System.out.println("a is something else!");
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The output is "a is something else!", because every condition is getting false, i. e. else block is getting executed.

The only case where the else block will execute is a is not 0, not 1 or not a.

Thus it is clear that a is something else, but what it is?

The answer is a is " ", a null string. Its because String in Java is immutable, they can not be changed once they are declared.

At very first line we have declared String a = " ", thus it will remain a null string throughout the program.

To changes it, we need to use more advanced features like Java String builder.

Add a comment
Know the answer?
Add Answer to:
Why is the answer "a is something else!" and NOT "a is 0!" when running the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • Question 2 1 point possble (graded) Consider the following code segment. String a- a0 if (a-0")...

    Question 2 1 point possble (graded) Consider the following code segment. String a- a0 if (a-0") else if (a"1") else if (a" else( System.out.printin("a is 0 System.out.printin("a is 1! System.out.println("a is al") System.out.println(a is something else!) Which of the following statements about the code segment is true? It produced "a is 0!" as output It produced "a is 1!" as output It produced "a is a!" as output It produced "a is something else!" as output It results in an...

  • Is the government of your country practicing free trade, protectionism, or something else? Why?

    Is the government of your country practicing free trade, protectionism, or something else? Why?

  • the question, this is not running it throws an exception would you help me it is...

    the question, this is not running it throws an exception would you help me it is a java program // why this is not running would you help me public class sample {     public static void main(String[] args){         String str[][] = new String[10]['K'];                 for(int i= 1;i<=10; i++){             for(char j= 'A';j<='J'; j++){                 System.out.print(i +" "+ j);                  str[i][j] = "A";                   System.out.print(" ");                                                   }              System.out.println();                                  }            ...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

  • Is the Calculation query something you would utilize when running reports?

    Is the Calculation query something you would utilize when running reports?

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • 22. Select the correct answer from the IUPAC names provided: Group of answer choices something else!...

    22. Select the correct answer from the IUPAC names provided: Group of answer choices something else! 1-butene cis-2-butene 1-butyne trans-2-butene Flag this Question Question 238 pts 23. Select the steps to achieve the desired transformation: Group of answer choices 1. Mg/THF 2. bromoethane 3. dilute acid 4. SOBr2 / pyridine / 0 degrees 5. NaCN / DMSO 6. LAH / -78 / THF 7. diluted base more than one correct answer here 1. Mg/THF 2. ethanol 3. dilute acid 4....

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT