Dasar-Dasar Pemrograman 2: Generics

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Arrays in JAVA CS 340 Software Design Muhammad Talha.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Java Generics.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
BUILDING JAVA PROGRAMS CHAPTER 1 ERRORS. 22 OBJECTIVES Recognize different errors that Java uses and how to fix them.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
C# Basic Syntax, Visual Studio, Console Input / Output
C# Basic Syntax, Visual Studio, Console Input / Output
Lecture 20: Wrapper Classes and More Loops
Arrays in JAVA Visit for more Learning Resources.
CIS265/506 Cleveland State University – Prof. Victor Matos
Generic(Parameterized ) Types
Generics and Subtyping
COP 3503 FALL 2012 Shayan Javed Lecture 8
Lecture Note Set 1 Thursday 12-May-05
CS Week 10 Jim Williams, PhD.
Topic 6 Generic Data Structures
Generics.
Introduction to Programming in Java
Object Oriented Programming
Yong Choi School of Business CSU, Bakersfield
Chapter 19 Generics Dr. Clincy - Lecture.
Polymorphism.
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
CMSC 202 Lesson 22 Templates I.
Generics.
Building Java Programs
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
CISC124 Assignment 4 on Inheritance due today at 7pm.
Java Intro.
Section 9: Ruby and Java
Unit 3 Test: Friday.
CS 180 Assignment 6 Arrays.
Variables, Types, Operations on Numbers
Suppose I want to add all the even integers from 1 to 100 (inclusive)
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Generics, Lambdas, Reflections
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Templates I CMSC 202.
Chapter 11 Inheritance and Polymorphism Part 2
Java’s Central Casting
Review: libraries and packages
Generic Programming.
Java: Variables, Input and Arrays
Chapter 21 Generics.
Chapter 19 Generics.
Subtype Substitution Principle
Foundations of Programming 2: Abstract Classes and Interfaces
Dasar-Dasar Pemrograman 2: Recursion
Dasar-Dasar Pemrograman 2: Java Basics
Dasar-Dasar Pemrograman 2: Java Conditions and Loops
Chapter 19 Generics.
Presentation transcript:

Dasar-Dasar Pemrograman 2: Generics Tim DDP 2 Fasilkom UI Correspondence: Fariz Darari (fariz@cs.ui.ac.id) Photo by chuttersnap on Unsplash Feel free to use, reuse, and share this work: the more we share, the more we have!

Why? Try this one: Create a method to print each element of ArrayList of Integers vertically.

Why? Try this one: Create a method to print each element of ArrayList of Strings vertically.

Why? Try this one: Create a method to print each element of ArrayList of Doubles vertically.

All the methods are similar, the only difference is the element type! public static void printInt(ArrayList<Integer> lst) { for(int i = 0; i < lst.size(); i++) { System.out.println(lst.get(i)); } public static void printStr(ArrayList<String> lst) { public static void printDouble(ArrayList<Double> lst) {

Generics parameterize types! public static <T> void print(ArrayList<T> lst) { for(int i = 0; i < lst.size(); i++) { System.out.println(lst.get(i)); }

Generics parameterize types! public static <T> void print(ArrayList<T> lst) { for(int i = 0; i < lst.size(); i++) { System.out.println(lst.get(i)); } Generics are variables, but for types. They can be used for methods and classes!

Quiz time: Create a generic method to print any object with "Hello, " + object hello("Hello"); hello(new Integer(5)); String[] strArr = {"1", "2", "3"}; hello(new ArrayList<String>(Arrays.asList(strArr))); Output: Hello, Hello Hello, 5 Hello, [1, 2, 3]

Quiz time: Create a generic method to print any object with "Hello, " + object public static <T> void hello(T obj) { System.out.println("Hello, " + obj); }

Quiz time: Create a generic method to print any object with "Hello, " + object public static <Bla> void hello(Bla obj) { System.out.println("Hello, " + obj); } The naming of the generics does not have to be T.

Quiz time: Create a generic method to print any object with "Hello, " + object public static <Bla> void hello(Bla obj) { System.out.println("Hello, " + obj + " of type " + obj.getClass().getName()); } Print the object's type

Generics for classes Without generics for ArrayList ArrayList ageList = new ArrayList(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Object age : ageList) { sum = sum + ((Integer) age); } This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection

Generics for classes Without generics for ArrayList ArrayList ageList = new ArrayList(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Object age : ageList) { sum = sum + ((Integer) age); } RUNTIME ERROR: Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection

Generics for classes With generics for ArrayList ArrayList<Integer> ageList = new ArrayList<Integer>(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Object age : ageList) { sum = sum + ((Integer) age); } Notice what's changed This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection

Generics for classes With generics for ArrayList ArrayList<Integer> ageList = new ArrayList<Integer>(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Object age : ageList) { sum = sum + ((Integer) age); } COMPILE TIME ERROR: java: incompatible types: java.lang.String cannot be converted to java.lang.Integer This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection

Generics for classes With generics for ArrayList ArrayList<Integer> ageList = new ArrayList<Integer>(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Object age : ageList) { sum = sum + ((Integer) age); } COMPILE TIME ERROR: java: incompatible types: java.lang.String cannot be converted to java.lang.Integer Compile time error is better than runtime error, because errors can be detected earlier! This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. https://docs.oracle.com/javase/tutorial/java/generics/why.html Casting is not needed when using generics!

Generics for classes With generics for ArrayList ArrayList<Integer> ageList = new ArrayList<Integer>(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Object age : ageList) { sum = sum + ((Integer) age); } COMPILE TIME ERROR: java: incompatible types: java.lang.String cannot be converted to java.lang.Integer Compile time error is better than runtime error, because errors can be detected earlier! This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. https://docs.oracle.com/javase/tutorial/java/generics/why.html Casting is not needed when using generics! We know what is the type, so we can directly state it here!

Generics for classes With generics for ArrayList ArrayList<Integer> ageList = new ArrayList<Integer>(); ageList.add(new Integer(46)); ageList.add("50"); Integer sum = new Integer(0); for (Integer age : ageList) { sum = sum + age; } COMPILE TIME ERROR: java: incompatible types: java.lang.String cannot be converted to java.lang.Integer Compile time error is better than runtime error, because errors can be detected earlier! This part is available under the Creative Commons Attribution-ShareAlike License, from: https://en.wikibooks.org/wiki/Java_Programming/Collection Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. https://docs.oracle.com/javase/tutorial/java/generics/why.html Fixed, looks better now.

Picture: https://unsplash.com/photos/VyC0YSFRDTU