Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Similar documents
The class Object. Lecture CS1122 Summer 2008

String is a Class; Quoted Text is an Object

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Creating an Immutable Class. Based on slides by Prof. Burton Ma

equals() in the class Object

Methods Common to all Classes

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Mathematics for Computer Graphics - Lecture 12

Lecture 4. Strings, Wrappers, & Containers

Introduction to Programming Using Java (98-388)

CMSC 132: Object-Oriented Programming II. Effective Java. Department of Computer Science University of Maryland, College Park

Building Strings and Exploring String Class:

Lecture Notes K.Yellaswamy Assistant Professor K L University

"Hello" " This " + "is String " + "concatenation"

Super-Classes and sub-classes

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

java.lang.object: Equality

JAVA NUMBERS, CHARS AND STRINGS

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Java Strings Java, winter semester

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Lab 14 & 15: String Handling

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

Lecture 8. Reviewing Methods; Wrapper Classes

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

TEXT-BASED APPLICATIONS

Object-Oriented Programming in the Java language

Class Library java.lang Package. Bok, Jong Soon

JAVA.LANG.INTEGER CLASS

CSC Java Programming, Fall Java Data Types and Control Constructs

Announcements. Equality. Lecture 10 Equality and Hashcode. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

Domain-Driven Design Activity

Chapter 11: Collections and Maps

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Creating Strings. String Length

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

חומר עזר לבחינה במבוא למדעי המחשב // Indicates whether some other object is "equal to" // this one. boolean equals(object other)

Core Java Syllabus. Overview

Index COPYRIGHTED MATERIAL

Uguaglianza e Identità. (no, non avete sbagliato corso )

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Java Fundamentals (II)

CSE 331 Software Design & Implementation

Lesson 3: Accepting User Input and Using Different Methods for Output

Expected properties of equality

Programming Languages and Techniques (CIS120)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

חומר עזר לבחינה מבוא למדעי המחשב

Principles of So3ware Construc9on: Objects, Design, and Concurrency. Introduc9on to Java. Josh Bloch Charlie Garrod Darya Melicher

CS 251 Intermediate Programming Java I/O Streams

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Object Oriented Software Design

Object Oriented Software Design

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

HST 952. Computing for Biomedical Scientists Lecture 8

CSE 331 Software Design & Implementation

SSE3052: Embedded Systems Practice

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Primer 1: Types, Classes and Operators

Programming Languages and Techniques (CIS120)

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

PIC 20A Number, Autoboxing, and Unboxing

OBJECT ORIENTED PROGRAMMING. Course 3 Loredana STANCIU Room B616

Programming Languages and Techniques (CIS120)

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Lesson 4 Utility classes: Math, String, I/O

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

APS101- Midterm Sample Questions

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

TEST (MODULE:- 1 and 2)

boolean add(object o) Method Description (from docs API ) Method Description (from docs API )

CS162: Introduction to Computer Science II

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Chapter 10. Object-Oriented Thinking

CS260 Intro to Java & Android 03.Java Language Basics

IS502052: Enterprise Systems Development Concepts Lab 1: Java Review

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Equality in.net. Gregory Adam 07/12/2008. This article describes how equality works in.net

Simple Java Input/Output

Introduction to Programming (Java) 4/12

Programming Languages and Techniques (CIS120e)

Chapter 2: Using Data

From C++ to Java. Duke CPS

Quiz 1. In class on Tuesday (April 14) Review learning goals (from lecture) Review lectures 1 4, PSA 1 You should be able to:

Intro to Computer Science II

Chapter 10: Text Processing and More about Wrapper Classes

Chapter 3: Using Classes and Objects

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Not overriding equals

Introduction to Object-Oriented Programming

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

Transcription:

Overloaded Methods Sending Messages Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Initialization & Cleanup 2 Overloaded Constructors Sending Parameters accessor method 3 4

Sending Parameters Some words about this attribute value!= parameter value 5 6 Some words about this Some methods from Class Object Method Description (from docs API ) Object clone() creates and returns a copy of this object boolean equals(object o) void finalize() int hashcode() indicates whether some other object is "equal to" this one called by the garbage collector on an object when garbage collection determines that there are no more references to the object returns a hash code value for the object String tostring() returns a string representation of the object 7 8

public boolean equals(object o) public boolean equals(object o) 9 10 public String tostring() protected void finalize() V 11 12

Class String Class String? String s = new String( My first string ); String s = My first string ; = String a = First string ; String b = a.concat( Second string ); String a = First string ; String b = a + Second string ; 13 14 Some constructors from Class String Some methods from Class String Constructor Description (from docs API ) Method Description (from docs API ) boolean equals(object o) compares this string to the specified object String() initializes a newly created String object so that it represents an empty character sequence int length() returns the length of this string String(String original) Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string char charat(int index) int indexof(int ch) returns the char value at the specified index returns the index within this string of the first occurrence of the specified character int indexof(string str) returns the index within this string of the first occurrence of the specified substring 15 16

Some (more) methods from Class String Some static methods from Class String Method Description (from docs API ) Method Description (from docs API ) String concat(string str) boolean endswith(string s) boolean startswith(string s) concatenates the specified string to the end of this string tests if this string ends with the specified suffix tests if this string starts with the specified prefix String valueof(boolean b) returns the string representation of the boolean argument String valueof(char c) returns the string representation of the char argument String valueof(int d) returns the string representation of the int argument String valueof(float f) returns the string representation of the float argument String substring(int beginindex) returns a new string that is a substring of this string String valueof(double l) returns the string representation of the double argument boolean isempty() returns true if, and only if, length() is 0 String valueof(object i) returns the string representation of the Object argument 17 18 Arrays Arrays int[] intarray = new int[10]; int intarray[] = new int[10]; String s[] = new String[5]; 19 20

Arrays Arrays 21 22 Arrays Arrays V 23 24

Arrays Arrays 25 26 Wrapper Classes Type Integer Primitive type Wrapper class Constructor byte Byte byte, String short Short short, String int Integer int, String Constructor/Method Description (from docs API ) Integer(int value) Integer(String s) creates an object that represents the specified int value creates an object that represents the int value indicated by the String parameter long Long long, String float Float float, double, String double Double double, String String tostring() int intvalue() returns a String object representing this Integer's value returns the value of this Integer as an int char Character char boolean equals(object o) compares this object to the specified object boolean Boolean boolean, String int compareto(integer i) compares two Integer objects numerically 27 28

Type Integer Static Method Description (from docs API ) Integer valueof(string value) returns an Integer object holding the value of the specified String Integer valueof(int value) returns an Integer instance representing the specified int value int parseint(string s) parses the string argument as a signed decimal integer 29 30 Autoboxing Unboxing 31 32

Java I/O read input characters Java I/O read input bytes from keyboard System.in.read(); from file FileInputStream filestream; filestream = new FileInputStream( myfile.txt ); filestream.read(); import java.io.*; insert try-catch blocks! from keyboard InputStreamReader r; r = new InputStreamReader(System.in); from file InputStreamReader r; r = new InputStreamReader(new FileInputStream( myfile.txt )); r.read(); 33 34 read input strings Java I/O Java I/O from keyboard BufferedReader r; r = new BufferedReader(new InputStreamReader(System.in)); write bytes from file BufferedReader r; r = new BufferedReader(new InputStreamReader(new FileInputStream( myfile.txt ))); FileOutputStream filestream; filestream = new FileOutputStream( myfile.txt ); filestream.write(var); //var is previously defined r.read(); //next character r.readline(); //next line 35 36

Java I/O Java I/O write characters write strings FileOutputStream filestream = new FileOutputStream( myfile.txt ); PrintStream filestream = new PrintStream(new FileOutputStream( myfile.txt )); OutputStreamWriter outcstream = new OutputStreamWriter(fileStream); filestream.print(var); //var is previously defined outcstream.write(var); //var is previously defined filestream.println(var); //var is previously defined 37 38 import java.io.*; 39 40

import java.io.*; Effective Java Companion slides of Joshua Bloch, Effective Java (Second Edition) 41 5 Avoid creating unnecessary objects 5 Avoid creating unnecessary objects V String s = new String( mystring ); String s = mystring ; X Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) V Boolean b = Boolean.valueOf( true ); Boolean b = new Boolean( true ); sum += i; System.out.println(sum); 43 44

5 Avoid creating unnecessary objects 45 46 6 Eliminate obsolete references 47 48

8 Obey the general contract when overriding equals 13 Minimize the accessibility of classes and members An equivalence relation that is reflexive x.equals(x) == true symmetric x.equals(y) == true if and only if y.equals(x) == true transitive x.equals(y) == true, y.equals(z) == true x.equals(z) == true consistent x.equals(y) returns the same value // Potential security hole public static final Thing[] VALUES = {...}; unless x or/and y is/are modified x.equals(null) == false,where x, y, z are non-null reference values 49 50 14 In public classes, use accessor methods, not public fields 15 Minimize mutability Much better: do not expose data but services!? An immutable class = a class whose instances cannot be modified String, boxed primitives,... 51 52

15 Minimize mutability 15 Minimize mutability Don t provide any methods that modify the object s state. Ensure that the class can t be extended. Make all fields final. Make all fields private. Ensure exclusive access to any mutable components. 53 54