BRIEF Overview ON THE MAJOR Similarities & Differences

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Programming Languages and Paradigms The C Programming Language.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C++ vs. Java: Similiarities & Differences Dr. Jeyakesavan Veerasamy Director of CS UTDesign program & CS Teaching Faculty University.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CIS 068 JAVA vs. C++ Main Differences. CIS 068 JAVA vs C++ Java is (an interpreted) write once, run anywhere language. –The biggest potential stumbling.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Game Programming in Java Dr. Jeyakesavan Veerasamy CS faculty, The University of Texas at Dallas Website:
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
CIS 068 JAVA vs. C++ Main Differences CIS 068.
C++ Lesson 1.
Object Oriented Programming in
Information and Computer Sciences University of Hawaii, Manoa
Chapter 1.2 Introduction to C++ Programming
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Data Types In Text: Chapter 6.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
BRIEF Overview ON THE MAJOR Similarities & Differences
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Static data members Constructors and Destructors
Data types Data types Basic types
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
Review: Two Programming Paradigms
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
C Basics.
Programmazione I a.a. 2017/2018.
Pointers and References
Basic Elements of C++ Chapter 2.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
C/C++ Basics.
Focus of the Course Object-Oriented Software Development
7 Arrays.
Java Basics Data Types in Java.
Chap 2. Identifiers, Keywords, and Types
C Language B. DHIVYA 17PCA140 II MCA.
Review for Midterm 3.
SPL – PS1 Introduction to C++.
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

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.

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

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

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 require the semicolon Java will provide a public default constructor if you do not define any It will initialize primitive member variables for you

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

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

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!

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

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

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

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 + - * / % ++ -- < > == >= <= && || !

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

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

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.

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.

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.

C++ vs. Java: Major Differences (in no particular order) Uses const keyword and can have 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).

C++ vs. Java: Major Differences (in no particular order) The goto keyword can be used The goto keyword does not exist in Java Labeled “break” and “continue” 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.

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). Dynamic binding: http://www.cplusplus.com/forum/general/63940/ 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 (think union as an extreme example) 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.

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.

C++ vs. Java: Major Differences (in no particular order) Strings in C++ are vectors 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.

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

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

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)

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

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

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

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

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

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

References FROM Dr. Jeyakesavan Veerasamy, Director of CS UTDesign Program & CS Teaching Faculty jeyv@utdallas.edu University of Texas at Dallas, USA C++ tutorials: http://www.cplusplus.com/files/tutorial.pdf, http://www.learncpp.com/ C++ reference: http://en.cppreference.com/w/ Java tutorial: http://docs.oracle.com/javase/tutorial/ Java API documentation: http://docs.oracle.com/javase/6/docs/api/