Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between the Java and C++ operators?

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?

like image 774
Sebastien Diot Avatar asked Jul 08 '11 11:07

Sebastien Diot


People also ask

Is Java very different from C?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.

Do Java and C have the same syntax?

Java is a statically typed object-oriented language that uses a syntax similar to (but incompatible with) C++. It includes a documentation system called Javadoc. Extends C with object-oriented programming and generic programming. C code can most properly be used.


1 Answers

  • 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.

like image 131
Oliver Charlesworth Avatar answered Oct 16 '22 18:10

Oliver Charlesworth