• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setAttribute() with primitives

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting a compile time error ? What is the error ? Because they should work fine.
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic