|
Department of Engineering |
|
|
Java Course and CUED's C++ courses
This page covers issues related to CUED's C++ courses (for those
confident enough about C++ to use it as a basis for learning Java).
Other pages
of CUED java support include
Many of the basic programming concepts coverered in CUED's 1A and 1B C++
courses will be of use when learning Java ( up to about chapter 5 of
David J. Eck's Introduction to Programming Using Java). Computer arithmetic, the
use of for, while, if, etc and the idea
of classes with public/private parts are similar in both languages.
In some ways Java is a stricter (safer) language than C++. Also Java
forces you to think in terms for classes and object-orientation, so
you'll need to have understood the 1B C++ computing course before
being able to transfer your knowledge to Java. In Java everything
except the primitive types are objects all sharing a common
ancestor java.lang.Object, which has a few methods (toString for example) which all objects inherit.
The more C++ you know, the more similarities and differences you'll
find between the languages. Java was designed to be a "simple,
object-orientated, distributed, interpreted, robust, secure,
architecture neutral, portable, high-performance, multithreaded, and
dynamic language". Some of these design criteria limit Java's functionality
or make apparently simple tasks complex (if you want a program to work the
same way on many types of machines you either need to go for the lowest-common-denominator, or write complicated code).
Here are some examples of similarities and surprising differences that C++
programmers might notice when learning Java
- Arrays - Arrays are objects but you can't make subclasses from
them. Java arrays have bounds-checking - the following code will
terminate with an exception (actually, an ArrayIndexOutOfBoundsException exception). Note that without exceptions it would be rather
awkward to deal with bounds-checking.
class Program14 {
public static void main(String[] args) {
int int_array[];
int_array=new int[3];
for (int i=0;i<5; i++)
System.out.println(int_array[i]);
}
}
- Strings -
As illustrated by the first example, you can concatenate anything onto a
Java string; Java automatically converts the added part to a string.
Built-in types such as numbers are converted in the obvious way.
Objects are converted by calling their toString() methods.
Unlike C++ strings, Java Strings can't be changed. A StringBuffer
can be changed and its toString() method used to convert it.
- Objects -
All Java objects share a common ancestor. New objects are created using
new and a constructor. Most functions will be called as methods of objects (object_name.method_name) but a few methods are
defined as public static meaning that they're callable from outside
the class and they belong to the class rather than the object (so they can be
used even if no object of that class exists). Examples are
Math.sqrt() and main.
- Testing for equality -
Except for variables that have primitive types (int etc)
all Java variables are references to objects. To a C++ programmer
this can produce rather odd effects. Try running this code
import java.io.*;
class Program12 {
public static void main(String[] args) {
String s1="one";
// the following string creation is deliberately tedious
char c[] = {'o', 'n', 'e'};
String s2= new String(c);
System.out.println("s1=" + s1 + " and s2=" + s2);
System.out.print("Using == ... ");
if (s1==s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is NOT equal to s2");
System.out.print("Using the equals method ... ");
if (s1.equals(s2))
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is NOT equal to s2");
}
}
You'll find that although both s1
and s2 are
"one", the
== comparison says they're different
because s1 and
s2 refer to different strings
(though the 2 strings concerned have the same value).
-
java.util
contains several of the things that C++ has in its Standard Library.
Here's a comparison of how to use vectors in Java and C++.
Java | | C++ |
import java.util.*;
import java.io.*;
class Program9 {
public static void main(String[] args) throws Exception {
Vector v = new Vector();
String s1="apple";
String s2="banana";
String s3="currants";
v.addElement(s1);
v.addElement(s2);
v.addElement(s3);
Enumeration e = v.elements();
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
|
|
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
vector<string> v;
string s1="apple";
string s2="banana";
string s3="currants";
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
for (int i=0; i<v.size(); i++)
cout << v[i] << endl;
}
|
Java Vectors have to contain objects (rather than primitive types) but they don't all have to be of the
same type. They have various methods including
removeElement and indexOf (for searching).
- final - Java's final has various uses
- With variables it's like C++'s const
- With methods it stops the method being overridden in a subclass
(whereas abstract would force overriding)
- With classes it stops subclasses being created
- finalize - In C++ objects can have destructors which
are called at the end of the object's lifetime. These destructor
routines do some tidying up and free memory that's no longer needed.
In Java, objects can have a finalize routine (with no
return type or arguments). In Java, memory that's no longer needed is
freed up automatically (but not immediately) using 'garbage collection'
so finalize methods aren't often needed.
|
|