9

If you take Java's primitive numeric types, plus boolean, and compare it to C++ equivalent types, is there any difference what concerns the operators, like precedence rules or what the bit-manipulation operators do? Or the effect of parenthesis?

Asked another way, if I took a Java expression and tried to compile and run it in C++, would it always compile and always give the same result?

Bruce
  • 7,094
  • 1
  • 25
  • 42
Sebastien Diot
  • 7,183
  • 6
  • 43
  • 85
  • I think something different happens when you cast ie (int). I seem to recall that you may get compile errors that you would not get in C. – Adam Gent Jul 08 '11 at 11:47
  • 'm not sure I understand you. Do you mean something compiles in C that does not in Java? That would be OK for me. But the other way around would be a problem. – Sebastien Diot Jul 08 '11 at 17:59
  • Yes something compiles in C that Java's coercion would not allow. I tried to figure out what it was last night... damn my memory. – Adam Gent Jul 09 '11 at 00:26

6 Answers6

13
  • For an expression like:

    a = foo() + bar();
    

    In Java, the evaluation order is well-defined (left to right). C++ does not specify whether foo() or bar() is evaluated first.

  • Stuff like:

    i = i++;
    

    is undefined in C++, but again well-defined in Java.

  • In C++, performing right-shifts on negative numbers is implementation-defined/undefined; whereas in Java it is well-defined.

  • Also, in C++, the operators &, | and ^ are purely bitwise operators. In Java, they can be bitwise or logical operators, depending on the context.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • @Lazarus: You're right, side-effects (which is what I was thinking about) are somewhat outside the scope of the question. Unless, of course, the side effects of `foo` affect the result of `bar`, or vice versa. – Oliver Charlesworth Jul 08 '11 at 12:52
  • @Lazarus: In terms of the post-increment, C++ works in terms of [sequence points](http://en.wikipedia.org/wiki/Sequence_point). There are two modifications to `i`, but no intermediate sequence point, so the result is undefined. – Oliver Charlesworth Jul 08 '11 at 12:53
  • 1
    An interesting piece on sequence points but let's face it, your post-increment statement isn't just an edge-case, it's an off the edge case ;) – Lazarus Jul 08 '11 at 14:19
5

Java specifies more about the order of evaluation of expressions than C++, and C++ says that you get undefined behavior if any of the legal evaluation orders of your expression modify an object twice between sequence points.

So, i++ + i++ is well defined in Java, but has undefined behavior (no diagnosis required) in C++. Therefore you can't blindly copy expressions from Java to C++.

For bitwise operators, Java specifies two's-complement representation of negative numbers whereas C++ doesn't. Not that you're likely to encounter another representation in C++, but if you did then you would find for example that -2 | 1 is always -1 in Java, but is -2 in a C++ implementation using 1s' complement.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

I believe that operator precedence is consistent between C++ and Java and provided the expression is deterministic then it should evaluate the same way.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
  • Agreed, there is the bitwise operators which I'm not familiar with in Java but providing the >> was translated to >>> the precedence of operators in an expression involving only primitives (as indicated by the OP) would surely still be fairly consistent (assuming the implementations followed spec). – Lazarus Jul 08 '11 at 12:03
1

See this:

int a = 0;

int b = (a++) + (a);

If you print b then it will output 0 in C++ whereas 1 in Java

Community
  • 1
  • 1
Dhaval Kapil
  • 385
  • 3
  • 10
1

The right-shift operator >> is different. In Java it's always an arithmetic shift, i.e. it copies the sign bit into the leftmost bit, whereas in C++ it's implementation-defined whether it's an arithmetic or logical shift. You can get a logical shift in Java using >>>, which doesn't exist in C++.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • Does implementation-define mean per-compiler, or per-OS? If I test gcc on Windows, will it word the same on Linux? Or can't I rely on that either? – Sebastien Diot Jul 08 '11 at 18:58
  • You just need to read the documentation for your compiler. Most modern compilers will give you an arithmetic right-shift for signed integers, so I'd be surprised if gcc behaves differently on Windows and Linux, but you just need to read the docs to be certain. – Graham Borland Jul 08 '11 at 19:21
0

There must be lot of difference, because in Java almost all numeric types are always signed. Read Java Puzzlers book and Java Language Specification to understand all the subtle differences.

Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51