aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setAttribute() with primitives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "setAttribute() with primitives" Watch "setAttribute() with primitives" New topic
Author

setAttribute() with primitives

Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Hi !

I know that in the case of a "long" primitive, we can use it in the method setAttribute() due to the autoboxing. So, can I use too an int, float, etc. ?

All these examples work or am I wrong ?

long
long myLong= 10L;
session.setAttribute("myLong", new Long(myLong));
long myLong= 10L;
session.setAttribute("myLong", myLong);


int
int myInt= 10;
session.setAttribute("myInt", new Integer(myInt));
int myInt= 10;
session.setAttribute("myInt", myInt);

float
float myFloat= 10.3f;
session.setAttribute("myFloat", new Float(myFloat));
float myFloat= 10.3f;
session.setAttribute("myFloat", myFloat);

double
double myDouble= 10.3;
session.setAttribute("myDouble", new Double(myDouble));
double myDouble= 10.3;
session.setAttribute("myDouble", myDouble);

And these examples will not work:
session.setAttribute("myLong", 12L);
session.setAttribute("myInt", 12);
session.setAttribute("myFloat", 12.0f);
session.setAttribute("myDouble", 12.0);

Is there another example that I can find during the exam ?

Thanks in advance !

Angy


SCJP 5
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Greetings Fabian,
Before I tell you which will work or not , have you tried them out yourself ? What result did you get and any idea why you getting the result ?


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Yes !

Everything works except the last four lines but that was only for confirmation and especially for the last question about the exam
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Are you getting a compile time error ? What is the error ? Because they should work fine.
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Saif Asif wrote:Are you getting a compile time error ? What is the error ? Because they should work fine.


These ?

session.setAttribute("myLong", 12L);
session.setAttribute("myInt", 12);
session.setAttribute("myFloat", 12.0f);
session.setAttribute("myDouble", 12.0);

I'll test again this evening (I'm at work, I can't test that now)
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Yes these should compile just fine. Auto-Boxing applies here as well.
Do post back for further queries

(I'm at work, I can't test that now)

P.S . Ditto here ;-)
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Hm... it works... I don't know what I did yesterday. Sorry ! But now I know that it works, that's the main thing.
Thank you Saif Asif !
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Fabian Angy wrote:Hm... it works... I don't know what I did yesterday. Sorry ! But now I know that it works, that's the main thing.
Thank you Saif Asif !


You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setAttribute() with primitives