Transition from C++ to Java
description
Transcript of Transition from C++ to Java
![Page 2: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/2.jpg)
Java: even a simple program is not simple.
public class Program1
{
public static void main(String[] arg)
{
System.out.println("Hello World");
}
}
![Page 3: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/3.jpg)
Some Similarities betweenC++ and Java
• 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".
![Page 4: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/4.jpg)
Some Differences betweenC++ and Java
• Java has automatic garbage 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++.
![Page 5: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/5.jpg)
More Differences• C++ classes can be avoided. Java classes
cannot reasonably be avoided.• C++ has built in console I/O. Java has no
standard console input (but does have standard console output.)
• C++ and Java divide a program into pieces (for separate compilation) in different ways.
These require some explanation.
![Page 6: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/6.jpg)
C++ classes can be avoided.Java classes cannot reasonably be
avoided.
![Page 7: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/7.jpg)
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) {
![Page 8: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/8.jpg)
In Java, every method is a member of
some class.
You cannot have a freestanding (global) function in Java.
![Page 9: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/9.jpg)
You can fake a "no classes" program in Java by making all
methods static.
But don’t do it!
![Page 10: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/10.jpg)
A Sample Java Class
![Page 11: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/11.jpg)
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; }
![Page 12: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/12.jpg)
public void writeOutput() { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); }
}
![Page 13: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/13.jpg)
C++ has built in console I/O.
Java has no standard console input
(but Java does have standard console output.)
![Page 14: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/14.jpg)
C++: has cin, cout, cerr
Java has: System.out.print and System.out.printlnbut NO console input.
Solutions?
![Page 15: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/15.jpg)
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
![Page 16: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/16.jpg)
C++ and Java divide a program into pieces (for separate
compilation) in different ways.
![Page 17: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/17.jpg)
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.
![Page 18: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/18.jpg)
• Java: A compilation unit is always a class definition.
• Every class is in a separate file (except for some special cases).
• No header files. • Normally, you have no one file
programs in Java.
![Page 19: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/19.jpg)
More Subtle Differences• C++ has pointer types.
– 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.
![Page 20: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/20.jpg)
Java has no pointer types
• But Java does have "pointers".• 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.
• In Java primitive types are just like in C++.
![Page 21: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/21.jpg)
• In Java a primitive type variable holds values, just as in C++. int n = 42;
• Java a class type variable contains a reference ("pointer") to the object (value).
• However, this is all automatic. There are no pointer types as such in Java. PetRecord myDog = new PetRecord("Fido", 3);
Note that all class objects are created dynamically.
![Page 22: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/22.jpg)
Assignment (=) and equality comparison (==) have minor differences.
• On primitive (simple) types, = and == are the same in C++ and Java.
• In Java, = and == on classes (or arrays) are comparing references ("pointers"),
• and you cannot overload (redefine) = and == in Java.
![Page 23: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/23.jpg)
Assignment (=) and equality comparison (==) have minor differences.
• If (n = 0) ….• In C++ this is probably an error with no
error message, assuming you meant to use ==.
• In Java this generates a compiler error.• In Java ints neither are nor can they be type
cast to Booleans
![Page 24: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/24.jpg)
C++: a choice of parameter types.
Java: no choice of parameter types.• C++: Call-by-value
– void f(int n);
• C++: Call-by-reference– void f(int& n);
• Other C++ variants:– void f(const int& n);– void f(const int n);
![Page 25: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/25.jpg)
C++: a choice of parameter types.
Java: no choice of parameter types.
• Java all parameters are call-by-value.
• But, it is almost like there are different parameter types for primitive types and classes.
![Page 26: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/26.jpg)
Java: no choice of parameter types,but
• All primitive type parameters are automatically call-by-
value.public void f(int n){...}
• All class types are automatically something very much like call-by-reference.public void f(String n){...}
![Page 27: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/27.jpg)
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.• All parameters are call-by-value of a
reference.
![Page 28: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/28.jpg)
C++: a choice of parameter types.
Java: no choice of parameter types.• 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.
![Page 29: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/29.jpg)
Java: no choice of parameter types.public void change(PetRecord r){ r.name = "FooFoo";}This really changes its PetRecord argument.public void change(int n){ n = 42;}This does not change its int argument.
![Page 30: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/30.jpg)
Java: no choice of parameter types.
public void change(int n){
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.
![Page 31: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/31.jpg)
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.
![Page 32: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/32.jpg)
public class Stuff{ private int n; .... public void changeTheN(Stuff s) { s.n = 42; }}
![Page 33: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/33.jpg)
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"
![Page 34: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/34.jpg)
AP Exception Requirements
• Covers exceptions as error messages.
• Does not cover try/throw/catch.
• Does not cover throws clause (declaring exceptions).
![Page 35: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/35.jpg)
Exception handling in Java
Fake it with "magic formulas" approach:public class TextFileOutputDemo
{public static void main(String[] arg) throws IOException{
PrintWriter outputStream = new PrintWriter(…)); outputStream.println("To file");
![Page 36: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/36.jpg)
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");
![Page 37: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/37.jpg)
Style Comparison C++/Java
Java uses loooong names:
e.g. FileNotFoundException
while C++ uses some abbreviations
Java spelling conventions:
ClassName, variableName, methodName,LITERAL_NAME
Java has an official commenting style:
javadoc
![Page 38: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/38.jpg)
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.
*/
![Page 39: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/39.jpg)
Getting a Java CourseOff-the-Ground
Need some "magic formulas," but
Move to real classes quickly.
Do something about console input:add console input classuse JOptionPaneuse magic formulas
![Page 40: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/40.jpg)
"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.
![Page 41: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/41.jpg)
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.
![Page 42: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/42.jpg)
GUIs (Graphical User Interfaces, i.e., Windowing Interfaces)
![Page 43: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/43.jpg)
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.
"Regular Windowing Systems":
Swing Library is the latest version.
![Page 44: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/44.jpg)
Java SoftwareJava 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.
![Page 45: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/45.jpg)
Java Software for MacGood (free?) Java compiler for Mac OS X
(I’m told): http://developer.apple.com/java/
Some of the good IDE’s for Mac Code Warrior, BlueJ.
JJ Works for any operating system.
![Page 46: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/46.jpg)
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/
![Page 47: Transition from C++ to Java](https://reader036.fdocuments.in/reader036/viewer/2022062304/56814094550346895dac2a16/html5/thumbnails/47.jpg)
Text Books
Lots to choose from.
For example,
Walter Savitch Java: An Introduction to Computer Science and Programming, Prentice-Hall.