• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Whizlabs Question on Operators&Assignments

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boolean b = new Boolean("TRUE");
boolean b1 = true;

Will this choice print true?
if(b.equals("true"){
System.out.println("true");
}

For me, when I compile&run it is printing true. But the answer shows that this is not the right choice.
Can somebody explain if I am wrong? Thanks in Advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there quotes around "true"?
  • b.equals("true") checks to see if the Boolean b is equal to a String.
  • b.equals(true) checks to see if the Boolean b is equal to an autoboxed boolean literal.
  •  
    Esther Kak
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are quotes around true.

    So as you said, it compares to a String===> so will the result be true or false for b.equals("true")?
     
    Ranch Hand
    Posts: 1274
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi ranchers,

    Esther Kak wrote:

    Can somebody explain if I am wrong? Thanks in Advance.



    Maybe you are wrong, the line:
    if(b.equals("true") )

    is comparing a Boolean with a String: "true"
    which will be false.

    Are this "" also included in the original test? I know, you cannot copy/paste from whizlabs.


    Yours,
    Bu.
     
    Burkhard Hassel
    Ranch Hand
    Posts: 1274
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Esther Kak wrote:

    There are quotes around true.

    So as you said, it compares to a String===> so will the result be true or false for b.equals("true")?




    false

    Bu.
     
    Esther Kak
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ooops, I got confused, you are right;
     
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    class Bijendra
    {
    public static void main(String[] args)
    {
    Boolean b = new Boolean("true");
    if(b.equals("true"))
    System.out.println("true");
    else
    System.out.println("false");
    }
    }

    Printing.... false

    class Bijendra
    {
    public static void main(String[] args)
    {
    Boolean b = new Boolean("TRUE");
    if(b.equals(true))
    System.out.println("true");
    else
    System.out.println("false");
    }
    }


    It is printing ...true..

    I want to ask that in equals method (b.equals("true")), we can use string to compare or not?
     
    Burkhard Hassel
    Ranch Hand
    Posts: 1274
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi wranglers,


    Bijendra S. Rajput posted October 12, 2006 11:15 PM

    I want to ask that in equals method (b.equals("true")), we can use string to compare or not?




    You can, but you shouldn't.
    When you compare a Boolean with a string - or any other type than Boolean - the result will always be



    Here's the relative part from java.lang.Boolean:






    Yours,
    Bu.
     
      Bookmark Topic Watch Topic
    • New Topic