Java method to check variable (counter) max length

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaalien
    New Member
    • May 2007
    • 34

    Java method to check variable (counter) max length

    Kindly please anyone who knows is the any method to know
    what is the limit of length of a counter?
    Depsite from initiliazed variable's types (either int, byte, long etc)
    is there any way for me to check my variable (counter)'s max lenght?
    Please share the info with me...
    Thank you in advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by javaalien
    Kindly please anyone who knows is the any method to know
    what is the limit of length of a counter?
    Depsite from initiliazed variable's types (either int, byte, long etc)
    is there any way for me to check my variable (counter)'s max lenght?
    Please share the info with me...
    Thank you in advance.
    What type is your counter ?

    Comment

    • javaalien
      New Member
      • May 2007
      • 34

      #3
      Originally posted by r035198x
      What type is your counter ?
      Integer type.
      Is there any method that I could check it despite initialize int type the max would be 32 lenght?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by javaalien
        Integer type.
        Is there any method that I could check it despite initialize int type the max would be 32 lenght?
        I'm not sure I got you right but maximum value an int can take is (2^31) - 1.
        You can get this using Integer.MAX_VAL UE

        Comment

        • javaalien
          New Member
          • May 2007
          • 34

          #5
          Originally posted by r035198x
          I'm not sure I got you right but maximum value an int can take is (2^31) - 1.
          You can get this using Integer.MAX_VAL UE
          Thank you ...
          You mean this syntax?
          Code:
          public static final int   MAX_VALUE = 0x7fffffff;
          How I am going to initialize my counter? besides the standard initialize :
          Code:
          int counter;
          One more thing, what would happen when my counter exceeds the max value?
          Is there any prompt error? It just that I want to make sure my counter returns correct (without exceeding) value.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by javaalien
            Thank you ...
            You mean this syntax?
            Code:
            public static final int MAX_VALUE = 0x7fffffff;
            How I am going to initialize my counter? besides the standard initialize :
            Code:
            int counter;
            One more thing, what would happen when my counter exceeds the max value?
            Is there any prompt error? It just that I want to make sure my counter returns correct (without exceeding) value.
            Try it and see what happens here:
            [CODE=java]
            public class WhatHappens {
            public static void main(String ... args) {
            int value = Integer.MAX_VAL UE;
            value = value + 1;
            System.out.prin tln(value);
            }
            }
            [/CODE]

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by javaalien
              Thank you ...
              You mean this syntax?
              Code:
              public static final int   MAX_VALUE = 0x7fffffff;
              How I am going to initialize my counter? besides the standard initialize :
              Code:
              int counter;
              One more thing, what would happen when my counter exceeds the max value?
              Is there any prompt error? It just that I want to make sure my counter returns correct (without exceeding) value.
              You don't have to specify that number yourself. It's already done for you. Read
              the API documentation for the Integer class. While your at it there's also a Long,
              Short, Character, Byte etc. classes. They all implement public static final values
              indicating the maximum and minimum values their corresponding primitives can
              hold. When you add one to the maximum value or subtract one from the minimum
              value, the value just 'wraps around' just like those old mechanical cash registers
              did in the middle of the previous century ;-)

              kind regards,

              Jos

              Comment

              • javaalien
                New Member
                • May 2007
                • 34

                #8
                Originally posted by r035198x
                Try it and see what happens here:
                [CODE=java]
                public class WhatHappens {
                public static void main(String ... args) {
                int value = Integer.MAX_VAL UE;
                value = value + 1;
                System.out.prin tln(value);
                }
                }
                [/CODE]
                Yeah, it returns -2^31. By looking at the output (negative integers) so we know that it exceeds limit.
                What happen when the counter keeps counting (probably big program and MANY iterations) until it returns positive integer? And we just take the output as an answer, Is that possible? How to know that our counter type is catered to store any output num?

                Comment

                • javaalien
                  New Member
                  • May 2007
                  • 34

                  #9
                  [QUOTE=JosAH]You don't have to specify that number yourself. It's already done for you. Read
                  the API documentation for the Integer class. While your at it there's also a Long,
                  Short, Character, Byte etc. classes. They all implement public static final values
                  indicating the maximum and minimum values their corresponding primitives can
                  hold. When you add one to the maximum value or subtract one from the minimum
                  value, the value just 'wraps around' just like those old mechanical cash registers
                  did in the middle of the previous century ;-)

                  kind regards,

                  yeah you're right. this code returns positive int 2147483643.
                  It's like -2 -2 + 2147483647
                  Code:
                   int value = Integer.MAX_VALUE;
                    value = value + 2147483647 + 2147483647 + 2147483647 + 2147483647;
                  System.out.prin tln(value);

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by javaalien
                    Yeah, it returns -2^31. By looking at the output (negative integers) so we know that it exceeds limit.
                    What happen when the counter keeps counting (probably big program and MANY iterations) until it returns positive integer? And we just take the output as an answer, Is that possible? How to know that our counter type is catered to store any output num?
                    If you are so worred about exceeding the maximum int size, why not use bigger types like long (max value (2^63) - 1 ) or even java.math.BigIn teger

                    Comment

                    • javaalien
                      New Member
                      • May 2007
                      • 34

                      #11
                      Originally posted by r035198x
                      If you are so worred about exceeding the maximum int size, why not use bigger types like long (max value (2^63) - 1 ) or even java.math.BigIn teger
                      Yeah, you're right..
                      Better use BigInteger type - but then how to initiliaze it, same as standard initialization for the other types? kindly please share some tips..thank you

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by javaalien
                        Yeah, you're right..
                        Better use BigInteger type - but then how to initiliaze it, same as standard initialization for the other types? kindly please share some tips..thank you
                        Can you please elaborate a bit about that humongous iteration you're planning
                        to implement? Note that arrays, ArrayLists and LinkedLists just take 2^31-1
                        elements maximum? BigIntegers are generally used for integer number
                        theoretical applications (you can't even use them for an index value).

                        kind regards,

                        Jos

                        Comment

                        • javaalien
                          New Member
                          • May 2007
                          • 34

                          #13
                          Originally posted by JosAH
                          Can you please elaborate a bit about that humongous iteration you're planning
                          to implement? Note that arrays, ArrayLists and LinkedLists just take 2^31-1
                          elements maximum? BigIntegers are generally used for integer number
                          theoretical applications (you can't even use them for an index value).

                          kind regards,

                          Jos
                          Actually that's the answer that I am looking for..I do need to know if a very large application with many iterations (I do not know how big it is but it just a caution)
                          what counter type that I need to initialize?
                          something like to check counter++ will return an int value...
                          So from your experience, how many iteration of any large application (the longest) could be? care to explain for me please..thank you.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by javaalien
                            Actually that's the answer that I am looking for..I do need to know if a very large application with many iterations (I do not know how big it is but it just a caution)
                            what counter type that I need to initialize?
                            something like to check counter++ will return an int value...
                            So from your experience, how many iteration of any large application (the longest) could be? care to explain for me please..thank you.
                            I'd stick with simple ints if I were you. If that won't work out you can't keep all
                            the objects in memory anyway and you'd need a database of some sort then.
                            Follow the "KISS" principle: Keep It Simple St*pid ;-) Don't forget 2^31-1 is
                            a whole lot.

                            kind regards,

                            Jos

                            Comment

                            • javaalien
                              New Member
                              • May 2007
                              • 34

                              #15
                              Need an assistance on how to put this kind of alert in my program...
                              Lat's say I assign a counter which suppose to count how many word 'A' in a text file. So how am I supposed to do to alert me when it's already achieve max limit? I can't wait until the execution to finish and returns -ve value or false value.
                              So when it reach int limit (2^31-1), it suppose stop the execution (or at least pop up an alert or etc). Is this possible to do? Thank you in advance.

                              note: Stick to Integer type.

                              Comment

                              Working...