Categories: Uncategorized

Methods in Java

Example 1

class Example{
public static void main(String args[]){
System.out.println("John");
System.out.println("Peter");
print(); // Method Calling statement
System.out.println("Anne");
System.out.println("James");
print();
System.out.println("Steve");
System.out.println("Jim");
print();
}
public static void print(){ // Method Declaration
System.out.println("=======");
}
}

Output

John
Peter
=======
Anne
James
=======
Steve
Jim
=======

Example 2

import java.util.*;
class Example{
public static void calculator(){
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter Number 1 : ");
int number1 = input.nextInt();
System.out.print("Enter Number 2 : ");
int number2 = input.nextInt();
System.out.println();
System.out.println("Total : " + (number1 + number2));
System.out.println();
}
public static void main(String args[]){
System.out.println("Start.....");
calculator();
System.out.println("End.....");
}
}

Output

Start…..

Enter Number 1 : 45
Enter Number 2 : 67

Total : 112

End…..

Parameterized Method

class Example{
public static void print(String text,int number){ // Parameter --> Parameterized Method
System.out.println("Your Text : " + text);
System.out.println("Your Number : " + number);
}
public static void main(String args[]){
print("Hello Java",10); // Argument
}
}

Output

F:\javas>java Example
Your Text : Hello Java
Your Number : 10

class Example{
public static void main(String args[]){
int[] arr = {10,20,30,40,50};
for(int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
addFive(arr);
for(int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
}
public static void addFive(int[] numbers){
for(int i = 0 ; i < numbers.length ; i++){
numbers[i] = numbers[i] + 5;
}
}
}

Output

10 20 30 40 50
15 25 35 45 55

 

admin

Recent Posts

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

4 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

3 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago