Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: int and Integer

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default int and Integer

    whats the difference with and reserved word int and Integer class?

    Integer[] number = new Integer[12];

    and

    int[] numbers = new int[12];


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: int and Integer

    int is a primitive, Integer is an immutable class. In most cases you want to stick with using primitives. In certain cases, say cases in which Objects are needed to do something, you need to use the object form (an example would be using it in Collections - although as of 1.5 the compiler does the conversions for you).
    see int vs Integer : Java Glossary

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (December 29th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: int and Integer

    that helped me bit sir! thats why im having problems with int[] arrays,,

    Integers allows null values, while int doesnt, the offset or default value of int is '0' , which has no countable value but nut null...

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: int and Integer

    0 is definitely a value

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: int and Integer

    Quote Originally Posted by chronoz13 View Post
    Integers allows null values, while int doesnt, the offset or default value of int is '0' , which has no countable value but nut null...
    If you wish to 'nulify' an int, you can initialize the value to something you know it will never be (in many cases this can be Integer.MAX_VALUE or Integer.MIN_VALUE) - which is sort of a pseudo-null in the sense that if it is equal to this default value, its value was never set.

  7. The Following 2 Users Say Thank You to copeg For This Useful Post:

    chronoz13 (December 31st, 2009), DanielJamesCollier (July 3rd, 2012)

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: int and Integer

    yah exactly world! thats why im having problems with int arrays!! because of 0, and 0 != null

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. How to fetch integer data from excel
    By nehakuls in forum JDBC & Databases
    Replies: 5
    Last Post: May 14th, 2010, 01:44 AM
  3. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  4. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM