Class 10 ICSE - Java

Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.

Methods

What are Methods ?




  • Methods are set of instructions which are used to perform certain actions, and they are also known as functions or modules. In other words a method is a block of code which only runs when it is called. You can also pass data, known as parameters, into a function.

  • Each method must be defined within the class.

  • There are basically two types of methods :
    • Built-in Methods : Built in methods are the part of compiler package , such as System.out.println() , System.exit(0), etc
    • User-defined Methods : User-defined methods are created by you the programmer. These methods take on names that you assign to them and perform tasks that you create .

Example:

Need/Advantages of Methods




  • Methods can be reused in a program without the need to rewrite the same code methods provide the means to avoid duplication of the code and helps in code reusability

  • They divide complex program into smaller code blocks

  • They help us in understanding the flow of program

  • Improves readability of the code

  • Makes debugging of the code easier.

Method Definition




Methods Are defined inside the class. The syntax of a method definition is shown below


Parts of Function




Function Prototype :

(i) Function prototype is nothing but declaration of a function.
(ii) Function prototype contains the function's return type , name and parameters passed.

- Writing a function prototype is "declaring" the function.
Example : float square (float x);

- In a function declaration or prototype , specifying an identifier (parameter name) is optional. Thus the following example in declaration of a function is legal
Example : float square (float);

Function Signature :

The method name along with the list of parameters used in method prototype is called as method or function signature. It is a part of a method prototype.
Let us understand the parts with an example shown below



In this example :

(i) The method prototype is : public void HelloMessage()
(ii) The method signature is : HelloMessage()
(iii) The access modifier public signifies that the method can be accsessed by other classes.
(iv) The return type void which signifies that method doesn't return a value.
(v) The parameter list is empty (i.e. no parameters) this means no value is passed to this method.

Method Parameters




Values can be passed to methods as parameters. Parameter act as a variable inside the method.
Parameters are given after method name inside the parenthesis. You can add multiple parameters to a method seperated with a comma(,).


The above example has a method that takes a String value name as parameter. When the method is called, we pass name, which is used inside the method to print the name

When a parameter is passed to the method, it is called an argument. So, from the example above: name is a parameter, while Jane, Jenny and Cristie are arguments.



The above example shows the use of multiple parameters. A method that takes a String value name and Integer value age as parameters. When the method is called, we pass name and age, which is used inside the method to print the name along with age

When you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

Invoking Methods




By defining a method in a class all you are doing is telling the compiler that the method exist with some functionality the method does not actually gets executed until it is invoked. The method can be invoked (or called or executed) by specifying its name followed by parameters being sent enclosed in the round brackets. For example, the findSquare () method defined earlier could be invoked using the following call statement :

findSquare(num); --------> (Method Calling) .

Actual and Formal Parameters




Actual Parameters :

Whenever we pass the parameter to the method at the time of method calling then those parameters are known as actual parameters

Formal Parameters :

Whenever we pass the parameter along with the data type to the method at the time of method definition then those parameters are known as formal parameters
Let us understand these with an example shown below

Returning from Methods




The Return Statement :

(i) It returns a value to the calling statement.
(ii) It causes an exit from the method abd transfers the control back to the calling method



Returning Values:

"void" keyword signifies that a method should not have a return value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the function.
Let us understand these with an example shown below :



The above example returns the sum of two parameters.



The above example shows that you can also store the result in a variable

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com