CSI 3125, Preliminaries, page 1 Generic Class & Generic methods.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

CPSC 388 – Compiler Design and Construction
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
 2006 Pearson Education, Inc. All rights reserved Generics.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
 2005 Pearson Education, Inc. All rights reserved Generics.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Methods and Parameters Chapter 5 How to split large programs into isolated sections. Focus on one part of the problem – which can be “called” on or “invoked”
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Types in programming languages1 What are types, and why do we need them?
CIS 270—Application Development II Chapter 18-Generics.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
CSI 3125, Preliminaries, page 1 Compiling the Program.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Building java programs, chapter 3 Parameters, Methods and Objects.
1 ICS103 Programming in C Lecture 8: Functions I.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
Object Oriented Programming Lecture 2: BallWorld.
Suppose we want to print out the word MISSISSIPPI in big letters.
Java Primer 1: Types, Classes and Operators
Chapter 20 Generic Classes and Methods
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Advanced Programming in Java
Data Types.
Methods and Parameters
Method Mark and Lyubo.
Interface.
Name: Rubaisha Rajpoot
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Method Overloading in JAVA
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Scope of variables class scopeofvars {
Peer Instruction 4 Control Loops.
Names of variables, functions, classes
Generic Programming.
Chapter 2: Java Fundamentals cont’d
Subtype Substitution Principle
Presentation transcript:

CSI 3125, Preliminaries, page 1 Generic Class & Generic methods

CSI 3125, Preliminaries, page 2 Generic Class & Generic methods Advantage of Java Generics There are mainly 3 advantages of generics. 1) Type-safety : We can hold only a single type of objects in generics. 2) Type casting is not required: There is no need to typecast the object. 3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime. The good programming strategy says it is far better to handle the problem at compile time than runtime.

CSI 3125, Preliminaries, page 3 Generic methods write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods: All generic method declarations have a type parameter section delimited by angle brackets ( ) that precedes the method's return type. Each type parameter section contains one or more type parameters separated by commas. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments. A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types (like int, double and char).

CSI 3125, Preliminaries, page 4 Generic methods class G { void fun(T x) { System.out.println("value that passed to the function "+x); } public static void main(String a[]) { G obj=new G(); obj.fun("ppp"); // call function with string argument obj.fun(1); // cal function with int argument obj.fun(2.5); // call fun with float argument } }

CSI 3125, Preliminaries, page 5 Generic methods class G { void swap(T x,T y) {T t=x; x=y; y=t; System.out.println(x +" "+y);} public static void main(String a[]) { G obj=new G(); obj.swap("ppp","aa"); obj.swap(1,5); obj.swap(2.5,1.5); } }

CSI 3125, Preliminaries, page 6 Generic Class class test { T a,b; test(T x,T y) {a=x;b=y;} void show() { System.out.println(a +" "+b); } public static void main(String a[]) { test obj=new test ("ajith","popo"); test obj1=new test (10,20); obj.show(); obj1.show();} }