Java puzzler

G

Gábor Bakos

Hello,

Sorry if this is already a known behaviour, but first I found this really strange:
int x = 0;
x += 0.5;
What does the code snippet above do:
- compile and run without error result is
- 0
- 1
- depends on the strict or nonstrict computation
- compile and ArithmeticException is thrown
- does not compile (Type mismatch: cannot convert from double to int)
For those who want to know the answer check this[1] and this[2].
Bests, gabor

[1]: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.2
[2]: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.3
 
L

Lew

Hello,

Sorry if this is already a known behaviour, but first I found this really strange:
int x = 0;
x += 0.5;
What does the code snippet above do:
- compile and run without error result is
- 0
- 1
- depends on the strict or nonstrict computation
- compile and ArithmeticException is thrown
- does not compile (Type mismatch: cannot convert from double to int)

What happened for you when you tried it?

What precisely did you find strange?

<sscce source="com/lewscanon/eegee/Conversioneer.java" >
package com.lewscanon.eegee;
/**
* Conversioneer.
*/
public class Conversioneer
{
/**
* main.
* @param args String []
*/
public static void main( String [] args )
{
int x = 0;
x += 0.5;
System.out.println( "x = "+ x );
}
}
</sscce>
 
R

Roedy Green

int x = 0;
x += 0.5;

If it is not illegal syntax, it should be. One of the language lawyers
like Lew or Patricia will likely do a Delphi-style oracle reading on
the JLS and tell you what it is supposed to do. If it is not what it
is doing, prepare an SSCCE and submit the bug.

See http://mindprod.com/jgloss/sscce.html
http://mindprod.com/jgloss/bug.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
How long did it take after the car was invented before owners understood
cars would not work unless you regularly changed the oil and the tires?
We have gone 33 years and still it is rare to uncover a user who
understands computers don't work without regular backups.
 
L

Lew

If it is not illegal syntax, it should be. One of the language lawyers

Utter nonsense. And you presume to give advice to Java programmers?
like Lew or Patricia will likely do a Delphi-style oracle reading on
the JLS and tell you what it is supposed to do. If it is not what it
is doing, prepare an SSCCE and submit the bug.

See http://mindprod.com/jgloss/sscce.html
http://mindprod.com/jgloss/bug.html

I already prepared an SSCCE half a day prior to your post, which youy
apparently did not notice. The OP already cited relevant sections of the JLS,
which you apparently did not notice.

Is there something wrong with being well-educated on the rules of the Java
language when programming Java code, as you snarkily imply?

There is no bug to submit, so that was useless advice.

The OP hasn't even explained what they dream a problem to be, which you
apparently did not notice.

You are doing a HUGE disservice to newbies by indicating that it is not good
to be intimately familiar with the programming rules by which one is working.
I thought you were all about helping the programmer, but apparently not.
You should stop preaching the virtues of ignorance, Roedy.
 
M

markspace

Is there something wrong with being well-educated on the rules of the
Java language when programming Java code, as you snarkily imply?


Actually I took that as a compliment. He's saying other people know the
language better than him.

I do agree that there's nothing wrong with trying to learn the language
better. Patricia's post explained the reason for (or at least gave a
rationalization for) this oddity in the language. Every once in a while
I find those and have to dial them into my brain. It's not hard to do,
and everyone should at least try.

I figure, once I do finally learn everything there is to know, they'll
probably chuck Java and start on something new....
 
R

Roedy Green

Is there something wrong with being well-educated on the rules of the Java
language when programming Java code, as you snarkily imply?


Everything I say you twist into some sort of crime. You single
handedly cut the traffic in the Java news groups with your personal
attacks on everyone. You have them afraid to speak. You bawl people
out for asking questions. You bawl people out for answering. Just
stick to the facts and stop commenting on motivations.

You normally nit pick just so you can find some excuse to put people
down, so I don't usually read your posts. You are knowledgeable but a
royal PITA.


--
Roedy Green Canadian Mind Products
http://mindprod.com
How long did it take after the car was invented before owners understood
cars would not work unless you regularly changed the oil and the tires?
We have gone 33 years and still it is rare to uncover a user who
understands computers don't work without regular backups.
 
R

Roedy Green

Actually I took that as a compliment. He's saying other people know the
language better than him.

Of course. Surely Lew knows the high esteem I hold Patricia Shanahan.
See http://mindprod.com/jgloss/who.html

I mentioned Lew in the same sentence. That is high praise.

When I read the JLS, it always seems to me extremely ambiguous. I
would never trust my interpretation. Others seem to understand its
language patterns more deeply and with confidence divine what it
means. It may be that they have done more experiments and have at a
gut level learned the sort of language Mr. Gosling uses.

To me it seems highly reckless to trust my own interpretation of the
JLS. It must be supplemented with some experiments for verification.

I have learned that people like Patricia and Lew can make
pronouncements apparently based on nothing but the JLS that are
remarkably accurate. To me this seems an almost magical ability, like
being able to see in the pitch dark.

Lew thinks everyone should have his skill and are just being lazy if
they don't. I think his ability is quite rare.

The basic disagreement is Lew wants people to attempt to understand
the JLS on a routine basis. In contrast, I don't want people to trust
their interpretations. They should do experiments too, and/or consult
people with high skill.

Interpreting the JLS is a special skill quite different from
programming.
--
Roedy Green Canadian Mind Products
http://mindprod.com
How long did it take after the car was invented before owners understood
cars would not work unless you regularly changed the oil and the tires?
We have gone 33 years and still it is rare to uncover a user who
understands computers don't work without regular backups.
 
L

Lew

Of course. Surely Lew knows the high esteem I hold Patricia Shanahan.
See http://mindprod.com/jgloss/who.html

I mentioned Lew in the same sentence. That is high praise.

When I read the JLS, it always seems to me extremely ambiguous. I
would never trust my interpretation. Others seem to understand its
language patterns more deeply and with confidence divine what it
means. It may be that they have done more experiments and have at a
gut level learned the sort of language Mr. Gosling uses.

To me it seems highly reckless to trust my own interpretation of the
JLS. It must be supplemented with some experiments for verification.

I have learned that people like Patricia and Lew can make
pronouncements apparently based on nothing but the JLS that are
remarkably accurate. To me this seems an almost magical ability, like
being able to see in the pitch dark.

Lew thinks everyone should have his skill and are just being lazy if
they don't. I think his ability is quite rare.

The basic disagreement is Lew wants people to attempt to understand
the JLS on a routine basis. In contrast, I don't want people to trust
their interpretations. They should do experiments too, and/or consult
people with high skill.

Interpreting the JLS is a special skill quite different from
programming.

If you cannot even TRY to understand the JLS, you should not program in Java.

The JLS is the ultimate authority on the Java language. You keep suggesting
that a person shouldn't even /attempt/ to understand it unless you have a rare
or special skill to read the specification. Bullshit.

You think I have a special skill at reading it. Sure - it's called "spending
time reading, studying and thinking about the JLS". Anyone who can program
can do that. Anyone who won't do that shouldn't program.

Even you, Roedy, can understand the JLS if you work at it. I'm fairly
confident that I'm not all that much smarter than you are.

I do NOT suggest that one use the JLS as one's only source of information.
That would be stupid. I urge all Java programmers to spend the time and
effort to understand the JLS, using other works to deepen your understanding,
show how to apply it, and explain rationales.

Telling yourself that Lew has a "magical ability" is a foolish fiction. I
just am not lazy about the JLS. Maybe Patricia has one; I can believe that.
Not me. I'm smart, but the key is I'M AT LEAST TRYING!

Roedy, if you cannot understand the JLS you should take down your Java advice
from mindprod.com until you do.

OK, I'm not serious, since your advice is 99% fabulous (and your picture
reveals that you are a surprisingly handsome man, BTW), but really, Roedy, you
do yourself a disservice let alone everyone else. Sure, the JLS can be opaque
in parts in its efforts at precision. But keep at it - it is, after all, the
only and ultimate source of Java language truth.
 
J

Joshua Cranmer

When I read the JLS, it always seems to me extremely ambiguous. I
would never trust my interpretation. Others seem to understand its
language patterns more deeply and with confidence divine what it
means. It may be that they have done more experiments and have at a
gut level learned the sort of language Mr. Gosling uses.

The JLS is quite rigidly specified, at least in comparison to other
specifications I have read (e.g., CSS). It is also well-organized
compared to, say, C++, where I have to spend a lot of time trying to
figure out which clause covers the question I have (I want to know about
something involving default initialization of templatized POD-classes...
only could be in about 5 sections).
Interpreting the JLS is a special skill quite different from
programming.

Much of the programming I have done has required me to read, write,
comprehend, synthesize, and implement specifications, be it as long and
dense as that for a programming language, as underspecified as an old
RFC that predates most concerns for i18n, as formal as the formal
project specification, or as informal as the API documentation.
Specifications like the JLS are merely API documentation at another
level: every token is an input to the function "Compile and run me!"
provided by javac and java, with the JLS merely being a thorough
exposition on the wonderous ways that tokens interact with each other.

In that vein, every programmer should have the skills to read,
comprehend, and interpret the JLS, since they should have the same
skills to read, comprehend, and interpret API documentation.
 
L

Lew

On 5/12/2011 5:35 AM, Lew wrote:
...

I'm not a useful data point for this discussion. The writers of the JLS
benefited from the multiple decades of language specification writing
the preceded it. For example, the syntax notation is a descendant of the
BNF notation in the Algol 60 report.

Before I started learning Java I had a combination of formal education
and practical, on-the-job experience in reading and applying programming
language specifications. When I started reading the JLS it was just a
new instance of a familiar type of document.

There is no way for any of us, myself included, to know how easy or
difficult I would have found the JLS if I had read it without that
background.

Your experience still provides a useful example.

Perhaps you or I do have a special skill at reading specifications. If so,
it's the special skill that distinguishes better programmers from worse ones,
and a very powerful if not actually requisite skill.

Or perhaps we have approximately the same aptitude as nearly everyone who
participates in this Usenet forum.

Either way, both of us - you through more years of formal education than I,
but both of us through diligent ongoing study over years - have applied
ourselves to the skill of reading and being clear about specifications,
language or otherwise. As Thomas Edison said, "Genius is 1% inspiration and
99% perspiration." I aver that it's the study and effort that pays off here,
not some mysterious brain power that Roedy or most people lack.

If you who wish to (advise those who) program in Java do lack the skill to
comprehend the JLS, or any other specification, even after diligent study and
great effort, you may be like the tone-deaf person with a sore throat who
hopes to audition for /American Idol/ - a touch underqualified for the job.
But bless your heart for trying.

If you don't even try, however, then you should at least practice this phrase
for your compatible profession: "Would you like fries with that?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top