CS 24L Object-Oriented Programming

[Home]

Comparing the Syntax of C++ to Java

One of the advantages for programmers versed in C++ is the similarity of the Java syntax to that of C++. However, there are still some major differences, notably in the area of reserved keywords. The tables below provide brief summaries and comparisons between the two languages. Note that this is a quick reference and that you still need to consult the appropriate Java/C++ text for the correct language usage.

a. Keywords

The table below describes some of the important Java keywords (the list is not complete).
 
C++
Java
Brief Description
break break Break out of while and for loops.
case case The case statement.
class Test class Test Start of the class definition for Test.
const int a = 1; final int a = 1; Constant a is assigned the value of 1.
continue continue Skip the rest of the statements and continue the while or for loop.
else else The else statement.
for for The for loop.
if if The if statement.
#include <stdio.h> import java.io.*; Including class libraries.
a = new aClass; a = new aClass(); The creation of an instance of the class aClass.
private:
  void aMethod( void );
  int a;
private void aMethod();
private int a;
Private method aMethod and private field a.
protected:
  void aMethod( void );
  int a;
protected void aMethod();
protected int a;
Protected method aMethod and protected field a.
public:
  void aMethod( void );
  int a;
public void aMethod();
public int a;
Public method aMethod and public field a.
switch switch The switch statement.
virtual aMethod( void ) = 0; abstract void aMethod(); Abstract method aMethod.
class A : public B class A extends B Class A inherits the attributes of class B. Single inheritance for Java and multiple inheritance for C++.
try
{
  <statements>
}
catch( anException e)
{
  <statements>
}
try
{
  <statements>
}
catch( anException e)
{
  <statements>
}
finally
{
  <statements>
}
Creating exception handlers.
class A : public B, public C class A implements B, C Class A implements the interfaces B and C. In the C++ case, the classes B and C are abstract classes.
namespace A { } interface A { } Creating an interface (grouping related functions).
while while The while loop.

b. Operators

The table below lists some of the Java operators (the list is not complete).
 
C++
Java
Description
()
()
Parentheses that do the following: 
  • Group expressions (changes associativity and precedence).
  • Isolate the conditional expressions in if, while and for statements.
  • Indicate the method calls and parameters.
[]
[]
Indicate the array subscripts.
.
.
Message passing and direct access to class fields.
!
!
Logical negation.
++
++
Increment.
--
--
Decrement.
*
*
Multiplication.
/
/
Division.
%
%
Modulus.
+
+
Addition.
-
-
Subtraction.
>
>
Greater than.
>=
>=
Greater than or equal to.
<
<
Less than.
<=
<=
Less than or equal to.
==
==
Equal to.
!=
!=
Not equal to.
&&
&&
Logical AND (conjunction).
||
||
Logical OR (disjunction).
=
=
Assignment.
+=
+=
Addition followed by assignment.
-=
-=
Subtraction followed by assignment.
*=
*=
Multiplication followed by assignment.
/=
/=
Division followed by assignment.
%=
%=
Modulus followed by assignment.

c. Operator Precedence

The operators with the highest precedence are listed first.

highest        ()    []    .
         ++    --    ~     !
         *     /     %
         +     -
         >>    >>>   <<
         >     >=    <     <=
         ==    !=
         &
         ^
         |
         &&
         ||
lowest        =     +=     -=     *=     /=     %=

d. Primitive Types

Java guarantees the same size for each primitive type, no matter which operating system or hardware is being used. On the other hand, the sizes of the C++ primitve types depend on the underlying hardware and compiler. However, the relative sizes are guaranteed in both cases. For example, the size of an int is always smaller than or equal to a long.

Note that since the sizes vary in C++, the comparison below is strictly from a functional view only.
 
C++
Java
Description
char
byte
Java – 1 byte 
char
char
Java – 2 bytes
short
short
Java – 2 bytes 
int
int
Java – 4 bytes 
long
long
Java - 8 bytes 
float
float
Java – 4 bytes 
double
double
Java – 8 bytes 
bool
boolean
Not available.


Copyright © 1999-2003, Colin Depradine. All rights reserved.