0% found this document useful (0 votes)
36 views7 pages

Java Functions: Predefined & User Defined

The document provides an overview of functions and methods in Java, including predefined and user-defined functions. It explains the syntax for defining methods, calling user-defined methods, and demonstrates examples of methods with return types. Additionally, it covers the use of parameters and access specifiers in method definitions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

Java Functions: Predefined & User Defined

The document provides an overview of functions and methods in Java, including predefined and user-defined functions. It explains the syntax for defining methods, calling user-defined methods, and demonstrates examples of methods with return types. Additionally, it covers the use of parameters and access specifiers in method definitions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Functions/Methods

------------------------------------
println()

nextInt()
length()
CharAt()

length()

"Besant technologies"

---->Methods is a block of code that perform some specific task.

Employee management.

Wipro,TCS,...

login()
logout()
-------------------------------------
types:
----------
1)predefined function
2)user defined function

1)predefined function
----------------------------------------
---->These Functions are defined by java library.

2)user defined function


-----------------------------------------
These functions are created by user requirement

main method
------------------------
public static void main(String[] args)
{

syntax:
----------------
public static datatype method_name(list of arg)
{
//statements
}

public---->access specifier
static --->keyword
datatype---->int,float,void,char

void---->does not return any value


int,float,char,..--->it returns any value

method_name ---->valid name


list_of arg --->parameter.

public class Myclass3


{
public static void main(String[] args)
{
[Link]("Welcome to Main method")
}

public static void mymethod1()


{
[Link]("This is User Defined Method");
}

Output
-------------
Welcome to Main method

How to calling a userdefined method?


---------------------------------------------------------------
methodname();

EX:
-----
public class Myclass3
{
public static void main(String[] args)
{
[Link]("Welcome to Main method")
}

public static void mymethod1()


{
[Link]("This is User Defined Method");
}

}
-----------------------------------------------------------------------------
public class Main
{
public static void main(String[] args)
{
mymethod2();
[Link]("Hello World");
mymethod1();//calling a method
}

public static void mymethod1()


{
[Link]("This is userdefined method");
}

public static void mymethod2()


{
[Link]("This is method2");

}
}
-------------------------------------------------------------
public class Main
{
public static void main(String[] args)
{
add();
}

public static void add()


{
int x=100,y=200;//functional variable

[Link](x+y);

}
}
-------------------------------------------------------------------
package Mypack1;

import [Link];

public class Myclass3


{
public static void main(String[] args)
{

[Link]("This is main method");


add(45,89);
add(5,3);
add(9,6);

public static void add(int x,int y)//x=45,y=89


{

[Link](x+y);
}

-------------------------------------------------------------------------
package Mypack1;

import [Link];

public class Myclass3


{
public static void main(String[] args)
{
Eligible_to_vote(55);
Eligible_to_vote(12);

public static void Eligible_to_vote(int age)


{
if(age>=18)
{
[Link]("Eligible for voting...");
}

else
{
[Link]("Not Eligible for voting");
}
}

---------------------------------------------------------------------
package Mypack1;

import [Link];

public class Myclass3


{
public static void main(String[] args)
{

int a;

Scanner sc=new Scanner([Link]);

[Link]("Enter the value");


a=[Link]();

Eligible_to_vote(a);
}

public static void Eligible_to_vote(int age)


{
if(age>=18)
{
[Link]("Eligible for voting...");
}

else
{
[Link]("Not Eligible for voting");
}
}

---------------------------------------------------------------------
return type
-----------------------
syntax:
----------------
return value;

EX:
-----
package Mypack1;

import [Link];

public class Myclass3


{
public static void main(String[] args)
{

cube(10);//calling function

[Link]("The value is:"+cube(10));


}

public static int cube(int n)


{
return n*n*n;//return 10*10*10-->return 1000
}
}
EX:
-----
package Mypack1;

import [Link];

public class Myclass3


{
public static void main(String[] args)
{

Mymethod1(12.3f);

[Link]("The Result is "+Mymethod1(12.3f));

public static float Mymethod1(float n)


{
return n*8;//
}
}

----------------------------------
EX:
----
package Mypack1;

import [Link];

public class Myclass3


{
public static void main(String[] args)
{

Mymethod1(3);

[Link]("The Result is "+Mymethod1(3));

public static float Mymethod1(int n)


{
return n*8;//
}
}

Common questions

Powered by AI

The 'void' keyword in Java method definitions indicates that the method does not return any value. It is significant when the method's purpose is to perform an action such as modifying an object's state or printing to the console, rather than computing and returning data. For example, 'public static void mymethod1()' prints a statement and expects no data return .

The 'public' access specifier in Java allows methods and classes to be accessible from any other class in different packages, ensuring maximum visibility. This means that if a method or a class is declared as public, it can be accessed by any other class, regardless of their package, further facilitating code modularity and reuse .

Parameter passing in Java methods works through passing arguments by value, meaning the method receives a copy of the variables provided by the caller. This allows methods to operate on the data without altering the original variables. For instance, in 'public static void add(int x, int y)', the function takes two integers and prints their sum, showing how data is passed into functions for processing .

Defining a method as 'public static' in a Java class makes it a class method, accessible without creating an instance of the class, which is important for utility classes and single-instance operations such as 'main'. If it's only 'public', it would require instantiation of the class it belongs to, impacting its utility by necessitating object creation, and not suitable for scenarios where class-wide operations need to be performed without specific instance context, such as commonly needed helper functions .

Method overloading in Java allows multiple methods in the same class to have the same name as long as their parameter lists differ. This enhances usability by permitting different implementations based on input types or numbers, providing flexibility and improved readability. It helps in creating more intuitive APIs by grouping similar operations under a single name differentiated by signature, leading to simpler and more organized code .

Java methods can specify return types, which determine the type of data the method will return upon completion. If a method has a return type other than 'void', it must include a 'return' statement that provides a value of the specified type. This return value can influence program flow by, for instance, serving as a condition in other operations or functions. For example, the method 'public static int cube(int n)' returns the cube of an integer, which can be used in further calculations or display operations .

Predefined functions in Java are those that are already available in the Java library, such as System.out.println() and Scanner's nextInt() method, which can be used directly without requiring explicit declaration . User-defined functions, however, are created by developers to fulfill specific, unique tasks based on the application requirements. For example, the function 'add(int x, int y)' is a user-defined function created to add two integers .

The 'main' method in Java serves as the entry point for program execution. It is essential because the Java Runtime Environment (JRE) calls this method to start the execution of an application. The syntax 'public static void main(String[] args)' allows it to be accessed publicly by the JRE, doesn't return a value, and accepts a string array for command-line arguments .

The 'static' keyword in Java indicates that a particular method or variable belongs to the class itself, rather than instances of the class. This allows the method to be called without creating an object of the class, which is significant for utility or helper methods that are universally relevant and need to operate without an instance context. For instance, in a main method context 'public static void main(String[] args)', it allows the program to start execution directly by the JVM without needing an object .

Using a Scanner for input operations facilitates user interaction by allowing programs to read data from various input sources like keyboard. It supports reading different data types using methods like nextInt(), nextLine(), etc., which helps in interactive applications. However, it also necessitates proper input validation to prevent user errors and handle exceptions effectively. For example, a scanner is used to read an integer input for the age check in a voting eligibility program .

You might also like