Transition From C++ To Java: Walt Savitch University of California, San Diego Wsavitch@ucsd - Edu
Transition From C++ To Java: Walt Savitch University of California, San Diego Wsavitch@ucsd - Edu
Walt Savitch
Simple (primitive) types: int, double, char Control Structures if-else, switch, while, for Arithmetic expressions Both have a string type: C++ string, Java String. Arrays Both have classes. Both have a "main".
collection. C++ does not. C++ has operator overloading. Java does not. C++ says "function". Java says "method". These require no explanation, unless students already know C++.
More Differences
Every compilation unit in Java is a class. A program is a class with a method named main:
public class Program1 { public static void main(String[] arg) {
You can fake a "no classes" program in Java by making all methods static.
public class PetRecord { private String name; private int age;//in years
public PetRecord(String initName, int initAge) { name = initName; if ((initAge < 0)) System.out.println("Error"); else age = initAge; }
public void writeOutput() { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); }
Solutions:
AP does not require console input. There are classes for console input
that are not part of Java but written in Java:
e.g., SavitchIn.readInt()
JOptionPane, simple GUI I/O
C++ and Java divide a program into pieces (for separate compilation) in different ways.
C++: Traditionally has an interface (header) file, implementation file(s), application (driver) file. C++: Can confine a program to a single file if you want.
Java has no pointer types . Assignment (=) and equality comparison (==) have minor differences. C++ gives a choice of parameter types. Java: No choice of parameter types. Exception handling can be avoided in C++ Exception handling is needed for some fundamental things in Java, e.g. file I/O.
C++: Call-by-value
void f(int n);
C++: Call-by-reference
void f(int& n);
Java all parameters are call-by-value. But, it is almost like there are different
parameter types for primitive types and classes.
Java: no choice of parameter types, but All primitive type parameters are automatically call-byvalue.
public void f(int n) {...}
C++: a choice of parameter types. Java: no choice of parameter types. Java Full Story:
In Java primitive types are just like in C++. In Java class (and array) types are REFERENCE
TYPES. A reference is a "pointer". All class values in Java are handled as references, but it is all automatic.
Java Full Story: In Java all parameters are call-by-value. Parameter is a local variable initialized to the value of the argument. Primitive types no surprises. Class type (local) variables hold references. Class parameters are call-by-value of a reference.
n = 42;
} This does not change its int argument.
There is no way to write a Java method that has a parameter for an int variable and that changes the value of an argument variable.
There is no way to write a Java method that has a parameter for an int variable and that changes the value of an argument variable. So, how do you manage to cope?
int n = computeNewValue(); OR use class objects.
public class Stuff { private int n; .... public void changeTheN(Stuff s) { s.n = 42; } }
Exception handling can be avoided in C++ Exception handling is needed for some fundamental things in Java, e.g. file I/O.
Solutions: AP requirements do not include file I/O. Teach exception handling. Fake it with "magic formulas"
AP Exception Requirements
Covers exceptions as error messages. Does not cover try/throw/catch. Does not cover throws clause
(declaring exceptions).
{
public static void main(String[] arg) throws IOException {
public class TextFileOutputDemo { //without magic formula: public static void main(String[] arg) { PrintWriter outputStream = null; try { outputStream = new PrintWriter( new FileOutputStream("out.txt")); } catch(FileNotFoundException e) {} outputStream.println("To file");
javadoc
Extracts an interface from a class definition. May not need full blown details for AP course, but be consistent with javadoc. Comments before method headings: /** javadoc comment style. */
"Magic Formulas"
public class ProgramName { public static void main(String[] arg) {
means "begin". Use this to explain simple flow of control then quickly move to classes and explain what this means.
Console Input
You need to do something. Use SavitchIn or some other console input class or Use a very messy magic formula or Explain the formula (still messy) or Use JOptionPane.
GUIs
Not part of the AP requirements. Applets: Designed to be used over the
internet. Can be used for ordinary programs, but have some problems and no easier than regular windowing systems.
Java Software
Java is well standardized. SDK (aka JDK) Java compiler is free. java.sun.com Works well with Windows and Unix: Want Java 2, version 1.4 or higher (Standard Edition is enough) Mac users have traditionally had limited choices, but things are better now. JJ works with all operating systems.
Some of the good IDEs for Mac Code Warrior, BlueJ. JJ Works for any operating system.
IDEs
Windows:
TextPad (shareware): www.textpad.com use with Sun SDK Forte (free): java.sun.com Borland: www.borland.com Mac: BlueJ (free): www.bluej.org CodeWarrior: www.metrowerks.com JJ: Works with all operating systems. www. .LearnJavaNow.org/
Text Books
Lots to choose from. For example, Walter Savitch Java: An Introduction to Computer Science and Programming,
Prentice-Hall.