Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

could u pls tell this

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class X {
2. public static void main (String[]args) {
3. String s1 = new String (�true�);
4. Boolean b1 = new Boolean (true);
5. if (s2.equals(b1)) {
6. System.out.printIn(�Equal�);
7. }
8. }
9. }

I guess the ouptut is Compile time error...since there is no wrapper class for
Boolean b1=new Boolean(true);
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes there is a wrapper class Boolean.

However if you use the .equals method on a String object and send a Boolean to it, you will get false.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code should run properly without throwing any exception.

.equals method takes any object as argument, but if both the argument and the object on which the method was invoked doesn't match, it will return a false.

You can check whether both the objects(argument and the one on which the method was invoked) are same using instanceof operator.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But I think s2 is not decleared.
So a compile time error.
Please let me know if I am wrong.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes....code is wrong...if u use s1 in the place of s2, and run the code...it wont thrw the error...but no output coz the s1 string is not equal to b1.
to c the output,can check one also
" if (!s1.equals(b1))"


try this code:
public static void main (String[]args) {
String s1 = new String ("true");
Boolean b1 = new Boolean (true);
if (s1.equals(b1)) {
System.out.println("Equal");
}
}
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, check this thread.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic