Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inetger Wrapper

 
aakash bath
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to add a value in Integer object
An eg:
Integer no = new Integer();
no = no+1;
It throws an error why we have to convert in to primitive type int and add it.
Regards,
aakash
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Integer constructor takes a parameter, either an int or a String. The parameter specifies the Integer's initial value.
You can't add one to a number until you know what that number is.
[ July 11, 2002: Message edited by: Elouise Kivineva ]
 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Also the return value of + is an integer, so u cannot directly assign it to Integer

MSM
 
aakash bath
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I missed the parameter
The statement
Integer t = new Integer(2);
t = t+1;
Throws an error
operator + cannot be applied to java.lang.Integer,int
Regards,
aakash
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Integer object, like other wrapper objects, is immutable. Once it is created, its state cannot be changed. You will just have to create a new Integer object.
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't change the value of the Integer object, but you can create a new one.
Try
Integer t = new Integer(2);
t = new Integer(t.intValue() + 1);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic