• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

1.5 Integer with +=

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this work:


But not this:


Thanks,
Kevin
[ March 02, 2005: Message edited by: Kevin Stock ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither compiles.

You can't add a number to an object using the '+' operator.

From the language specification...


15.15.3 Unary Plus Operator +

The type of the operand expression of the unary + operator must be a primitive numeric type, or a compile-time error occurs.

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They specified 1.5 in the title. So, they CAN use the "+" operator with an Integer object. It's called "autoboxing" (in this case unboxing actually) which is a new feature of the Java language in 1.5. The reason you can't do the second operation is because the left-hand side of an assignment has to be a variable. In your case, it's not. It's an expression (the result of casting i to an Integer).
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note that += is not the unary + operator. The latter is similar to the unary - operator:
 
Kevin Stock
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James,

I tryed some test cases, and when not casting on the left it works fine. Why can't expressions such as that be on the left side? Doesn't casting effectivly 'replace' what was there with the casted object?
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is your casting parenthesis. Try this:

( ( Integer ) i ) += 1;
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Bizman:
Try this:
( ( Integer ) i ) += 1;

I tried it using JDK 1.5 for kicks, and it failed. As James said, you need an assinable variable on the left side of an assignment operator (called an lvalue). Casting a variable evalutes to the value the variable holds cast to the specified type which is not assignable (called an rvalue).
 
today's feeble attempt to support the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic