473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
May 31 '07 #1
29 7672
r035198x
13,262 8TB
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 ?
May 31 '07 #2
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?
Jun 1 '07 #3
r035198x
13,262 8TB
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_VALUE
Jun 1 '07 #4
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_VALUE
Thank you ...
You mean this syntax?
Expand|Select|Wrap|Line Numbers
  1. public static final int   MAX_VALUE = 0x7fffffff;
  2.  
How I am going to initialize my counter? besides the standard initialize :
Expand|Select|Wrap|Line Numbers
  1. 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.
Jun 1 '07 #5
r035198x
13,262 8TB
Thank you ...
You mean this syntax?
Expand|Select|Wrap|Line Numbers
  1. public static final int MAX_VALUE = 0x7fffffff;
  2.  
How I am going to initialize my counter? besides the standard initialize :
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class WhatHappens {
  3.  public static void main(String ... args) {
  4.   int value = Integer.MAX_VALUE;
  5.   value = value + 1;
  6.   System.out.println(value);
  7.  }
  8.  
Jun 1 '07 #6
JosAH
11,448 Expert 8TB
Thank you ...
You mean this syntax?
Expand|Select|Wrap|Line Numbers
  1. public static final int   MAX_VALUE = 0x7fffffff;
  2.  
How I am going to initialize my counter? besides the standard initialize :
Expand|Select|Wrap|Line Numbers
  1. 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
Jun 1 '07 #7
Try it and see what happens here:
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class WhatHappens {
  3.  public static void main(String ... args) {
  4.   int value = Integer.MAX_VALUE;
  5.   value = value + 1;
  6.   System.out.println(value);
  7.  }
  8.  
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?
Jun 1 '07 #8
[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
Expand|Select|Wrap|Line Numbers
  1.  int value = Integer.MAX_VALUE;
  2.   value = value + 2147483647 + 2147483647 + 2147483647 + 2147483647;
System.out.println(value);
Jun 1 '07 #9
r035198x
13,262 8TB
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.BigInteger
Jun 1 '07 #10
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.BigInteger
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
Jun 3 '07 #11
JosAH
11,448 Expert 8TB
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
Jun 3 '07 #12
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.
Jun 3 '07 #13
JosAH
11,448 Expert 8TB
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
Jun 3 '07 #14
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.
Jun 5 '07 #15
JosAH
11,448 Expert 8TB
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.
Use a long counter then; if a certain word occurs more than n times; the word
has to present in the text for at least 2^63-1 times. Note that if everybody on the
world owns more than a couple of thousand books than for every book (~ 200 pages)
the counter won't overflow if it occurred more than tens of times on every page
on every book.

kind regards,

Jos
Jun 5 '07 #16
r035198x
13,262 8TB
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.
Have a look at the JOPtionPane class .
Jun 5 '07 #17
Use a long counter then; if a certain word occurs more than n times; the word
has to present in the text for at least 2^63-1 times. Note that if everybody on the
world owns more than a couple of thousand books than for every book (~ 200 pages)
the counter won't overflow if it occurred more than tens of times on every page
on every book.

kind regards,

Jos
Thank you.
If my rewrite my question to long type, how am I going to solve it then?
It just that I can't wait for it to keep counting after exceed limit , better I stop the execution or give some kind of alert.
Jun 5 '07 #18
JosAH
11,448 Expert 8TB
Thank you.
If my rewrite my question to long type, how am I going to solve it then?
It just that I can't wait for it to keep counting after exceed limit , better I stop the execution or give some kind of alert.
You do realize that with a maximum numer as big as 2^63-1 you will be able to
count every thousands of a millimetre for a journey starting at the Sun and
ending at Pluto do you?

kind regards,

Jos
Jun 5 '07 #19
You do realize that with a maximum numer as big as 2^63-1 you will be able to
count every thousands of a millimetre for a journey starting at the Sun and
ending at Pluto do you?

kind regards,

Jos
Ok Jos, It's a caution matter to me, I do need to put something that if the thing is going to happen..not more than that.
Jun 5 '07 #20
JosAH
11,448 Expert 8TB
Ok Jos, It's a caution matter to me, I do need to put something that if the thing is going to happen..not more than that.
Be realistic and do your maths; files that don't exist while they're supposed to;
sockets that won't connect while the server should be up and running; headless
machines running your application; some loonie user switching your computer
off in the middle of some sort of transaction; spilling cola all over your keyboard
while some critical code is running; power failures while new batteries fail too
and a UPS burn out at the same time; half of your RAM memory failing because
of a bad contact caused by one single pin; your mother in law burning the one
and only source code listing with her cigar; sending all backup disks into orbit
(including your workdisk); eclipse wiping out all your sources because of a cvs
failure; a nuclear bomb on your office; EMP into your network connection cable;
the sky falling down of everybody's head.

*Everything* is more likely to make your application fail than an overflow in your
long counter. Don't even implement a JOptionPane message for that.

kind regards,

Jos
Jun 5 '07 #21
r035198x
13,262 8TB
You do realize that with a maximum numer as big as 2^63-1 you will be able to
count every thousands of a millimetre for a journey starting at the Sun and
ending at Pluto do you?

kind regards,

Jos
If you don't mind to please show your working and assumed distances.
Jun 5 '07 #22
JosAH
11,448 Expert 8TB
If you don't mind to please show your working and assumed distances.
Sure, I'd be glad to:

|Sun, Pluto| ~= 6e12 metres

6e12/(2^63) ~= 6.5e-7 metres ~= 6.5e-4 millimetres < 1/1000 millimetres.

kind regards,

Jos
Jun 5 '07 #23
r035198x
13,262 8TB
Be realistic and do your maths; files that don't exist while they're supposed to;
sockets that won't connect while the server should be up and running; headless
machines running your application; some loonie user switching your computer
off in the middle of some sort of transaction; spilling cola all over your keyboard
while some critical code is running; power failures while new batteries fail too
and a UPS burn out at the same time; half of your RAM memory failing because
of a bad contact caused by one single pin; your mother in law burning the one
and only source code listing with her cigar; sending all backup disks into orbit
(including your workdisk); eclipse wiping out all your sources because of a cvs
failure; a nuclear bomb on your office; EMP into your network connection cable;
the sky falling down of everybody's head.

*Everything* is more likely to make your application fail than an overflow in your
long counter. Don't even implement a JOptionPane message for that.

kind regards,

Jos
Jos. Please step away from the keyboard slowly and go to bed.

@OP: Jos is right of course. It's very good of you to be thinking of overflows in your programs and all that but (2^63) - 1 is very big. Try to write it down and see.
Jun 5 '07 #24
JosAH
11,448 Expert 8TB
Jos. Please step away from the keyboard slowly and go to bed.
I still have to have dinner and a bit of leisure time until I go to bed!

kind regards,

Jos ;-)
Jun 5 '07 #25
blazedaces
284 100+
Sure, I'd be glad to:

|Sun, Pluto| ~= 6e12 metres

6e12/(2^63) ~= 6.5e-7 metres ~= 6.5e-4 millimetres < 1/1000 millimetres.

kind regards,

Jos
r035198x, I'm not trying to be insulting, I just think this was funny as ...

YOU HAVE BEEN
PWNED!!!!!!!!!

:P

-blazed

P.S. I still can not understand why you still have a problem. You keep asking how to spit out a warning if your counter gets too high. If you really want to do it, just do it, it's just an if-statement every time (waste of processing speed, but very very very little depending on how often you do it) asking if the counter == integer.maxvalue or whatever they wrote up top.
Jun 5 '07 #26
r035198x
13,262 8TB
...(waste of processing speed, but very very very little depending on how often you do it) asking if the counter == integer.maxvalue or whatever they wrote up top.
and the work done / the functionality gained is disappointing considering that the condition is most likely never going to be satisfied.
Jun 5 '07 #27
blazedaces
284 100+
and the work done / the functionality gained is disappointing considering that the condition is most likely never going to be satisfied.
Yes, I concur, but he kept insisting, so I wanted to point out the obvious choice of doing whatever you want regardless of the advice from people with more knowledge and experience (not talking about myself here).

-blazed
Jun 5 '07 #28
Thank you very much for all the advice.
Of course I take note all of your precious advice to me...
I am here to consult from you guys that must be very experienced and already be there before me....So by asking some sort of silly questions (to you guys but not for me, because I really don't know) so I will get some feedback, am I doing right or maybe that's not even possible..that's it.
I am learning and sorry for making any trouble here...thank you
Jun 6 '07 #29
blazedaces
284 100+
Thank you very much for all the advice.
Of course I take note all of your precious advice to me...
I am here to consult from you guys that must be very experienced and already be there before me....So by asking some sort of silly questions (to you guys but not for me, because I really don't know) so I will get some feedback, am I doing right or maybe that's not even possible..that's it.
I am learning and sorry for making any trouble here...thank you
I apologize, I am often too harsh. Please don't ever hesitate to ask whatever question you would like.

The only stupid question is the one you don't ask.

Good luck in your future endeavors,

-blazed
Jun 6 '07 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
4
by: Rhino | last post by:
I've been playing with Java UDFs for the last couple of days and I've got some questions about scratchpads. I'm running DB2 LUW V8 (FP8) on WinXP. Somewhere in the manuals, I found some remarks...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: loganandvicky | last post by:
i have assigned several variables in one method and i want to use them in another method. that works ok until i call the second method. i get java.lang.string error heres my work.... /** ...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
2
by: dmstn | last post by:
Hey! I've got a little problem. I have to make a web site for a university essay. I curently have to create a search engine. Users can enter a hotel name in a search bar and results have to appear in...
49
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
1
by: TrippW06 | last post by:
I working on a program to analyze a string and tell the user how may of each char is in the sentence. I have it sorta working but cant fidgure it out. I get a weird last line and my method to remove...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.