Material Java type system Reflection

Similar documents
Java Object Model. Or, way down the rabbit hole

CS1007: Object Oriented Design and Programming in Java. Lecture #18 Mar 28 Shlomo Hershkop Announcement

Programming Language Concepts: Lecture 10

Introduction to Programming Using Java (98-388)

CSC Java Programming, Fall Java Data Types and Control Constructs

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Reflection. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 28

Introduction to Reflection

type conversion polymorphism (intro only) Class class

Programming Language Concepts: Lecture 9

Java Primer 1: Types, Classes and Operators

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators

Concepts of Object-Oriented Programming Peter Müller

Java Reflection. adapted from original slides by Tim Lethbridge University of Ottawa. Examining Classes. What is Reflection?

CS 251 Intermediate Programming Methods and Classes

Introduction to Java

JAVA. Reflection API. Java, summer semester

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

CS 251 Intermediate Programming Methods and More

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CSE 431S Type Checking. Washington University Spring 2013

Getting started with Java

CS260 Intro to Java & Android 03.Java Language Basics

Selected Questions from by Nageshwara Rao

9 Working with the Java Class Library

CS 177 Week 15 Recitation Slides. Review

Lecture 9 : Basics of Reflection in Java

Conversions and Overloading : Overloading

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Inheritance (continued) Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Pace University. Fundamental Concepts of CS121 1

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Name of subject: JAVA PROGRAMMING Subject code: Semester: V ASSIGNMENT 1

Peer Instruction 1. Elementary Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Computer Programming, I. Laboratory Manual. Final Exam Solution

CS 231 Data Structures and Algorithms, Fall 2016

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS2141 Software Development using C/C++ C++ Basics

JAVA.LANG.CLASS CLASS

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Introflection. Dave Landers BEA Systems, Inc.

Computer Science II (20082) Week 1: Review and Inheritance

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Java Object Oriented Design. CSC207 Fall 2014

C a; C b; C e; int c;

Announcements. Java Graphics. Exceptions. Java Odds & Ends

Binghamton University. CS-140 Fall Data Types in Java

TeenCoder : Java Programming (ISBN )

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Chapter 2: Basic Elements of Java

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

ECE 122. Engineering Problem Solving with Java

CH. 2 OBJECT-ORIENTED PROGRAMMING

Advanced programming for Java platform. Introduction

CPS 506 Comparative Programming Languages. Programming Language

CS180 Recitation. More about Objects and Methods

Points To Remember for SCJP

1 Shyam sir JAVA Notes

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

CS 61B Data Structures and Programming Methodology. June David Sun

Pointers (continued), arrays and strings

Java Classes: Math, Integer A C S L E C T U R E 8

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

COE318 Lecture Notes Week 8 (Oct 24, 2011)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Binghamton University. CS-140 Fall Dynamic Types

CS112 Lecture: Primitive Types, Operators, Strings

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Pointers (continued), arrays and strings

index.pdf January 21,

INHERITANCE. Spring 2019

What is Inheritance?

Programming with Java

Java Bytecode (binary file)

For this section, we will implement a class with only non-static features, that represents a rectangle

Chapter 2: Using Data

CompuScholar, Inc. 9th - 12th grades

Operators and Expressions

Polymorphism. return a.doublevalue() + b.doublevalue();

Array. Prepared By - Rifat Shahriyar

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Transcription:

CS1706 Intro to Object Oriented Dev II -Fall 04 Announcements Week 15 Final Exam: Tues. Dec. 14 @ 3:25pm Material Java type system Reflection

Java Type System Type System is a set of values and the operations that can be applied to the values Strongly typed language: compiler and run-time system check that no operation can execute that violates type system rules Compile-time check: call a method that does not exist Employee e = new Employee(); e.clear(); // ERROR Run-time check e = null; e.setsalary(20000); // ERROR

Types in Java Primitive types: int short long byte char float double boolean Class types Employee e; Interface types ActionListener a; Array types String[] s; The null type Note: void is not a type in Java

Java Values Four types of values in Java 1. Value of primitive types 10, 10.5, h, true 2. Reference to object of a class type new Employee() 3. Reference to array new int[] { 1, 2, 3, 4 } 4. null value is a special type of value null

Primitive Types Primitive types are not classes and thus their values cannot be treated like objects. Example: Object o = 10; //illegal Java provides wrapper classes Integer for int, etc. Use the wrapper to treat primitives like objects Objects o = new Integer(10); //legal

Type inquiry Some languages provide facilities to check types of values at runtime. Consider Object o =... What is stored in o? What is the type of the value stored there? Depends on runtime behavior of program Java s runtime system keeps information of type, so it can be checked at runtime

Type safety when casting You cast values to convert type Shape e = (Shape) somevalue; What if somevalue is not of type (or subtype) Shape? Illegal cast exception will occur... To test if value can be casted, use instanceof if (somevalue instanceof Shape)

The Class class (what?) Every object in java has a type object associated with it at runtime. Object e = new Rectangle(); Class c = e.getclass(); System.out.println(c.getName()); // prints java.awt.rectangle Class object describes a type

Class object Class object reveals superclass interfaces package names and types of fields names, parameter types, return types of methods parameter types of constructors There is a single instance for a given class, never more than one

Getting a Class object Three ways to get a Class object getclass() method gets class of any object, returning an object of type Class. Rectangle r = new Rectangle(); Class c = r.getclass(); Class.forName() method yields Class object: Class c = Class.forName( java.awt.rectangle );.class suffix yields Class object too Class c = Rectangle.class;

Some methods Some methods in the Class class Class getsuperclass() Class[] getinterfaces() Package getpackage() Field[] getdeclaredfields() Constructor[] getdeclaredconstructors() Method[] getdeclaredmethods() Note that you can get access to fields, methods, and contructors You can also call constructor and methods

Enumerate fields Prints the names of all static fields of the Math class Field[] fields = Math.class.getDeclaredFields(); for (int i = 0; i < fields.length; i++) if (Modifier.isStatic(fields[i].getModifiers())) System.out.println(fields[i].getName());

Enumerate Constructors Prints names/parameter types of all Rectangle constructors for (int i = 0; i < cons.length; i++) { Class[] params = cons[i].getparametertypes(); System.out.print("Rectangle("); for (int j = 0; j < params.length; j++) { if (j > 0) System.out.print(", "); System.out.print(params[j].getName()); } System.out.println(")"); } Prints Rectangle() Rectangle(java.awt.Rectangle) Rectangle(int, int, int, int) Rectangle(int, int) Rectangle(java.awt.Point, java.awt.dimension) Rectangle(java.awt.Point) Rectangle(java.awt.Dimension)

Getting single method Supply method name, array of parameter types (formal arguments) Example: Rectangle.contains(int, int) Method m = Rectangle.class.getDeclaredMethod( "contains", new Class[] { int.class, int.class }); Example: Default constructor Constructor c = Rectangle.class.getDeclaredConstructor( new Class[] {});

Invoking a method Supply implicit parameter, array of explicit parameter values Must wrap primitive types and unwrap primitive return value Example: calling System.out.println Method m = PrintStream.class.getDeclaredMethod( "println", new Class[] { String.class } ); m.invoke(system.out, new Object[] { "Hello, World!" });

Inspecting objects Can obtain object contents at runtime, useful for generic debugging tools, need to gain access to private fields Class c = obj.getclass(); Field f = c.getdeclaredfield(name); f.setaccessible(true); //Throws exception if security manager disallows access //Access field value: Object value = f.get(obj); f.set(obj, value);