This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inetger Wrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inetger Wrapper" Watch "Inetger Wrapper" New topic

Inetger Wrapper

aakash bath

Joined: Mar 15, 2002
Posts: 12
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.
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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

Joined: Jun 25, 2001
Posts: 110
Also the return value of + is an integer, so u cannot directly assign it to Integer

aakash bath

Joined: Mar 15, 2002
Posts: 12
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
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: May 10, 2001
Posts: 265
You can't change the value of the Integer object, but you can create a new one.
Integer t = new Integer(2);
t = new Integer(t.intValue() + 1);
I agree. Here's the link:
subject: Inetger Wrapper
It's not a secret anymore!