Comparison of Java and C++: Difference between revisions

Content deleted Content added
m Semantics: {{code}}
m Syntax: {{nowrap}}
Line 103:
=== Syntax ===
{{see also|Java syntax|C++ syntax}}
* [[Java syntax]] has a [[context-free grammar]] that can be parsed by a simple [[LALR parser]]. Parsing C++ is more complicated. For example, {{nowrap|<code>Foo&lt;1&gt;(3);</code>}} is a sequence of comparisons if Foo is a variable, but creates an object if Foo is the name of a class template.
* C++ allows namespace-level constants, variables, and functions. In Java, such entities must belong to some given type, and therefore must be defined inside a type definition, either a class or an [[interface (Java)|interface]].
* In C++, objects are values, while in Java they are not. C++ uses ''value semantics'' by default, while Java always uses ''reference semantics''. To opt for reference semantics in C++, either a pointer or a reference can be used.
Line 112:
|-
| <syntaxhighlight lang="cpp">
class Foo { // Declares class Foo
int x = 0; // Private Member variable. It will
// be initialized to 0, if the
// constructor would not set it.
// (from C++11)
public:
Foo(): x{0} // Constructor for Foo; initializes
{} // x to 0. If the initializer were
// omitted, the variable would
// be initialized to the value that
// has been given at declaration of x.
 
int bar(int i) { // Member function bar()