Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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

Q from Marcus Green Exam

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is the question
class Base{
public static int iAge;

}
public class CSturt{
public static void main(String argv[]){
CSturt cs = new CSturt();
cs.ww();
}
public void ww(){
Base b = new Base();
Base b2 = new Base();
b.iAge = 10;
b2.iAge = 20;
System.out.print(b.iAge);
Integer iw = new Integer(b.iAge);
col(iw);

}
public static void col(Object o){
Integer iw = (Integer) o; //line 1
System.out.print(iw.intValue());
}
}
The ans is 2020.
Line 1 does not produce a ClassCastException while running. Any idea?
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only call to col() is col(iw); iw is an Integer, which is implicitly narrowed to an Object with the call, however, it is still an Integer object (referenced to by Object reference o) and can be explicitly widened (cast) to such within the col method.
Add col( new Object() ); (or any object other than Integer) to get the result you're expecting.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic