Section 11: Comparing Java and C

Slides:



Advertisements
Similar presentations
A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett.
Advertisements

This lecture is a bit of a departure in that we’ll cover how C++’s features are actually implemented. This implementation will look suspiciously similar.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Today More on memory bugs Java vs C, the battle..
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
A Quick Java Course part 1 Michael McDougall CIS 573 September 27 th, 1999.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
University of Washington Today Reconnecting to Java  Back to CSE143!  But now you know a lot more about what really happens when we execute programs.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSE 333 Systems Programming Hal Perkins Winter 2016 Bonus Lecture– Function Pointers and Objects in C.
Memory Management in Java Mr. Gerb Computer Science 4.
Java Programming Language Lecture27- An Introduction.
University of Washington The last week of 351 Monday lecture (inside)  Java vs. C  Evaluations or start parallelism Wednesday lecture (outside?)  Parallelism.
Spring 2016Java & C Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Java vs. C CSE 351 Winter
Java and C I CSE 351 Spring 2017 Instructor: Ruth Anderson
Computer Organization and Design Pointers, Arrays and Strings in C
Scope and Code Generation
CSE 3302 Programming Languages
Java Primer 1: Types, Classes and Operators
Templates.
Java and C I CSE 351 Autumn 2016 Instructor: Justin Hsia
Overloading and Constructors
Object Oriented Programming (OOP) LAB # 8
Java Programming Language
Code Shape II – Objects & Classes Hal Perkins Autumn 2011
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Java and C CSE 351 Summer 2018 Instructor: Justin Hsia
Java and C CSE 351 Winter 2018 Instructor: Mark Wyse
Java and C CSE 351 Autumn 2018 Instructor: Teaching Assistants:
Code Shape II – Objects & Classes Hal Perkins Summer 2004
Java Programming Language
Java Basics Data Types in Java.
Java and C I CSE 351 Spring 2017 Instructor: Ruth Anderson
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Memory Bugs, Java and C CSE 351 Winter 2019
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Code Shape II – Objects & Classes Hal Perkins Autumn 2009
Week 9 - Monday CS222.
Presentation transcript:

Section 11: Comparing Java and C Data representations in Java Pointers and references Method calls Virtual machines and runtime environment Java Implementation

Pointers to fields In C, we have “->” and “.” for field selection depending on whether we have a pointer to a struct or a struct (*r).a is so common it becomes r->a In Java, all variables are references to objects We always use r.a notation But really follow reference to r with offset to a, just like C’s r->a Java Implementation

Casting in C We can cast any pointer into any other pointer s n p 4 8 struct BlockInfo { int sizeAndTags; struct BlockInfo* next; struct BlockInfo* prev; }; typedef struct BlockInfo BlockInfo; … int x; BlockInfo *b; BlockInfo *newBlock; newBlock = (BlockInfo *) ( (char *) b + x ); Cast b into char pointer so that you can add byte offset without scaling Cast back into BlockInfo pointer so you can use it as BlockInfo struct s n p 4 8 12 s p n x Java Implementation

Casting in Java Can only cast compatible object references class Sister extends Parent{ int hers; }; class Object{ … }; class Parent { int address; }; class Brother extends Parent{ int his; }; // Parent is a super class of Brother and Sister, which are siblings Parent a = new Parent(); Sister xx = new Sister(); Brother xy = new Brother(); Parent p1 = new Sister(); // ok, everything needed for Parent // is also in Sister Parent p2 = p1; // ok, p1 is already a Parent Sister xx2 = new Brother(); // incompatible type – Brother and // Sisters are siblings Sister xx3 = new Parent(); // wrong direction; elements in Sister // not in Parent (hers) Brother xy2 = (Brother) a; // run-time error; Parent does not contain // all elements in Brother (his) Sister xx4 = (Sister) p2; // ok, p2 started out as Sister Sister xx5 = (Sister) xy; // inconvertible types, xy is Brother How is this implemented / enforced? Java Implementation

Creating objects in Java fields class Point { double x; double y; Point() { x = 0; y = 0; } boolean samePlace(Point p) { return (x == p.x) && (y == p.y); } } … Point newPoint = new Point(); constructor method creation Java Implementation

Creating objects in Java “new” Allocates space for data fields Adds pointer in object to “virtual table” or “vtable” for class vtable is shared across all objects in the class! Includes space for “static fields” and pointers to methods’ code Returns reference (pointer) to new object in memory Runs “constructor” method The new object is eventually garbage collected if all references to it are discarded x vtable constructor samePlace y Point object: Java Implementation

Initialization newPoint’s fields are initialized starting with the vtable pointer to the vtable for this class The next step is to call the ‘constructor’ for this object type Constructor code is found using the ‘vtable pointer’ and passed a pointer to the newly allocated memory area for newPoint so that the constructor can set its x and y to 0 Point.constructor( ) x = 0 vtable constructor samePlace y = 0 How does the constructor know where to find x and y? Java Implementation

Java Methods Methods in Java are just functions (as in C) but with an extra argument: a reference to the object whose method is being called E.g., newPoint.samePlace calls the samePlace method with a pointer to newPoint (called ‘this’) and a pointer to the argument, p – in this case, both of these are pointers to objects of type Point Method becomes Point.samePlace(Point this, Point p) return x==p.x && y==p.y; becomes something like: return (this->x==p->x) && (this->y==p->y); ====E-mail sent to class after this lecture:==== To follow up on a question asked during today's lecture, on slide 17: "Do static methods [which are specific to the entire class, not a particular object] still get the implicit this argument?" It looks like the answer is no: the JVM specification [1] says: "By convention, an instance method is passed a reference to its instance in local variable 0. In the Java programming language the instance is accessible via the this keyword. Class (static) methods do not have an instance, so for them this use of local variable 0 is unnecessary." If the static method accesses any static members of the class, then these accesses are resolved at compile time (when the Java code is translated into bytecode, which we'll talk more about on Friday); there is no need to pass a "reference" to the class itself. [1] http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.6 Java Implementation

Subclassing Where does “aNewField” go? class PtSubClass extends Point{    int aNewField;    boolean samePlace(Point p2) {        return false;    }    void sayHi() {       System.out.println("hello");    }  } Where does “aNewField” go? At end of fields of Point – allows easy casting from subclass to parent class! Where does pointer to code for two new methods go? To override “samePlace”, write over old pointer Add new pointer at end of table for new method “sayHi” Java Implementation

Subclassing vtable x y aNewField tacked on at end aNewField class PtSubClass extends Point{    int aNewField;    boolean samePlace(Point p2) {        return false;    }    void sayHi() {       System.out.println("hello");    }  } aNewField tacked on at end vtable x y aNewField constructor samePlace sayHi vtable for PtSubClass (not Point) Pointer to old code for constructor Pointer to new code for samePlace Java Implementation