Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 10.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Java and C++, The Difference An introduction Unit - 00.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object-Oriented Programming in C++ More examples of Association.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Workin’ with Pointas An exercise in destroying your computer.
CS 261 – Data Structures Introduction to C Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Variables and memory addresses
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
C++ -> Java - A High School Teacher's Perspective David B. Levine and Alyce Brady (modified by Mike Scott)
Chapter 9 Introduction to Arrays Fundamentals of Java.
A Sample Program #include using namespace std; int main(void) { cout
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Pointers and References
C++ -> Java - A High School Teacher's Perspective
group work #hifiTeam
Dynamically Allocated Memory
Dynamic Memory Allocation Reference Variables
Java Programming Language
Pointers, Dynamic Data, and Reference Types
Created by Hwansoo Han Edited by Ikjun Yeom
Dynamic Memory.
SPL – PS3 C++ Classes.
Presentation transcript:

Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing?? How are parameters passed?? What makes Java “safer”??

Hello World //Java public class Hello {public static void main() {System.out.println(“Hello world”); } } //C++ #include using namespace std; int main() { cout << “Hello world” << endl; return 0; }

Object oriented design is very important in Java. All code should be either an application (client) class or a support class. Many classes come with Java. Some do not need to be imported. Example: System (always available, used for console output and other utilities)

Some aspects of Java are very similar to C++ semantics (reserved words) syntax decision statements control statements What do the following loops do?

Control Structures //C++ int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; } //Java int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; }

Variable declarations //C++ // Primitives int x; double d; bool done; // Objects Dice cube(6); //Java // Primitives int x; double d; boolean done; // Objects Dice cube = new Dice(6);

BankAccount bc1; C++Java Actually creates an instance of the class BankAccount: allocates enough memory to hold the object’s data Creates a reference to an instance of the class BankAccount. bc1 is only a variable that will hold the address of an instance of this class when memory is allocated for it. In Java this statement actually creates the object: bc1 = new BankAccount();

Assignment of objects: What does bc2 = bc1 do? A copy of the object, bc1, including all its data, is made and named bc2 in C++. bc2, a reference to an object of the BankAcct class, receives the address of the object, bc1 in Java. That is, both bc1 and bc2 refer to the same object !!

How can a copy of an object be made in Java? A “deep copy” must be made. After a new reference to an instance of the same class is declared, the programmer must copy each data field.

Linear Aggregate Structures C++ arrays No bounds checking Java arrays Bounds checking As in C++ the size of an array cannot be changed, once established. (Other classes exist for that.) In Java an array is an object, inheriting from the class Object, and is always available. Ex: int[] myInts; ( declares myInts a reference to an array) myInts = new int[50]; (allocates memory needed)

Parameter Passing C++ Value Reference const Reference Java Value (primitives) Reference (objects) In Java you can not pass a pointer by reference. Therefore, addresses can not be inadvertently changed. Of course there are no pointers - but references do the same job. There are no symbols: * or &.

while (p != NULL) { if (key == p->data) return p; p = p->next; } Searching a linked list while (p != null) { if (key == p.data()) return p; p = p.next(); } C++Java

Out with the old In C++ –Inheritance – It’s there, but more difficult to implement –Multiple implementations of Abstract Data Types – through classes only Java supports Inheritance (extends) – It’s there and it’s easy Multiple implementations through implements keyword

In with the New In C++ –Const –many uses –Operator overloading –delete – to clean up memory management In Java final – a Pascal/Ada-like use of const NO operator overloading Automatic garbage collection