As a concept I don't find that weird since in the general case a+b could be be greater than 127. If one uses
then the compiler can easily decide that a+b can't exceed 127 so it will compile.
Now one could make the compiler much more clever so that it recognised that in your example both 'a' and 'b' can never be modified by anything but what would be the advantage?
This compiles! Okay, I didn't know that... learned something new. Thanks!
We can either close this thread (up to the moderators) or leave it open just for fun for future weird things / things that could be done in a better way....
Maybe someone can go through the list then an use it to build the next supercompiler
It is fundamental to Java that in the addition of byte, char and short they are first promoted to int so that a+b results in an int which cannot be guaranteed to fit into short. Now as I said before one could get the compiler to do a much much more complex analysis of a program (which of course will make it slower to compile) to decide whether or not the result of the addition will fit into the target type but I prefer it not to.
Richard Tookey wrote:It is fundamental to Java that in the addition of byte, char and short they are first promoted to int so that a+b results in an int which cannot be guaranteed to fit into short. Now as I said before one could get the compiler to do a much much more complex analysis of a program (which of course will make it slower to compile) to decide whether or not the result of the addition will fit into the target type but I prefer it not to.
youre right, but common, having to explicitly cast the sum of two bytes to a byte / short / int... doesn't really make for nice and concise source code...
personally I would let the compiler do that implicitly, but hey... maybe I will write one day my own compiler where everything works as I expect it to be ;)
You know what? I don't ever find that to be a problem, because I never use byte or short variables (unless I'm working with somebody else's API which requires them). As Richard said, internally they are promoted to int before they are used in arithmetic, so why not just declare them as int in the first place?
Bora Sabrioglu wrote:yeah, youre right... lets close this thread
I think (and I am guessing) the issue here is that you are learning the Java language -- and is coming in with expectations from a different programming language. A Java programmer who has been using it for a bit, don't see this as weird. I think it is better to let go of your expectations -- as it is probably better to learn the different languages without trying to relate them to each other.
Bora Sabrioglu wrote:yeah, youre right... lets close this thread
I think (and I am guessing) the issue here is that you are learning the Java language -- and is coming in with expectations from a different programming language. A Java programmer who has been using it for a bit, don't see this as weird. I think it is better to let go of your expectations -- as it is probably better to learn the different languages without trying to relate them to each other.
Henry
you're right... I just miss my C
I want to have pointers... and goto statements .... and ....
I guess I'm better off if I do it as you said... just learn Java without any preconceptions from other languages... Thanks everybody!
Stephan van Hulst wrote:I'm really happy I don't have to work with pointers and goto statements
I have no issues with C. In fact, I am currently using the language at this very moment -- porting some code that I wrote a few months ago from Linux to Windows. Of course, you can argue that I am currently doing more C pre-processor coding than C coding...
Having said that, I am going to argue that the OP probably may not have much commercial experience with C (in large projects). Quite frankly, goto statements (even in C) are something that you would really need to justify in using. I personally try my best not to use them, because I don't like to be red-flagged in a code review.
Pointers, however, I can get really comfortable in using.
Oh, pointers are fine! I don't have much problems working with them, and they have good uses; I'm just happy Java hides most of that stuff and I don't have to fool around with all the annoying referencing and dereferencing, etc.
Campbell Ritchie wrote:Nothing wrong with pointers … at least not until you try arithmetic on them
The thing that bites me the most is actually not pointer arithmetic. That is actually something that I don't do much of -- even in C.
When coming back to C (from Java), I tend to forget that pointers may or may not point to something that is valid. Java coding tends to make me lazy. And I wind up getting (and having to correct) lots of errors with pointers pointing to invalid data. Or worse, memory leaking (and not getting any errors).
Henry Wong wrote:I have no issues with C. In fact, I am currently using the language at this very moment -- porting some code that I wrote a few months ago from Linux to Windows. Of course, you can argue that I am currently doing more C pre-processor coding than C coding...
Oddly, that's the thing I miss most about C. The ability to create macros.
getchar(), as far as I'm concerned, is an absolute masterpiece of minimialist code - 1 line, less than 80 characters long, that took me almost an hour to figure out. I still bow three times to its creater.
Although, it has to be said, after you've ploughed your way through a few n-hundred line absurdly cryptic headers, it does get kind of tedious.
When will we programmers learn? Everything in moderation.
Henry Wong wrote: I am going to argue that the OP probably may not have much commercial experience with C (in large projects).
True. I'm a rookie.
Quite frankly, goto statements (even in C) are something that you would really need to justify in using. I personally try my best not to use them, because I don't like to be red-flagged in a code review.
Now I think its time to quote my role model as far as programming is concerned... imho the best and most talented programmer since the invention of computers - Ken Thompson:
"If you need to go somewhere - goto is the best way to get there."
Henry Wong wrote:
I have no issues with C. In fact, I am currently using the language at this very moment -- porting some code that I wrote a few months ago from Linux to Windows. Of course, you can argue that I am currently doing more C pre-processor coding than C coding...
Dagnabbit !! I can't believe that I forgot that Microsoft doesn't support the ANSI C99 standard. Now, I have lots of local variable declarations that I have to move...
Bora Sabrioglu wrote:
Quite frankly, goto statements (even in C) are something that you would really need to justify in using. I personally try my best not to use them, because I don't like to be red-flagged in a code review.
Now I think its time to quote my role model as far as programming is concerned... imho the best and most talented programmer since the invention of computers - Ken Thompson:
"If you need to go somewhere - goto is the best way to get there."
I like it... that quote should definitely get you some smirks during a code review...
However, keep in mind that "best-practices" change over time. During Ken Thompson era, I don't think he had to worry about the ridiculous large project, where the most costly component in the project is the developer.
If he had, then C would had handled it -- and C++ would not have been invented.
Bora Sabrioglu wrote: Now I think its time to quote my role model as far as programming is concerned... imho the best and most talented programmer since the invention of computers - Ken Thompson:
"If you need to go somewhere - goto is the best way to get there."
It might be worth mentioning that he also said:
"I view Linux as something that's not Microsoft — a backlash against Microsoft, no more and no less. I don't think it will be very successful in the long run..."
A brilliant fellow, no doubt; but I suspect the 90's weren't his best decade when it comes to quotes.
Another weird feature: you know that integer literals are never negative? And the range for an int, for example, is twice as large if you write it in hex than if you write decimal.
Campbell Ritchie wrote:Another weird feature: you know that integer literals are never negative?
Nitpick time ;)
Decimal integer literals are never negative.
A hex, octal or binary literal may be negative:
JLS 3.10.1
A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits interspersed with underscores, and can represent a positive, zero, or negative integer.
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.
A binary numeral consists of the leading ASCII characters 0b or 0B followed by one or more of the ASCII digits 0 or 1 interspersed with underscores, and can represent a positive, zero, or negative integer.
Now, we are talking! ... It is not very common to quote someone who invented the algorithm that is used in every piece of high-end networking equipment today -- every piece of equipment that supports link-state-routing, that is. Of course, this is made doubly impressive because when he invented it, computer networks were not very common!
"3.8 Goto and Labels
C provides the infinitely-abusable goto statement(...). Formally the goto is never necessary, and in practise it is almost always easy to write code without it.(...)
Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out fo two or more loops at once.(...)
Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all."
(The C Programming Language, 2nd edition, Dennis Ritchie <the inventor of C>)
So thats right, makes no sense to have gotos all over the place... but having one or the other goto here and there is just like the spice in the program... having no gotos at all is just so
Winston Gutkowski wrote: I don't think it will be very successful in the long run..."
He was right actually... MS still dominates the desktop market... Linux only dominates the server market... so Linux has not been very successful... only successfull... ;))
Bora Sabrioglu wrote:
So thats right, makes no sense to have gotos all over the place... but having one or the other goto here and there is just like the spice in the program... having no gotos at all is just so
not bad really, this way I learned that a double can have 324 digits after the comma:
Anyone who can make Java make more iterations will get a cow
Campbell Ritchie wrote:How can you get more iterations than that?
Maybe with BigInteger...
Have you worked out how many iterations that will be and why?
Since the range of double varies (I guess its JVM or platform dependent) one can't tell for sure how many iterations... but they're enough for you to make one washup or two
Bora Sabrioglu wrote:
not bad really, this way I learned that a double can have 324 digits after the comma:
Anyone who can make Java make more iterations will get a cow
BTW, this is an infinite loop. Why? While a double can hold a number with digits 300+ places to the right of the decimal point, it can only hold numbers with a precision of about 15 or 16 significant digits. This means that once the double index variable has a non-zero digit more than 15 or 16 digits from the number that you are adding, you are effectively adding zero (due to rounding). The index will stop increasing in value.
Hence, infinite loop, and no way to make more iterations.
Henry Wong wrote:BTW, this is an infinite loop. Why? While a double can hold a number with digits 300+ places to the right of the decimal point, it can only hold numbers with a precision of about 15 or 16 significant digits. This means that once the double index variable has a non-zero digit more than 15 or 16 digits from the number that you are adding, you are effectively adding zero (due to rounding). The index will stop increasing in value.
Have you worked out how many iterations that will be and why?
Since the range of double varies (I guess its JVM or platform dependent) one can't tell for sure how many iterations... but they're enough for you to make one washup or two
Java's implementation of floating point follows the IEEE 754 standard. It is *not* JVM or platform dependent. And BTW, this standard is also supported by most of today's modern day processors, and hence, also supported by most programming languages.
Henry Wong wrote:Java's implementation of floating point follows the IEEE 754 standard.
But only if you use strictfp, right?
Oh. Completely forgot about that.
I believe that Java will follow IEEE 754 regardless of whether it is strict or not. However, if it is not strict, the JVM will be allowed to use more precision calculations, for interim operations, on those platforms that support higher precision hardware. It is still technically IEEE 754, but in your test loop, it may have more iterations before the index stops increasing due to less round-off. Not that it matters, as it will be an infinite loop in all cases.
Henry Wong wrote: . . . This means that once the double index variable has a non-zero digit more than 15 or 16 digits from the number that you are adding, you are effectively adding zero (due to rounding). . . .
Are you sure? I am not convinced. If you look up Double.MIN_VALUE it is 4.9E-324. When you get to such small values, precision is so low (only to the nearest bit, i.e. 0.3010... digits), that 1.0E-324 is < 0.5 × MIN_VALUE. Chances are, the compiler will convert that to 0.0, as the nearest available double, and you will be adding 0 from the very first iteration.
Actually 0 . 324 zeros 1 is 1.0E-325, so it is even closer to 0.0
No, I seem to have been mistaken. 1.0E-324 won't compile and if you use 1.0E-323 the loop runs for an appreciable time, so the compiler obviously thinks of it as MIN_VALUE not 0.
Bora Sabrioglu wrote:"...Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out fo two or more loops at once.(...)"
With all due deference to the God Dennis Ritchie, I'd say that he was probably writing back in the days when programs were still monolithic. Since I rarely write loops more than two-deep - and if I need them I'll put succeeding layers in methods - you really do need an alternative to "branch/jump direct".
That said, I'm not averse to multiple return statements, unlike some.
Winston
Post by:autobot
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, the permaculture playing cards