3

Just came across this statement and was wondering why this function call had what at first looked like a cast?

SomeClass bo = new SomeClass(); // blabla something like that to initialize the object variable
(bo).setValue(bo.getValue().negate());

As I have not yet seen this syntax - what does it do compared to a simple

bo.setValue(bo.getValue().negate());

?

Lino
  • 19,604
  • 6
  • 47
  • 65
PhiSe
  • 33
  • 3
  • This looks like an anonymous class. But I fail to see the point in an anonymous subclass of `Object` . – Arnaud Jul 27 '18 at 07:31
  • the braces only enforce precedence of an expression over another operator with usually higher precedence, eg. `(1 + 2) * 3`. Here they do not change the meaning since there is no operator in the expression inside it* – joH1 Jul 27 '18 at 07:33
  • 5
    @Arnaud I think the issue is on the second line... The first is just an example of initialization--misleading though. – joH1 Jul 27 '18 at 07:33
  • 5
    There is no difference between `(bo).setValue(...)` and `bo.setValue(...)`. The parenthesis around `bo` are superfluous. – Andreas Jul 27 '18 at 07:35
  • Question title is misleading. Your question is about the `()` around `bo`. `()` are called "parentheses" (or "round brackets"), while `{}` are called "braces" (or "curly brackets"). See [Bracket vs brace](https://english.stackexchange.com/questions/3379/bracket-vs-brace). – Andreas Jul 27 '18 at 07:41

1 Answers1

7

(bo).setValue(bo.getValue().negate()) and bo.setValue(bo.getValue().negate()) are identical statements and the parentheses are reduntant here.

They are needed though when we write expressions like

Object o;
(o = new Object()).toString();  // class java.lang.Object

If we had omitted them,

Object o;
o = new Object().toString();  // class java.lang.String
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142