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 3 of 3

Thread: About char type (Reaching MAX limit)

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default About char type (Reaching MAX limit)

    Hello,

    I have noticed that if you add one in the char variable which holds 2^16-1 the variable will hold the zero as the new value.
    Can someone explain me why is it happening?

    Thank you


  2. #2
    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: About char type (Reaching MAX limit)

    First you have to understand how computers represent numbers and perform math. For this example let's consider a 4-bit number, but it's the same for 8-bit, 16-bit, 32-bit, etc. I'm only going to consider unsigned numbers here, though it's similar for signed numbers.

    A 4-bit number can have the values 0000 through 1111.

    When you have the maximum 4-bit number, 1111 and add 1 to it, the result should be 10000. However, this number can't be represented in 4-bits so any bits beyond the right most 4 bits (known as the least significant bits). This is 0000.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: About char type (Reaching MAX limit)

    char - and all the other primitive data types - can only hold distinct number of different values. The limits are documented in Oracle's Tutorial on the Primitive Data Types.

    Floating point values will "overflow" and throw a runtime exception when they do. But integral types (int, char, long) "wrap around" as you have found.

    ---

    I don't really know why... you'd have to ask the language's authors! Perhaps someone here can explain the sort of context where one behaviour rather than another (wrap around vs overflow) is to be preferred, and especially why one group of data types is handled differently to the other.

Similar Threads

  1. JTextField character limit
    By hump_truck in forum AWT / Java Swing
    Replies: 1
    Last Post: May 31st, 2012, 07:17 AM
  2. Not Reaching If Statement
    By Damian3395 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 18th, 2012, 09:04 AM
  3. How do i limit the input time
    By itayj in forum Java Theory & Questions
    Replies: 6
    Last Post: October 24th, 2011, 03:47 PM
  4. [SOLVED] Char type Comparisson
    By solo_2101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 26th, 2010, 09:42 PM
  5. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM