JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:



Similar documents
JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

JAVA - INNER CLASSES

Moving from CS 61A Scheme to CS 61B Java

Introduction to Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Introduction to Object-Oriented Programming

J a v a Quiz (Unit 3, Test 0 Practice)

C++ INTERVIEW QUESTIONS

LAB4 Making Classes and Objects

Example of a Java program

Building Java Programs

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 1 Java Program Design and Development

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Chapter 5 Functions. Introducing Functions

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Object-Oriented Programming Lecture 2: Classes and Objects

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

13 Classes & Objects with Constructors/Destructors

Building Java Programs

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

In this Chapter you ll learn:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Install Java Development Kit (JDK) 1.8

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Chapter 2: Elements of Java

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS

Basic Object-Oriented Programming in Java

AP Computer Science Java Subset

Java Interview Questions and Answers

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Java Programming Fundamentals

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Computer Programming C++ Classes and Objects 15 th Lecture

Java CPD (I) Frans Coenen Department of Computer Science

Chapter 2 Introduction to Java programming

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Conversion Constructors

C++FA 5.1 PRACTICE MID-TERM EXAM

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Crash Course Part I

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Building Java Programs

Fundamentals of Java Programming

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Chapter 3. Input and output. 3.1 The System class

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Contents. 9-1 Copyright (c) N. Afshartous

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Object Oriented Software Design

Topic 11 Scanner object, conditional execution

AP Computer Science Java Mr. Clausen Program 9A, 9B

Comp 248 Introduction to Programming

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

public static void main(string[] args) { System.out.println("hello, world"); } }

C Compiler Targeting the Java Virtual Machine

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Teach Yourself Java in 21 Minutes

JAVA - OBJECT & CLASSES

Inheritance, overloading and overriding

Object Oriented Software Design II

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

JAVA ARRAY EXAMPLE PDF

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

JAVA - MULTITHREADING

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

Part I. Multiple Choice Questions (2 points each):

Java programming for C/C++ developers

Description of Class Mutation Mutation Operators for Java

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

CS170 Lab 11 Abstract Data Types & Objects

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

OpenCL Static C++ Kernel Language Extension

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

C++ Overloading, Constructors, Assignment operator

Transcription:

http://www.tutorialspoint.com/java/java_methods.htm JAVA - METHODS Copyright tutorialspoint.com A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console. Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design. Creating Method: Considering the following example to explain the syntax of a method: public static int methodname(int a, int b) { // body Here, public static : modifier. int: return type methodname: name of the method a, b: formal parameters int a, int b: list of parameters Method definition consists of a method header and a method body. The same is shown below: modifier returntype nameofmethod (Parameter List) { // method body The syntax shown above includes: modifier: It defines the access type of the method and it is optional to use. returntype: Method may return a value. nameofmethod: This is the method name. The method signature consists of the method name and the parameter list. Parameter List: The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters. method body: The method body defines what the method does with statements. Here is the source code of the above defined method called max. This method takes two parameters num1 and num2 and returns the maximum between the two: /** the snippet returns the minimum between two numbers */ public static int minfunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min;

Method Calling: For using a method, it should be called. There are two ways in which a method is called i.e. method returns a value or returning nothing noreturnvalue. The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when: return statement is executed. reaches the method ending closing brace. The methods returning void is considered as call to a statement. Lets consider an example: System.out.println("This is tutorialspoint.com!"); The method returning value can be understood by the following example: int result = sum(6, 9); Following is the example to demonstrate how to define a method and how to call it: public class ExampleMinNumber{ public static void main(string[] args) { int a = 11; int b = 6; int c = minfunction(a, b); System.out.println("Minimum Value = " + c); /** returns the minimum of two numbers */ public static int minfunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; inimum value = 6 The void Keyword: The void keyword allows us to create methods which do not return a value. Here, in the following example we're considering a void method methodrankpoints. This method is a void method which does not return any value. Call to a void method must be a statement i.e. methodrankpoints255.7;. It is a Java statement which ends with a semicolon as shown below. public class ExampleVoid { public static void main(string[] args) { methodrankpoints(255.7);

public static void methodrankpoints(double points) { if (points >= 202.5) { System.out.println("Rank:A1"); else if (points >= 122.4) { System.out.println("Rank:A2"); else { System.out.println("Rank:A3"); Rank:A1 Passing Parameters by Value: While working under calling process, arguments is to be passed. These should be in the same order as their respective parameters in the method specification. Parameters can be passed by value or by reference. Passing Parameters by Value means calling a method with a parameter. Through this the argument value is passed to the parameter. The following program shows an example of passing parameter by value. The values of the arguments remains the same even after the method invocation. public class swappingexample { public static void main(string[] args) { int a = 30; int b = 45; System.out.println("Before swapping, a = " + a + " and b = " + b); // Invoke the swap method swapfunction(a, b); System.out.println("\n**Now, Before and After swapping values will be same here**:"); System.out.println("After swapping, a = " + a + " and b is " + b); public static void swapfunction(int a, int b) { System.out.println("Before swapping(inside), a = " + a + " b = " + b); // Swap n1 with n2 int c = a; a = b; b = c; System.out.println("After swapping(inside), a = " + a + " b = " + b);

Before swapping, a = 30 and b = 45 Before swapping(inside), a = 30 b = 45 After swapping(inside), a = 45 b = 30 ** Now, Before and After swapping values will be same here**: After swapping, a = 30 and b is 45 Method Overloading: When a class has two or more methods by same name but different parameters, it is known as method overloading. It is different from overriding. In overriding a method has same method name, type, number of parameters etc. Lets consider the example shown before for finding minimum numbers of integer type. If, lets say we want to find minimum number of double type. Then the concept of Overloading will be introduced to create two or more methods with the same name but different parameters. The below example explains the same: public class ExampleOverloading{ public static void main(string[] args) { int a = 11; int b = 6; double c = 7.3; double d = 9.4; int result1 = minfunction(a, b); // same function name with different parameters double result2 = minfunction(c, d); System.out.println("Minimum Value = " + result1); System.out.println("Minimum Value = " + result2); // for integer public static int minfunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; // for double public static double minfunction(double n1, double n2) { double min; if (n1 > n2) min = n2; else min = n1; return min; inimum Value = 6 inimum Value = 7.3 Overloading methods makes program readable. Here, two methods are given same name but with different parameters. The minimum number from integer and double types is the result. Using Command-Line Arguments: Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main.

A command-line argument is the information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy.they are stored as strings in the String array passed to main. The following program displays all of the command-line arguments that it is called with: public class CommandLine { public static void main(string args[]){ for(int i=0; i<args.length; i++){ System.out.println("args[" + i + "]: " + args[i]); Try executing this program as shown here: $java CommandLine this is a command line 200-100 args[0]: this args[1]: is args[2]: a args[3]: command args[4]: line args[5]: 200 args[6]: -100 The Constructors: A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other startup procedures required to create a fully formed object. All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used. Here is a simple example that uses a constructor without parameters: // A simple constructor. class MyClass { int x; // Following is the constructor MyClass() { x = 10; You would call constructor to initialize objects as follows: public class ConsDemo { public static void main(string args[]) { MyClass t1 = new MyClass();

MyClass t2 = new MyClass(); System.out.println(t1.x + " " + t2.x); parametarized constructor Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name. Here is a simple example that uses a constructor with parameter: // A simple constructor. class MyClass { int x; // Following is the constructor MyClass(int i ) { x = i; You would call constructor to initialize objects as follows: public class ConsDemo { public static void main(string args[]) { MyClass t1 = new MyClass( 10 ); MyClass t2 = new MyClass( 20 ); System.out.println(t1.x + " " + t2.x); 10 20 The this keyword this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods. NoteThe keyword this is used only within instance methods or constructors

In general the keyword this is used to : Differentiate the instance variables from local variables if they have same names, within a constructor or a method. class Student{ int age; Student(int age){ this.age=age; Call one type of constructorparametrizedconstructorordefault from other in a class. It is known as explicit constructor invocation. class Student{ int age Student(){ this(20); Student(int age){ this.age=age; Example Here is an example that uses this keyword to access the members of a class. Copy and paste the below given program in a file with name This_Example.java public class This_Example { //Instance variable num int num=10; This_Example(){ System.out.println("This is an example program on keyword this "); This_Example(int num){ //Invoking the default constructor this(); //Assigning the local variable num to the instance variable num this.num=num; public void greet(){ System.out.println("Hi Welcome to Tutorialspoint"); public void print(){ //Local variable num int num=20; //Printing the instance variable System.out.println("value of local variable num is : "+num); //Printing the local variable System.out.println("value of instance variable num is : "+this.num);

//Invoking the greet method of a class this.greet(); public static void main(string[] args){ //Instantiating the class This_Example obj1=new This_Example(); //Invoking the print method obj1.print(); //Passing a new value to the num variable through parametrized constructor This_Example obj2=new This_Example(30); //Invoking the print method again obj2.print(); This is an example program on keyword this value of local variable num is : 20 value of instance variable num is : 10 Hi Welcome to Tutorialspoint This is an example program on keyword this value of local variable num is : 20 value of instance variable num is : 30 Hi Welcome to Tutorialspoint Variable Argumentsvar args: JDK 1.5 enables you to pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows: typename... parametername In the method declaration, you specify the type followed by an ellipsis... Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it. public class VarargsDemo { public static void main(string args[]) { // Call method with variable args printmax(34, 3, 3, 2, 56.5); printmax(new double[]{1, 2, 3); public static void printmax( double... numbers) { if (numbers.length == 0) { System.out.println("No argument passed"); return; double result = numbers[0]; for (int i = 1; i < numbers.length; i++) if (numbers[i] > result) result = numbers[i]; System.out.println("The max value is " + result);

The max value is 56.5 The max value is 3.0 The finalize Method: It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize, and it can be used to ensure that an object terminates cleanly. For example, you might use finalize to make sure that an open file owned by that object is closed. To add a finalizer to a class, you simply define the finalize method. The Java runtime calls that method whenever it is about to recycle an object of that class. Inside the finalize method, you will specify those actions that must be performed before an object is destroyed. The finalize method has this general form: protected void finalize( ) { // finalization code here Here, the keyword protected is a specifier that prevents access to finalize by code defined outside its class. This means that you cannot know when or even if finalize will be executed. For example, if your program ends before garbage collection occurs, finalize will not execute. Loading [MathJax]/jax/output/HTML-CSS/jax.js