0% found this document useful (0 votes)
123 views

Transition From C++ To Java

- C++ and Java share many basic features like primitive types, control structures, and classes, but differ in key ways like memory management, parameter passing, and exception handling - In Java, everything must be defined within a class, while C++ allows programs without classes; Java uses pass-by-value for all parameters while C++ offers pass-by-reference - Java handles memory automatically through garbage collection while C++ requires manual memory management; Java also requires exception handling for I/O while C++ allows exceptions to be avoided
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Transition From C++ To Java

- C++ and Java share many basic features like primitive types, control structures, and classes, but differ in key ways like memory management, parameter passing, and exception handling - In Java, everything must be defined within a class, while C++ allows programs without classes; Java uses pass-by-value for all parameters while C++ offers pass-by-reference - Java handles memory automatically through garbage collection while C++ requires manual memory management; Java also requires exception handling for I/O while C++ allows exceptions to be avoided
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Transition from C++ to Java

Java: even a simple program is not simple.


public class Program1 { public static void main(String[] arg) { System.out.println("Hello World"); } }

Some Similarities between C++ 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".

Some Differences between C++ 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++.

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.

C++ classes can be avoided. Java classes cannot reasonably be avoided.

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) {

In Java, every method is a member of some class. You cannot have a freestanding (global) function in Java.

You can fake a "no classes" program in Java by making all methods static.

But dont do it!

A Sample Java Class

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"); }

C++ has built in console I/O. Java has no standard console input

(but Java does have standard console output.)

C++: has cin, cout, cerr Java has:


System.out.print and System.out.println

but NO console input. Solutions?

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: 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.

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.

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++.

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.

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.

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

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);

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.

Java: no choice of parameter types, but All primitive type parameters are automatically call-byvalue.
public void f(int n) {...}

All

class types are automatically something very much like call-byreference.


public void f(String 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.

All

parameters are call-by-value of a reference.

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.

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.

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.

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:

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");

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");

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

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. */

THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy