File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Weird parts of Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Weird parts of Java" Watch "Weird parts of Java" New topic
Author

Weird parts of Java

Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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!
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1050
    
  10

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?
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1050
    
  10

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.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10068
    
163

Incidentally, there's a thread which is discussing a very similar topic and has some very good explanation here http://www.coderanch.com/t/632033/ocajp/certification/System-println-print#2894449

[My Blog] [JavaRanch Journal]
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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 ;)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
yeah, youre right... lets close this thread
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

I'm really happy I don't have to work with pointers and goto statements
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Nothing wrong with pointers … at least not until you try arithmetic on them
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
You need to go back to 1966 for a real great: Edsger Dijkstra; “go to statement considered harmful”.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 933
    
  14

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.


Formely Pawel Pawlowicz
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
But you write it as a positive number. The most negative int can be written as 0x80000000
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Thank you
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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

Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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... ;))
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

If you consider that Android is based on Linux... I would call that very successful.
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
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"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Bora Sabrioglu
Ranch Hand

Joined: Nov 19, 2012
Posts: 86
Henry Wong wrote:Java's implementation of floating point follows the IEEE 754 standard.

But only if you use strictfp, right?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Weird parts of Java