Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer

Similar documents
Java and C# in Depth

Exercise Session Week 8

Exercise Session Week 8

Introduction to Programming Using Java (98-388)

Exercise Session Week 7

PROGRAMMING FUNDAMENTALS

Zheng-Liang Lu Java Programming 45 / 79

C#: framework overview and in-the-small features

CSC 1214: Object-Oriented Programming

Introduction to Java

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Java Fundamentals (II)

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java Identifiers, Data Types & Variables

Operators and Expressions

JAVA OPERATORS GENERAL

Array. Array Declaration:

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Getting started with Java

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

Java Programming for Selenium

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Selected Questions from by Nageshwara Rao

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

PIC 20A Number, Autoboxing, and Unboxing

Java Bytecode (binary file)

CIS133J. Working with Numbers in Java

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

CS 231 Data Structures and Algorithms, Fall 2016

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Program Fundamentals

Expressions & Flow Control

Class, Variable, Constructor, Object, Method Questions

Introduction to C# Applications

Java: framework overview and in-the-small features

Selenium Class 9 - Java Operators

Java Primer 1: Types, Classes and Operators

Exercise Session Week 6

Introduce C# as Object Oriented programming language. Explain, tokens,

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

3. Java - Language Constructs I

Prof. Navrati Saxena TA: Rochak Sachan

COMP 202 Java in one week

CSC Java Programming, Fall Java Data Types and Control Constructs

Index COPYRIGHTED MATERIAL

Learn C# Errata. 3-9 The Nullable Types The Assignment Operators

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

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

Elementary Programming

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Another IS-A Relationship

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lesson 36: for() Loops (W11D1)

Java: introduction to object-oriented features

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Loops. CSE 114, Computer Science 1 Stony Brook University

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

BM214E Object Oriented Programming Lecture 4

Variable initialization and assignment

COMP 110 Programming Exercise: Simulation of the Game of Craps

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Array. Prepared By - Rifat Shahriyar

Java Programming Language. 0 A history

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

2 rd class Department of Programming. OOP with Java Programming

Programming Basics. Digital Urban Visualization. People as Flows. ia

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Interfaces. An interface forms a contract between the object and the outside world.

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

CS 251 Intermediate Programming Methods and Classes

CS11 Java. Fall Lecture 1

Full file at

Chapter 1 Getting Started

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Chapter 2. Elementary Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Handout 4 Conditionals. Boolean Expressions.

Lec 7. for loops and methods

STRUCTURING OF PROGRAM

1 Shyam sir JAVA Notes

CMPS 11 Intermediate Programming Midterm 2 Review Problems

Transcription:

Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer Exercise Session Week 2

Agenda Ø Quizzes Ø More quizzes Ø And even more quizzes 2

Quiz 1. What will be printed? Ø Integers public class Program{ public static void main(string[] args) { Integer i1 = 123; Integer i2 = 123; Integer i3 = 128; Integer i4 = 128; System.out.println(i1 == i2); System.out.println(i3 == i4); true false public class Program { static void Main(string[] args) { Object i1 = 123; Object i2 = 123; Object i3 = 128; Object i4 = 128; Console.WriteLine(i1 == i2); Console.WriteLine(i3 == i4); false false (Java) Integer and int are different types o Autoboxing and unboxing o JVM creates a new object, someime reuses an old one. (C#) no caching of boxed objects 3

Quiz 2. What will be printed? Ø FloaIng point numbers public class Program{ public static void main(string[] args) { double d1=4.0; double d2=3.1; System.out.println(d1 d2); 0.8999999999999999 public class Program { static void Main(string[] args) { double d1 = 4.0; double d2 = 3.1; Console.WriteLine(d1 - d2); 0.9 Why the imprecision? o Nature of floaing point numbers (IEEE 754) Why the precision? o Don t get fooled! C# rounds float/double values to certain significant digits for base- 10 string representaion (what you see is not what it is) o Using BigDecimal in Java and decimal in C# solves these problems. Both classes work on base- 10 representaions, and are therefore slow. 4

Standard Numeric Format Strings (C#) float sum = 0; for (int i = 1; i < 11; i++){ sum += 0.1f; Console.Write(String.Format("{0,- 10", sum.tostring())); Console.Write(", "); Console.WriteLine(sum.ToString("g9")); 0.1, 0.100000001 0.2, 0.200000003 0.3, 0.300000012 0.4, 0.400000006 0.5, 0.5 0.6, 0.600000024 0.7, 0.700000048 0.8000001, 0.800000072 0.9000001, 0.900000095 1, 1.00000012 5

Quiz 3: What does it do? Ø Does the following Java code test oddity correctly? public static boolean isodd(int i) { return i % 2 == 1; No Ø What do you get from the following C# code? public int Divide(int operand1, int operand2){ return operand1 / operand2; public int Test(){ return Divide(4, 0); DivideByZeroException Ø What if we change int s to double s? Compilation error (Java) Double.PositiveInfinity or Double.NegativeInfinity (C#) 6

Quiz 4. What will be printed? Ø Strings public class ImmutableStrings{ public static void main(string[] args){ String a = "string"; String b = "string"; String c = "str"; c += "ing"; System.out.println(a.equals(b)); System.out.println(a == b); System.out.println(a == c); class ImmutableStrings { static void Main(string[] args) { string a = "string"; string b = "string"; string c = "str"; c += "ing"; Console.WriteLine(a.Equals(b)); Console.WriteLine(a == b); Console.WriteLine(a == c); true true false Strings are immutable objects in both languages Operator overloading in C# o Reference equality true true true Console.WriteLine(Object.ReferenceEquals(s1, s2)); 7

Performance of concatenation versus StringBuilder (C#, in seconds) # of appends + StringBuilder 10 0.000 0.00 100 0.000 0.00 1,000 0.000 0.00 2,500 0.000 0.00 5,000 0.020 0.00 7,500 0.050 0.00 10,000 0.090 0.00 15,000 0.250 0.00 25,000 1.052 0.00 35,000 2.373 0.00 50,000 5.699 0.00 65,000 10.625 0.00 75,000 14.831 0.01 85,000 19.418 0.01 100,000 27.159 0.01 150,000 65.374 0.01 250,000 209.221 0.02 350,000 441.615 0.02 500,000 910.129 0.04 650,000 1521.708 0.06 750,000 1999.305 0.06 850,000 2576.575 0.06 1,000,000 3562.933 0.07

Quiz 5. Can we do this? Ø Switch int i1; i1 = 2; // can we do this? switch (i1) { case 1: print(1); case 2: print(2); case 3: print(3); double d1;... // this? switch (d1) { case 1.0:... case 2.0:... case 3.0:... String s1;... // or this? switch (s1) { case "1":... case "2":... case "3":... Rules about fall- through Data types o (Java) char, byte, short, int, their corresponding reference types, String, or enum o (C#) Numeric, String, or enumeraion data types Refer to the language specificaion for more informaion 9

Quiz 6. What do these programs do? (Java) Ø Type public class Type1 { public static void main(string[] args) { String s = null; System.out.println(s instanceof String); public class Type2 { public static void main(string[] args) { System.out.println(new Type2() instanceof String); Print false Compilation error public class Type3 { public static void main(string args[]) { Type3 t3 = (Type3) new Object(); Runtime exception 10

Quiz 7. What will be printed? (Java) 01 public class Test{ 02 int x = 5; 03 04 public static void main(string[] args) { 05 final Test f1 = new Test(); 07 Test f3 = testswitch (f1); 08 System.out.println((f1 == f3) + " " + (f1.x == f3.x) + " " + f1.x + " " 09 + " " + f3.x); 10 11 12 static Test testswitch(final Test x) { 13 final Test z = x; 14 z.x = 6; 15 return z; 16 17 true true 6 6 References f1, z, and f3 refer to the same instance. o final assures that a reference variable cannot be referred to a different object. o But final doesn t keep the object s state from changing. 11

Quiz 8. Type var and dynamic in C# Ø Example Ø True or False? var mydata = 365; The var keyword can be used to define local variables in a method or property scope, return values, parameters, or field data of a custom type; False Implicitly typed variables could be declared and iniialized at different locaions; False Given the above declaraion, assignment mydata = false; will change the type of mydata from int to bool. False Ø What if we change var to dynamic? Ø When to use? var: Mostly, if not only, for results from LINQ queries dynamic: scriping, interfacing with other languages, etc. 12

Quiz 9. Nullable data types in C# Ø A nullable type can represent all the values of its underlying type, plus the value null Ø True or False? int? nullableint = 10; nullableint = null; Nullable types could be based on reference types too, e.g. String? nullablestr = A ; The? suffix notaion is a shorthand for creaing an instance of the generic System.Nullable<T> structure type. True if (nullableint.hasvalue) Console.WriteLine("Value of 'nullableint' is: {0", nullableint.value); aint = nullableint.hasvalue? nullableint.value : 100; // Could be written as: aint = nullableint?? 100; False 13

Quiz 10. What does this program do? (C#) Ø enum using System; IndexOutOfRangeException class Program{ private static int[] resource = new int[] {0, 1, 2; public enum Size{Small, Medium, Large public static void Method1(Size thesize){ Console.WriteLine(theSize); Console.WriteLine("Resource: {0", resource[(int)thesize]); static void Main(string[] args){ Method1(Size.Small); Method1(Size.Large); Method1((Size) 1); Method1((Size) 3); Lacks type- safety Problem with serializaion if(system.enum.isdefined( typeof(size), thesize)){ else{ throw new Exception( ); 14

QuesUons? 15