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();                                  }            ...

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

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

  • 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...

  • 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 =...

  • Hello Can you help to fix the program. When running, it still show Exception in thread...

    Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap    at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...

  • please help me with this problem The following program uses if-else-if statements to determine a letter...

    please help me with this problem The following program uses if-else-if statements to determine a letter grade. Rewrite it using if statements with logical operators (&&, ||, !) to produce the same output when given the same inputs. import java.util.Scanner; public class GradeCalculator{ public static void main(String[] args){ int grade; Scanner input = new Scanner(System.in); System.out.println("Enter numeric grade out of 100: "); grade = input.nextInt(); if (grade >= 90){ System.out.println("Congratulations! You got an A!"); } else if (grade >= 80){...

  • 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....

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