Results 1 to 6 of 6
Thread: wrapper classes
- 12-10-2007, 09:19 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 50
- Rep Power
- 0
wrapper classes
which is usefull(commonly used) way to represent numbers like
int x,y;
or
Integer x,y;
what is the difference between them ?
i wrote code like
Java Code:class test4 { public static void main(String args[]) { Integer x,y; x=23; y=30; System.out.println(x+ys); } }
1.incompatible types
2. operator + cannot be applied to java.lang.Integer,java.lang.Integer.
what is wrong with my code ?
please tell me..
- 12-10-2007, 11:08 PM #2
Member
- Join Date
- Aug 2007
- Posts
- 30
- Rep Power
- 0
int is a "low-level" or "base" data type. There is no class associated with it, it is just a space in the computers' memory (in the case of Java in the JVM's memory).
Integer has all of the class information you would expect - you can inherit from Integer, etc.
For me, I only use Integer where I need a class (for storage in lists, etc)... There is a small amount (really, very small) of overhead using Integer, so if I don't have a good reason to, I don't.
Hope that helps.
Don.Don MacVittie F5 Networks - DevCentral
- 12-11-2007, 01:51 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 7
- Rep Power
- 0
Well, you have declared x and y as type "Integer". Integer is a class and when you make such declarations you are creating objects of that class. As such you cannot use the assignment operator to assign them values of type "int", and also you cannot apply the "+" operator to them. In order to use x and y, you need to call the methods of the class Integer.
I think what you want to do is this:
Java Code:class test4 { public static void main(String args[]) { int x,y; x=23; y=30; System.out.println(x+y); } }
- 12-11-2007, 02:28 AM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
There is really no reason to use the Integer class for something as simple:
Java Code:int x = 7, y = 4; int d = y+x;
(sorry for beating the topic to death, it took me a long time to reply, so i didnt see the other two answers getting posted)Last edited by staykovmarin; 12-11-2007 at 03:06 AM. Reason: sorry
- 12-11-2007, 06:14 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 50
- Rep Power
- 0
Hi,
i can't understand one thing.
what is the use of Integer class objects (like x,y in my code) if they are not useful to assign integers.
please give me an example(some code) to understand the use of wrapper classes.
- 12-11-2007, 09:45 PM #6
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Similar Threads
-
Java Service Wrapper 3.3.0
By Java Tip in forum Java SoftwareReplies: 0Last Post: 03-29-2008, 01:04 PM -
problem in wrapper class
By binoympappachen in forum New To JavaReplies: 4Last Post: 12-13-2007, 12:31 PM -
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2
By osval in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 03:47 PM -
Exception Failed to Generate Wrapper Class on WebLogic
By christina in forum New To JavaReplies: 1Last Post: 08-07-2007, 02:15 AM
Bookmarks