Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ExamLab PracticeTest-1 Q54

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output:-true,false,true,false

Dear Friends Please explain me why we are not getting compilation Error at line1 & line2 as "+" operator is overloaded for Strings
not for StringBuilder and StringBuffer.

Thanking You
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile error in "instanceof" operator occur only when both operand belongs to different inheritance tree
So String, Object belongs to same inheritance tree.


But it will not work::

 
aruna dabas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepesh my Question was why expression "a+b" and "c+a" is not giving error.
 
Deepesh Deomurari
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm..

lets see what happened on a+b
a is a String and b is an StringBuilder(i.e. OBJECT)
a+b=> a +b.toString();
(remembered we generally use OBJECT NAME to print with system.out.println which takes STRING argument like system.out.println("Object Created:"+obj) so always in concatenation with the String, result will be String.

it can be taken as
(a+b.toString()) , which will return an object of type STRING.
As you may be aware that you can pass String to object reference.
So Object d=newly created STRING.

Similar explanation for second one
 
aruna dabas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepesh now its clear.javascript:emoticon('');
 
Space pants. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic