Dcit 50 - Java Methods
Dcit 50 - Java Methods
Dcit 50 - Java Methods
tasks within applications. These functions are commonly known as methods. A method in
programming is a named collection of code that can be executed at any point in a program. When
you use the method's name, your program jumps to the code within that method. Once the
method finishes its task, your program returns to where it was called from, allowing it to proceed
to the next line of code. In Java, a method is a set of instructions that performs a specific task and
can return a result to the caller. A Java method can also perform a task without returning
anything. Java methods enable code reuse without the need for redundant typing. In Java, every
method must be part of a class, which sets it apart from languages like C, C++, and Python.
ava Methods
A method is a block of code that performs a specific task.
Suppose you need to create a program to create a circle and color it. You can
create two methods to solve this problem:
Dividing a complex problem into smaller chunks makes your program easy to
understand and reusable.
returnType methodName() {
// method body
}
Here,
If the method does not return a value, its return type is void .
For example,
int addNumbers() {
// code
}
In the above example, the name of the method is adddNumbers() . And, the return
type is int . We will learn more about return types later in this tutorial.
This is the simple syntax of declaring a method. However, the complete
syntax of declaring a method is
Here,
For example, the sqrt() method of standard Math class is static. Hence,
we can directly call Math.sqrt() without creating an instance of Math class.
parameter1/parameter2 - These are values passed to a method. We
can pass any number of arguments to a method.
// create a method
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
return sum;
}
Sum is: 40
Here, we have called the method by passing two arguments num1 and num2 .
Since the method is returning some value, we have stored the value in
the result variable.
Note: The method is not static. Hence, we are calling the method using the
object of the class.
Ethics in Java programming encompass several key principles that guide responsible and
professional coding practices. These include: The imperative to write well-structured and well-
documented code that is easy to understand and maintain, benefiting both the developer and
future contributors. And the ethical duty to ensure that code is secure and free from
vulnerabilities, particularly in applications handling sensitive user data. Also, the respecting and
protecting user privacy by responsibly collecting and handling data and maintaining transparency
regarding data usage. And being forthright about the functionality and limitations of the code,
especially when it interacts with sensitive data, to prevent misuse or misunderstandings. Also, the
ethical obligation to conduct comprehensive testing to prevent the introduction of errors or bugs
that could harm users or their data. And its upholding intellectual property rights by adhering to
copyright and licensing agreements, especially when integrating third-party code. And the ethical
participation in open-source projects, fostering knowledge sharing and community collaboration,
benefiting the broader development community. These ethical considerations form the
foundation of responsible and professional Java programming, ensuring code functions
effectively while upholding societal well-being.
Java has a fascinating history that began with its original design for interactive television,
although it proved too advanced for the digital cable TV industry of that time. The inception of
Java traces back to a group known as the Green Team, who aimed to create a language for digital
devices like set-top boxes and televisions. However, its true calling was found in internet
programming. Later, Netscape integrated Java technology into its products. The guiding
principles behind Java's creation included simplicity, robustness, portability, platform
independence, security, high performance, multi-threading, architecture neutrality, object-
oriented design, interpretability, and dynamic capabilities. This versatile programming language,
often attributed to James Gosling, who is affectionately known as the "father of Java," was
developed in 1995, with the project taking shape in the early '90s. Today, Java is ubiquitous,
finding applications in internet programming, mobile devices, games, e-business solutions, and
more. Its history showcases its evolution from an idea for interactive television to a fundamental
part of modern technology.
In conclusion, a Java method is a fundamental building block of Java programming, offering
a structured way to encapsulate and execute specific tasks. These methods play a crucial role in
code organization, reusability, and maintaining the integrity of Java programs. As we've
explored, Java methods can either return results or simply perform tasks, providing flexibility in
addressing a wide range of programming requirements. Java's method-based approach promotes
modular and efficient software development, and it aligns with best coding practices and ethical
considerations.
Reference:
R, V. M. (2021). What Are Methods In Java? Know Java Methods From Scratch.
Edureka. https://www.edureka.co/blog/java-methods/
Programiz. (2023). Java methods. www.programiz.com.
https://www.programiz.com/java-programming/methods
GeeksforGeeks. (2023b). Java methods. GeeksforGeeks.
https://www.geeksforgeeks.org/methods-in-java/
History of Java - Javatpoint. (n.d.). www.javatpoint.com.
https://www.javatpoint.com/history-of-java
Lesson: Object-Oriented Programming Concepts (The JavaTM Tutorials > Learning the
Java Language). (n.d.). https://docs.oracle.com/javase/tutorial/java/concepts/index.html