File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No compile time error? Why?

 
Peter Petrov
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method compiles fine.



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

What am I missing?

 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Petrov
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too Socked
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use compound assignments all the time. I never knew there were subtle differences like that. Thanks!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic