This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes No compile time error? Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "No compile time error? Why?" Watch "No compile time error? Why?" New topic
Author

No compile time error? Why?

Peter Petrov
Greenhorn

Joined: Feb 03, 2006
Posts: 17
This method compiles fine.



All variables are defined as long except the variable 'c'.

What am I missing?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18543
    
  40

Take a look at section 15.26.2 of the Java Specification...

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

The relevant paragraph is...

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T may be either an identity conversion (§5.1.1) or a narrowing primitive conversion (§5.1.3).


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Petrov
Greenhorn

Joined: Feb 03, 2006
Posts: 17
Well, thanks. That's amazing ;) After your reply I tried this
c = c + s;
this does not compile OK - it gives errors.

I never knew this I think, I thought the two assignments are just synonyms.
Did not suspect there's some difference in their semantics.

Is this something introduced after Java 1.4 ( new in 5.0 / 6.0 ) ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

me too Socked
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2997
    
    9
Peter Petrov wrote:I never knew this I think, I thought the two assignments are just synonyms.
Did not suspect there's some difference in their semantics.

Is this something introduced after Java 1.4 ( new in 5.0 / 6.0 ) ?

No, I think it's always been like this. Since the first edition of the JLS, at least. It's a subtle point, so it doesn't come up very often.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I use compound assignments all the time. I never knew there were subtle differences like that. Thanks!!

SCJA
~Currently preparing for SCJP6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: No compile time error? Why?
 
Similar Threads
June Newsletter Puzzle
int -> float -> int and loss of precision
Finding primitive polynomials
why not i += s?
Save file without unwanted extensions