Variables and Java vs C++

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

From Theory to Practice 2 OOP Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Introduction to By Mati Yanko. Primary Goals of Java Portable Secured Object Oriented.
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.
1 Repetition structures Overview while statement for statement do while statement.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Peyman Dodangeh Sharif University of Technology Fall 2013.
Copyright Curt Hill Variables What are they? Why do we need them?
CS 261 – Data Structures Introduction to C Programming.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Object Oriented Programming Lecture 2: BallWorld.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
EECE 309: Software Engineering
Object Lifetime and Pointers
CMSC 202 ArrayList Aug 9, 2007.
Information and Computer Sciences University of Hawaii, Manoa
Data Types In Text: Chapter 6.
Intro to ETEC Java.
Java Course Review.
CSE 303 Concepts and Tools for Software Development
Advanced Programming Behnam Hatami Fall 2017.
Data Structures ADT List
An Introduction to Java – Part I, language basics
Overview of Memory Layout in C++
CMSC 202 ArrayList Aug 9, 2007.
Java Arrays & Strings.
Unit-2 Objects and Classes
Chapter 11: More on the Implications of Inheritance
CS 200 Loops Jim Williams, PhD.
CMSC 202 ArrayList Aug 9, 2007.
CS2011 Introduction to Programming I Arrays (II)
Assessment – Java Basics: Part 1
class PrintOnetoTen { public static void main(String args[]) {
Basics of OOP A class is the blueprint of an object.
Core Concepts.
Arrays in Java.
Dynamic Memory.
C++ File Structure and Intro to the STL
Lecture 6: References and linked nodes reading: 16.1
Methods/Functions.
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
CS 240 – Advanced Programming Concepts
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Variables and Java vs C++

What can be improved? (variables) public void goDirection(String directionName) { boolean wentToRoom = false; for (Direction direction : currentRoom.getDirections()) { if (direction.getDirectionName().equalsIgnoreCase(directionName)) { wentToRoom = true; currentRoom = direction.getDestinationRoom(); break; } if (!wentToRoom) { System.out.println("I can't go " + directionName); A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable

What can be improved? (variables) public static void main(String[] args) { String currRoomName = ""; // deals with args ... Layout mapLayout = UserInterface.LoadMap(newMapUrl); Map<String, Room> playMap = GameState.GenerateVirtualMap(mapLayout); Room currRoom = playMap.get(mapLayout.getStartingRoom()); while (continuePlaying) { currRoomName = GameState.play(currRoom); currRoom = playMap.get(currRoomName); if (currRoomName.equals("EXIT")) { continuePlaying = false; } A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable

What can be improved? A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable What can be improved? String description; String currentRoom = layout.getStartingRoom(); String done = ""; for (int i = 0; i < layout.getRooms().length; ) { int currentRoomIndex = layout.getRoomFromName(currentRoom); description = layout.getRooms()[currentRoomIndex].getDescription(); System.out.println(description); ArrayList<String> directionName = new ArrayList<String>(); for (int j = 0; j < layout.getRooms()[currentRoomIndex].getDirections().length; j++) { directionName.add(layout.getRooms()[currentRoomIndex] .getDirections()[j].getDirectionName().toLowerCase()); } String direction = getDirectionsOption(directionName);

What can be improved? (variables) public static void checkFloorPlan() throws Exception { // ... (removed stuff) for (Room currRoom : roomCollection.values()) { boolean roomFound = false; for (Direction currDirection : currRoom.getDirections()) { roomFound = roomFound || findRoomInConnecting(currRoom.getName(), roomCollection.get(currDirection.getRoom())); } if (!roomFound) { throw new BadFloorPlanJsonException("Rooms not connected."); A) Eliminating temporary variable B) Eliminating intermediate results C) Eliminating control flow variable D) Shrinking scope of variable E) Prefer write once variable

Which is better? A String originalDirectionName = input.substring(3); return "I can't go " + originalDirectionName + "\n"; B return "I can't go " + input.substring(3) + "\n";

Which is better? A String input = scanner.nextLine(); String output = gameController.handleInput(input); B String output = gameController.handleInput(scanner.nextLine());

Java and C++ Similar in: Syntax: Java was designed to use syntax similar to C++ to ease adoption Structurally: Both are object-oriented languages Different in goals: Java designed for: safety and portability C++ designed for: Control and performance

How much time did Adventure assignment take? 0 – 3 hours 4 – 6 hours 7 – 9 hours 10– 12 hours More than 12 hours

How much difficult was the Adventure assignment? Trivial Easy Reasonable Difficult Excessive

Java vs C++ Memory Java Automatic Garbage Collected C++ Automatic Heap Allocated on stack Lifetime from function Garbage Collected All arrays All Classes Allocated on the heap Lifetime as long as referenced C++ Automatic Allocated on stack Lifetime from function Heap Allocated on the heap Lifetime managed by various methods

Java vs C++ Functions Java All functions belong to a class (methods) All arguments call by value static used to allow methods to be called without an object. C++ Functions just exist Classes can have functions just like in Java called methods they then get a implicit this argument

Java Array vs C++ Array Java C++ Allocated on heap Checks bounds Think of as an object Size set when created Knows length Allocated either on heap or stack No bounds check on accesses Think of as a pointer Size set when allocated

Java Array vs C++ Vector C++ std::vector Allocated on heap Checks bounds Think of as an object Size set when created Knows length Allocated on heap Can check bounds Can extend Knows length