• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting int from session variable

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have this statement in my Servlet

session.setAttribute("errorflag", 0);

I'm having problem casting it to int like...

int errorFlag = (int) session.getAttribute("errorflag", 0);

any idea on how to solve this? Thanks!
[ October 27, 2005: Message edited by: Timothy Sam ]
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

session.setAttribute("errorflag", 0);



This will not compile under Java 1.4. The second parameter must be an object. If you are using Java 1.5, this may be ok since the 0 may auto-box to an Integer. I'm not sure since I'm still using 1.4.

int errorFlag = (int) session.getAttribute("errorflag", 0);



First, getAttribute() does not take a second parameter.

Second, since you cannot cast anything to something that it is not, you cannot cast the return value of type Object to int, since int is not an Object. If an Integer is being returned, you could use the intValue() method to obtain the int value of the Integer.

Again, I'm not certain what the auto-boxing behavior of Java 1.5 would be -- it could be that your only issue is the spurious 2nd parameter.
[ October 27, 2005: Message edited by: Bear Bibeault ]
reply
    Bookmark Topic Watch Topic
  • New Topic