03 Handout 1 PDF
Document Details
Uploaded by ImpressiveErhu1738
STI
Tags
Related
- Method Overloading - IT1908 PDF
- Java Programming: Chapter 3 Using Methods, Classes, and Objects PDF
- Week 5: Methods and Functions in Java CSC 211 PDF
- Lecture 2 (Chapter 2) Introduction to Java Programming PDF
- CSC435: Object-Oriented Programming - Topic 3 - Basic Class Concept PDF
- برمجة حاسبات (1) - الباب الرابع - التحميل الزائد للدوال PDF
Summary
This document explains method overloading and autoboxing principles in Java programming. It provides detailed examples of overloading methods with different parameter types and demonstrates autoboxing between primitive and object types. The document also outlines the concept of pass-by-value and the behavior of variables within methods.
Full Transcript
IT1908 METHOD OVERLOADING Overloaded methods are differentiated by the number and the...
IT1908 METHOD OVERLOADING Overloaded methods are differentiated by the number and the type of arguments passed into the method. Passing Information to a Method You cannot declare more than one (1) method with the same Parameters are the variables listed in a method declaration. name and the same number and type of arguments. Ex. public double computeSalePrice(double You cannot declare two (2) methods with the same signature origPrice, double discountRate) { } even if they have different return types. Arguments are the actual values that are passed in when the Ex. method is invoked. public void draw(int i) { } Ex. computeSalePrice(37.50, 0.15) public int draw(int i) { } //does not compile Java is a pass-by-value language. This means that a copy of the variable is made, and the method receives that copy. Autoboxing Ex. main() method calls newNumber() method Autoboxing is the automatic conversion that the Java public static void main(String[] args) { compiler makes between the primitive types and their int num = 1; corresponding object wrapper classes. newNumber(2); Ex. int to Integer, double to Double System.out.println(num); When the primitive type version is not present, Java performs } autoboxing. public static void newNumber(int num) { public void draw(Integer i) { num = 3; System.out.print(“Integer”); } } Output: 1 Calling statement: draw(10); Explanation: The variable num in main() does not change Output: Integer even after calling it because no assignments are made to it. When the primitive type version is present, Java does not Overloading Methods need to perform autoboxing. Ex. A method signature consists of the method’s name and the public void draw(Integer i) { parameter types. System.out.print(“Integer”); Ex. The method signature of computeSalePrice(double } origPrice, double discountRate) is public void draw(int i) { computeSalePrice(double, double) System.out.print(“int”); Method overloading occurs when there are different method } signatures with the same name but different type parameters. Calling statement: draw(10); Ex. Output: int public void draw(String s) { } public void draw(int i) { } References: public void draw(int i, double f) { } Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning Java programming: The Thus, you can create statements like the following: object-oriented approach. Indiana: John Wiley & Sons, Inc. draw(“Circle”); Oracle Docs (n.d.). Citing sources. Retrieved from https://docs.oracle.com/javase/tutorial/java/javaOO/index.html draw(10); draw(10, 5.0); 03 Handout 1 *Property of STI [email protected] Page 1 of 1