aspose file tools*
The moose likes Beginning Java and the fly likes assignment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assignment operator" Watch "assignment operator" New topic
Author

assignment operator

tnemani
Greenhorn

Joined: Jan 17, 2005
Posts: 1
What is the difference between a+=4 and a=a+4;
if we decare a as byte and if we assign a=5 ;i am getting the answer for a+=4; but for a=a+4 i got compilation error why?
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
Originally posted by tnemani:
What is the difference between a+=4 and a=a+4;
if we decare a as byte and if we assign a=5 ;i am getting the answer for a+=4; but for a=a+4 i got compilation error why?


except for the leaving the semicolon off the one and not the other, i can't see any difference at all. unless you've got some kind of syntax error (like a left-off semicolon, maybe?) you really shouldn't be getting compilation errors off either one of these that the other one wouldn't get. could you post the actual error text you're getting, please?
shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
hi tmemani

this error is bcos when u use a=a+4(comsidering a is a byte) the compiler converts a into an int( a widening conversion) and adds it with 4 and the resulting int is passed into a which is of type byte(a narrowing conversion)
so u get a compiler error saying it cannot convert int to byte or inappropriate types etc
where as if u use a+=4 the compiler doesnt give an error cos += operator works in a different way compared with +

hope that explains ur doubt


sandy
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No, actually, he should be seeing an error.

The difference is that the expression "a+4" is of type integer, and therefore the compiler won't let you directly assign it to the byte variable "a" without a cast; i.e., "a = (byte) (a + 4)" is OK.

With the += operator, though, that cast is built-in, and so the assignment compiles without a problem. See Section 15.26.2 of the Java language spec:



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.


See that cast "(T)" in there? That would be "byte" in our example.


[Jess in Action][AskingGoodQuestions]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The compound assignment operator includes an implicit cast to type of the LHS. That is why you are not getting a compile time error.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

tnemani --

Welcome to JavaRanch!

You may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
whoops. thanks, mr. Friedman, for correcting me. i think i've been too spoiled with dynamically typed languages; i'll have to try harder to adjust my thinking for static typing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assignment operator