Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-10-2007, 11:19 PM
Member
 
Join Date: Nov 2007
Posts: 50
sireesha is on a distinguished road
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
Code:
class test4 { public static void main(String args[]) { Integer x,y; x=23; y=30; System.out.println(x+ys); } }
i got following errors
1.incompatible types
2. operator + cannot be applied to java.lang.Integer,java.lang.Integer.

what is wrong with my code ?
please tell me..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-11-2007, 01:08 AM
Member
 
Join Date: Aug 2007
Posts: 30
dmacvittie is on a distinguished road
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 -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-11-2007, 03:51 AM
Member
 
Join Date: Dec 2007
Posts: 7
definewebsites is on a distinguished road
Quote:
Originally Posted by sireesha View Post
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
Code:
class test4 { public static void main(String args[]) { Integer x,y; x=23; y=30; System.out.println(x+ys); } }
i got following errors
1.incompatible types
2. operator + cannot be applied to java.lang.Integer,java.lang.Integer.

what is wrong with my code ?
please tell me..

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:

Code:
class test4 { public static void main(String args[]) { int x,y; x=23; y=30; System.out.println(x+y); } }
HTH
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-11-2007, 04:28 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
There is really no reason to use the Integer class for something as simple:
Code:
int x = 7, y = 4; int d = y+x;
The difference is where the information is stored (primitives are on the stack, while Class instances are put on the heap). Primitives are the ones that you can add subtract and so on. Classes (with the exception of String) do not have those options.

(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 05:06 AM. Reason: sorry
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-11-2007, 08:14 PM
Member
 
Join Date: Nov 2007
Posts: 50
sireesha is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-11-2007, 11:45 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
// ArrayList<int> intgers = new ArrayList<int>(); // <- breaks. You cant // have prmitives in generics ArrayList<Integer> intgers = new ArrayList<Integer>(); // <-works, and it // has the same effect as having integers into it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Service Wrapper 3.3.0 Java Tip Java Announcements 0 03-29-2008 03:04 PM
problem in wrapper class binoympappachen New To Java 4 12-13-2007 02:31 PM
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2 osval Advanced Java 1 08-07-2007 05:47 PM
Exception Failed to Generate Wrapper Class on WebLogic christina New To Java 1 08-07-2007 04:15 AM
Java Wrapper Classes JavaForums Java Blogs 0 08-04-2007 04:24 AM


All times are GMT +3. The time now is 03:24 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org