• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapper classes  RSS feed

 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Byte bb=new Byte(12); /error
Byte bbb=new Byte(12); //error

Integer i1=new Integer(123);
Integer i2=new Integer(123);



Short s1=new Short(100); // error
Short s2=new Short(100); //error

Long l1=new Long(123);
Long l2=new Long(123);


Float f1=new Float(123);
Float f2=new Float(123);

Double d1=new Double(100);
Double d2=new Double(100);



Why only errors for Byte and Short Wrapper types ?



Long long=127L;
Long long1=127L;

System.out.println(long==long1); /// TRUE


Long long=128L;
Long long1=128L;

System.out.println(long==long1); /// FALSE





 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the error? What do you think the error means? Did you have a look at the API documentation for the constructor of the classes Byte and Short?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivas sy wrote:Long long=127L;
Long long1=127L;

System.out.println(long==long1); /// TRUE


Long long=128L;
Long long1=128L;

System.out.println(long==long1); /// FALSE


This is an effect due to the implementation of autoboxing ... and, yet another reason to not compare references, when you mean to compare the values. If you want to compare the values, then use the equals() method.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:If you want to compare the values, then use the equals() method.

@srinivas: You may also find some answers in the CachedObjects page.

Winston
 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry Wong,
thanks for reply.I think hashcode and equals methods are already overriden in Wrapper classes....


Ny question here is why is it giving true for 127L values and why for 128L?


up to 127L its giving true..after that it is not holding more that that value i think???
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivas sy wrote:thanks for reply.I think hashcode and equals methods are already overriden in Wrapper classes....
Ny question here is why is it giving true for 127L values and why for 128L?

Because what you're doing has nothing to do with either hashCode() OR equals(). Look at your code again and read the AvoidTheEqualityOperator (←click) page ... and maybe the other one I pointed you to.

Winston
 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper de Jong,

the error is " The constructor Byte(int) is undefined"

Why have they made like this? not For all wrapper classes,but only for these two?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivas sy wrote:Hi Jesper de Jong [...] Why have they made like this? not For all wrapper classes,but only for these two?

Srinivas,

You must do some work for yourself, rather than just rattling off questions machine-gun style. Jesper already gave you a link to look at. Have you read it?

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!