Presentation is loading. Please wait.

Presentation is loading. Please wait.

BRIEF Overview ON THE MAJOR Similarities & Differences

Similar presentations


Presentation on theme: "BRIEF Overview ON THE MAJOR Similarities & Differences"— Presentation transcript:

1 BRIEF Overview ON THE MAJOR Similarities & Differences
C++ vs. Java BRIEF Overview ON THE MAJOR Similarities & Differences

2 History C (1969)  C++ (1979)  Java (1995) Both support OOP.
Syntax is very close – Java has strong influence of C/C++. Easy to learn the other language when you know one of these.

3 C++ compiler & Linker usage
file1.cpp file2.cpp filen.cpp file1.o file2.o filen.o Linker application (executable) Compiler This application runs directly on top of OS. C++ compiler does not care about filenames.

4 Java Runtime Environment (JRE)
Java compiler usage Operating System Java Runtime Environment (JRE) file1.java file2.java filen.java file1.class file2.class filen.class Compiler

5 Simple classes and objects in Java & C++
class C { private: int a; public: void setA(int a) { this->a = a; } int getA() { return a; } }; public class C { private int a; public void setA(int a) { this.a = a; } public int getA() { return a; } }

6 Class Definitions In C++, a visibility specifier (public:, protected:, or private:) sets off a section of member declarations If missing, the members have private visibility In C++, the body of the class definition should end with a semicolon ; after the closing brace } C++ will provide a public default constructor if you do not define any It is not guaranteed to initialize primitive member variables for you In Java, a visibility specifier (public, protected, or private) is attached to each member declaration If missing, the member has “package” visibility Java does not required the semicolon Java will provide a public default constructor if you do not define any It will initialize primitive member variables for you

7 Simple classes and objects in C++ & Java
#include <iostream> int main() { C* x; x = new C(); x->setA(5); std::cout << x->getA()<< std::endl; } Package edu.fit.camorde.examples; public class Hello { public static void main(String[] args) C x; x = new C(); x.setA(5); System.out.println(x.getA()); }

8 Variable declarations have similar syntax…
If C is a class in C++: Declares x to be of type C. Creates an object that is an instance of the class C, using the default constructor of the class. x directly names this object; it is not a pointer. If C is a class in Java: Declares x to be of type C. Does not create an object that is an instance of the class C. It creates a pointer that can point to such an object, but does not create any object. C x; C x; …but could have very different semantics

9 C++ treats primitive types and classes similarly…
Compare the semantics of these declarations in C++ vs. Java: C x; int a; … Java treats primitive types and classes differently!

10 Pointers In C++, you have to explicitly specify that the variable is a pointer by using * (the dereference operator) The -> operator dereferences a pointer and accesses a member of the object pointed to. You may use the dereference operator * and the . operator: In Java, declaring a variable of class type creates a pointer that can point to an instance of that type (an object). The dot operator . dereferences a pointer and accesses a member of the object pointed to. C *x; x = new C(); x->setA(5) ; C x; x = new C(); x.setA(5); (*x).setA(5);

11 Memory Management in Java and C++
In C++, objects can be created using new. Memory for such objects is not automatically reclaimed when it can no longer be accessed in your program. Note: automatic variables in a C++ program are reclaimed automatically. In Java, all objects are created using new. Memory an object takes is automatically reclaimed by the garbage collector when it can no longer be accessed. //create an object C* x = new C(); x = 0; //object is inaccessible; PROBLEM //it will not be freed...memory leak! //create an object C x = new C(); x = null; //object is inaccessible; //No problem! gc will free it

12 The size of variables C++ doesn’t specify the size of its types as precisely as Java does. For example, an int in a C++ program might be 16 bits, or 32 bits, or even 64 bits C++ provides a way to tell how much memory a type takes: the sizeof() operator For any variable x, the expression sizeof(x) has an integer value equal to the number of bytes to store x For any typename T (even a user-defined type), the expression sizeof(T) has an integer value equal to the number of bytes (not bits) that it takes to store a variable of type T Primitive Types C++ Java char >= 1 byte 2 bytes short int >= 2 bytes signed int 4 bytes signed long int >= 4 bytes 8 bytes float double >= 8 bytes

13 Arithmetic and Boolean Operators
C++ has all the arithmetic operators that Java has, and they work similarly: C++ has all the comparison and boolean operators that Java has, too: However, these operators work a bit differently, because of how C++ deals with boolean values One important feature of C++ operators is that they can be overloaded: you can write your own definitions of them + - * / % ++ -- < > == >= <= && || !

14 Boolean Expressions C++ provides a primitive type bool, and literals true, false However, in every context these are equivalent to integral values where 0 means "false", and any nonzero value means "true" In Java, the test expression for each control flow construct must return an actual Boolean value (true or false).

15 C++ Templates & Java Generics
template <typename T> class C { public: C():a(0) { } C(T* _a):a(_a) { } T* getA() const { return a; } void setA(T* _a) { a = _a; } private: T* a; }; public class C<T> { public C() { a = null; } public C(T _a) { a = _a; } public T getA() { return a; } public void setA(T _a) { a = _a; } private T a; }

16 C++ vs. Java: Major Differences (in no particular order)
WOCE Write once, compile everywhere  unique executable for each target. WORE Write once, run everywhere  same class files will run above all target-specific JREs. No strict relationship between class names and filenames: a header file and implementation file are used for each class, typically. Strict relationship is enforced: Source code for class PayRoll has to be in PayRoll.java.

17 C++ vs. Java: Major Differences (in no particular order)
I/O statements use cin and cout: cin >> x; cout << y; I/O input mechanism is bit more complex default mechanism reads one byte at a time (System.in) Output is easy: System.out.println(x); Automatic Garbage Collection for automatic variables Explicit memory management for dynamic memory allocation. Supports destructors. Automatic Garbage Collection.

18 C++ vs. Java: Major Differences (in no particular order)
Composite data types are accomplished through the use of class, struct, union, and typedef. Composite data types are accomplished in Java exclusively through the use of class definitions. The struct, union, and typedef keywords have all been removed in favor of classes. Allows multiple inheritance. Java classes are singly inherited Some multiple-inheritance features provided through interfaces. Can use template classes. Java does not have template classes but generic classes can be created.

19 C++ vs. Java: Major Differences (in no particular order)
Uses const keyword and can pass by const reference. Java does not include const keyword nor the ability to pass by const reference explicitly. Constants can be created by using the final modifier when declaring class and instance variables. The if, while, for, and do-while statements are syntactically the same The test expression can return an integer, not just Boolean values. The test expression must return an actual Boolean value (true or false).

20 C++ vs. Java: Major Differences (in no particular order)
The goto keyword can be used The goto keyword does not exist in Java Labeled breaks and continues to break out of and continue executing complex switch or loop constructs There are functions that are not tied to classes. All functions must be methods. There are no functions that are not tied to classes. Supports operator overloading. Specifically operator overloading was thrown out. Support default arguments. Does not support default arguments.

21 C++ vs. Java: Major Differences (in no particular order)
The programmer has to decide whether or not to use dynamic binding (using the virtual keyword). There’s no virtual keyword in Java because all non-static methods always use dynamic binding. The primitive data types can be cast to objects and vice versa. The primitive data types cannot be cast to objects or vice versa Automatic casting occurs only when there will be no loss of information. All other casts must be explicit.

22 C++ vs. Java: Major Differences (in no particular order)
All C++ primitive data types may have different sizes and behavior across platforms and operating systems. All Java primitive data types have consistent sizes and behavior across platforms and operating systems. There are unsigned integer data types. There are no unsigned data types (except char, which is a 16-bit unsigned integer). Pointers, references, and pass by value are supported. Primitive data types always passed by value. Objects are passed by reference.

23 C++ vs. Java: Major Differences (in no particular order)
Strings in C++ are arrays of characters, terminated by a null character (‘ \0’). To operate on and manage strings, you treat them as you would any other array. Must keep track of pointer arithmetic Must be careful not to stray off the end of the array. Strings in Java are objects, and all methods that operate on strings can treat the string as a complete entity. Strings are not terminated by a null Like arrays, string boundaries are strictly enforced. No array bound checking. Array bounds are always checked.

24 C++ vs. Java: Major Differences (in no particular order)
Has a preprocessor and can use #defines or macros. Does not have a preprocessor, and as such, does not have #defines or macros. Command-line arguments first element in the argument vector (argv[0]) in C++ is the name of the program itself; Using command-line arguments, the first argument is the first of the additional arguments. There is no way to get hold of the actual name of the Java program There’s a scope resolution operator :: There’s no scope resolution operator ::

25 Types of Memory used by Executable Task
data (static) heap (dynamic) Code Stack

26 Program Memory stack heap bss data text ← contains automatic variables
← shared by all threads, shared libraries, and dynamically loaded modules in a process uninitialized data bss ← contains all global variables and static variables that are initialized to zero or do not have explicit initialization (Block Started by Symbol) initialized data data ← contains any global or static variables which have a pre-defined value and can be modified text ← contains any constant variables along with program code (Read-only)

27 Objects cannot be in the stack
Objects can be created as local variables just like any basic data types in C++, unlike Java. C++: Java: ComplexType num1; nothing equivalent Objects cannot be in the stack

28 Objects in Heap C++: Java: ComplexType *num1 = new ComplexType(…);

29 Arrays C++: Java: nothing equivalent
Basic data types and classes are treated the same way in C++, unlike Java. C++: Java: ComplexNumber numbers[5]; nothing equivalent

30 Arrays C++: Java: ComplexNumber *numbers;
Nothing equivalent for classes in Java, but possible for basic data types: ComplexNumber *numbers; numbers = new ComplexNumber[5]; C++: Java: int[] numbers; numbers = new int[5];

31 Array of arrays C++: Java: ComplexNumber **numbers;
numbers = new ComplexNumber*[5]; for ( index i = 0 ; i < 5 ; i++ ) numbers[i] = new ComplexNumber(…); C++: Java: ComplexNumber[] numbers; numbers = new ComplexNumber [5]; for ( index i = 0 ; i < 5 ; i++ ) numbers[i] = new ComplexNumber(…);

32 The biggest potential stumbling block is…
Speed? Interpreted Java runs something like 20 times slower than C++. Compiler optimizers available. Nothing prevents the Java language from being compiled and there are just-in-time compilers appearing which offer significant speed-ups. A JIT turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor. A JIT compiler runs after the program has started and compiles the code on the fly (or just-in-time, as it's called) into a form that's usually faster like the host CPU's native instruction set.

33 References FROM Dr. Jeyakesavan Veerasamy, Director of CS UTDesign Program & CS Teaching Faculty University of Texas at Dallas, USA C++ tutorials: C++ reference: Java tutorial: Java API documentation:


Download ppt "BRIEF Overview ON THE MAJOR Similarities & Differences"

Similar presentations


Ads by Google