Forums Register Login

Weird parts of Java

+Pie Number of slices to send: Send
Let's make a thread with only the weird parts of Java (and I'm afraid that this thread is going to be a long one ;=))

This is what I found today (p. 192, scjp6 study guide, bates/sierra):

byte a = 3;
byte b = 8;
byte c = a + b; // wont compile!

you have to cast explicitly to a byte:

byte c = (byte) (a+b); // now the compiler is happy...


Now isn't that just a brilliant compiler feature!
1
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

Richard Tookey wrote:


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
+Pie Number of slices to send: Send
 

Richard Tookey wrote:As a concept I don't find that weird since in the general case a+b could be be greater than 127.



but even this doesn't compile:



the sum of two bytes can never be bigger one short... you still have to explicitly cast to a short.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Incidentally, there's a thread which is discussing a very similar topic and has some very good explanation here https://coderanch.com/t/632033/ocajp/certification/System-println-print#2894449
+Pie Number of slices to send: Send
 

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 ;)
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
yeah, youre right... lets close this thread
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

Henry Wong wrote:

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!
+Pie Number of slices to send: Send
I'm really happy I don't have to work with pointers and goto statements
+Pie Number of slices to send: Send
Nothing wrong with pointers … at least not until you try arithmetic on them
+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.

Winston
+Pie Number of slices to send: Send
 

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."

+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
 

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.

Winston
(1 cow)
+Pie Number of slices to send: Send
You need to go back to 1966 for a real great: Edsger Dijkstra; “go to statement considered harmful”.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

 
+Pie Number of slices to send: Send
But you write it as a positive number. The most negative int can be written as 0x80000000
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:You need to go back to 1966 for a real great: Edsger Dijkstra; “go to statement considered harmful”.




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!

Have a cow!!
Henry
+Pie Number of slices to send: Send
Thank you
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Edsger Dijkstra; “go to statement considered harmful”.


Amazing discoveries:

"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

+Pie Number of slices to send: Send
 

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... ;))
+Pie Number of slices to send: Send
If you consider that Android is based on Linux... I would call that very successful.
+Pie Number of slices to send: Send
 

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


Note:
"Don't try this at home"
+Pie Number of slices to send: Send
 

Bora Sabrioglu wrote:. . .
Anyone who can make Java make more iterations will get a cow

How can you get more iterations than that? Have you worked out how many iterations that will be and why?


Note:
"Don't try this at home"

I could tell the Missus I am just waiting for my loop to finish, then I'll do the washing up
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.

Hence, infinite loop


"principle of most surprise"
+Pie Number of slices to send: Send
 

Bora Sabrioglu wrote:

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
+Pie Number of slices to send: Send
 

Henry Wong wrote:Java's implementation of floating point follows the IEEE 754 standard.


But only if you use strictfp, right?
+Pie Number of slices to send: Send
 

Bora Sabrioglu wrote:

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

+Pie Number of slices to send: Send
 

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

Even worse way to get an ∞ loop, I think
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
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
https://gardener-gift.com


reply
reply
This thread has been viewed 2187 times.
Similar Threads
byte b = 'a'; ???
narrowing convertion in case of final
operators
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
Simple Assignment Operator =
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:43:13.