Results 1 to 6 of 6

Thread: wrapper classes

  1. #1
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default 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);
     }
    }
    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..

  2. #2
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    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

  3. #3
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default

    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
    Java 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:

    Java Code:
    class test4
    {
    public static void main(String args[])
     {
      int x,y;
      x=23;
      y=30;
      System.out.println(x+y);
     }
    }
    HTH

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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;
    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 04:06 AM. Reason: sorry

  5. #5
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    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.

  6. #6
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java 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.

Similar Threads

  1. Java Service Wrapper 3.3.0
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 03-29-2008, 02:04 PM
  2. problem in wrapper class
    By binoympappachen in forum New To Java
    Replies: 4
    Last Post: 12-13-2007, 01:31 PM
  3. Replies: 1
    Last Post: 08-07-2007, 04:47 PM
  4. Exception Failed to Generate Wrapper Class on WebLogic
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 03:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •