Understanding the similarities and differences between Java, C, and C++ simplifies learning the Java language. The similarities and differences can be seen in some simple examples.
The examples illustrate the similarities listed below. These are things you do not have to spend much time learning — you already know a lot about them. You only need to focus on some minor differences.
The examples also illustrate some of the important differences that are listed below. These are things you will need to focus your attention on.
The similarities between C, C++, and Java are made apparent by comparing three programs that do the same thing:
These programs each print all of the Fibonacci numbers that can be represented with 32-bit signed numbers.
The following are things you do not have to spend much time learning — you already know a lot about them. You only need to focus on some minor differences.
The following are things you will need to focus your attention on when learning Java. The first two are discussed in this web presentation. The last three are discussed in separate presentations.
Other than literal values, all Java data is accessed through variables, which can be associated with objects (instance variables), classes (class variables), or instantiations of methods (parameters and local variables). All variables are typed as a simple type, or a class, or an interface. An interface is like a class whose methods are declared but not defined. That is, the methods are named, and types are specified for their returned values and parameters, but no code is provided to define their behavior.
Java variables with simple types contain value copies. Although two variables may contain the same value, they will have two distinct copies. If one of the variables is changed, it has no effect on the other. Thus the behavior of simple Java variables is similar to their C counterparts.
Java object variables, on the other hand, are references to objects. Two object variables may refer to the same object. If the object is modified then the change can be seen through both variables.
The only mechanism for changing the value of a simple Java variable is an assignment statement. Java assignment syntax is identical to C assignment syntax. As in C, an assignment replaces the value of a variable named on the left-hand side of the equals sign by the value of the expression on the right-hand side of the equals sign.
Java object variables can be changed in two ways. Like simple variables, you can make assignments to object variables. When this is done the object referenced by the variable is not changed. Instead, the reference is replaced by a reference to a different object.
With a few exceptions, the only other thing that you can do with an object variable is to send it a message. This is an important part of any Java program, allowing communication between objects.
In it simplest form, a Java class definition has the following structure.
public class A { object and class member definitions }
The object and class member definitionsare method (including constructor), variable, and constant definitions, which are like C function, variable, and constant definitions with the exception of some keyword modifiers as described below.
There is one important difference between Java member definitions and their C counterparts: Java does not require that class members be defined prior to their use. A Java compiler works like an assembler in that it makes multiple passes through a code file. The early passes are just recording symbols and types associated with them.
There is a price to pay for this added freedom: it complicates error handling by the compiler. Error messages from the compiler can be frustrating because as you fix errors, new errors are uncovered. It is not uncommon to get an early report of a small number of errors and a large number of errors after the first errors are fixed.
In Java, members may be associated with either objects or classes.
Members that are associated with objects are usually called
instance members.
In a class definition, all members are instance members except those
that are qualified by the static
keyword.
In a message that accesses an instance member, the receiver is specified
by a variable or expression that references an object.
In a message that accesses a class member, the receiver is specified by
the name of a class.
The most important variables that are defined in a class are
instance variables.
Instance variables are attached to objects so that each object in a
class has its own instance variables.
All variable declarations are declarations of instance variables unless
qualified by the static
keyword.
The static
keyword indicates that the variable is a
class variable.
A class variable is attached to the class in which its definition
appears so that its value or reference is shared by all objects in the
class.
The following declaration declares x
to be a class variable
of type int
.
static int count;
If this declaration appears in class A
then the variable is
accessed with the message A.count
.
This message returns the value of the variable, which may be part of a
more complex expression, such as
System.out.println(A.count);
Here, out
is a variable of the System
class.
Like variables, methods are instance methods except when declared as
class methods using the keyword static
.
If class A
defines method f() as static then messages using
the method must be sent to the class as in
x = A.f();
Unlike C, Java uses the keyword final
to declares
constants.
Many constants are also class variables, so they are often declared like
static final int taxRate;
Java uses the keywords public
, protected
, and
private
to modify access to members in a class definition.
These keywords determine the kinds of scope from which a member can be
accessed.
There is also a default scope for members that are defined without an
access keyword.
A scope is a limited portion of source code that provides a context for interpreting identifiers or names. An identifier can refer to different things depending on the scope in which it appears. This is one of the important mechanisms for encapsulation.
As in C, the scope for a local variable or parameter is the method or
innermost block in which it is declared.
A block scope is either a control statement or a segment of code
delimited by braces.
For example, the scope for the local variable i
in the
following code is the entire for
statement.
The scope of the local variable x
is the region enclosed by
the braces.
for (int i = 0; i < 100; i++) { double x; . . }
There are two scopes in Java that are not available in C: class scopes and package scopes.
To be used in a package scope, a source code file should contain a statement with the following form.
package package-name;
Here, package-nameis the name of the package. Rules for naming class source code files and packages are described in the section "Naming Java Source Code Files and Packages".
Access to a member of a Java class can be limited to a scope using one
of the keywords public
, protected
, or
private
, or to a default scope if none of these keywords is
used.
If an access keyword is used, it should precede the type for the member.
A
is declared
with the public
keyword, it can be accessed through any
reference to an object from class A
.
A
is declared
with the protected
keyword, it can be accessed only
within the package that contains A
or in the class
definition of a subclass of A
.
A
is declared
with the private
keyword, it can be accessed only in the
class definition of A
.
A
is declared
without one of these keywords, it can be accessed only within the
package that contains A
.