I recently started learning Java, and in my first few sessions we covered some of the most important basics. In this post, I want to walk through what I learned: methods, the main method, objects, how to call methods, and even building a small calculator program.
Methods in Java
Methods are blocks of code that perform a specific task. They make code reusable, organized, and easier to read.
We looked at four different types of methods:
1.With return type, without arguments
2.With return type, with arguments
3.Without return type, with arguments
4.Without return type, without arguments
Example
public class Mathematics {
//With return type, with arguments
public int addition (int a, int b) {
return a+b;
}
//Without return type, with arguments
public void int subtraction (int a, int b){
}
//Without return type , without arguments
public void String print() {
System.out.println(" Hello Everyone");
}
}
The Main Method
Every Java program starts with the main method. It is the entry point for the program, and without it the code will not run.
public static void main(String[] args) {
// program execution begins here
}
Objects in Java
Objects are instances of a class. Using objects, we can access the methods and variables defined inside a class.
Syntax:
ClassName obj name = new ClassName();
Calling Methods
Once a method is defined, we can call it using an object of the class.
public static void main (string[] args){
Mathematics arithmetics = new Mathematics();
System.out.println(arithmetics.addition(46,87));
}
Naming Conventions
We also learned about common naming conventions in Java:
PascalCase -> CalculatorProgram
camelCase -> simpleCalculator ( used for naming objects,methods etc...)
snake_case -> simple_calculator (lower),
upper snake case -> SIMPLE_CALCULATOR (upper)
lowercase -> simple
Consistent naming makes the code easier to read and maintain.
What I learned
In these sessions, I was able to understand:
-Different types of methods
-The role of the main method
-How objects work
-How to call methods
-Writing a simple calculator program
-Basic naming conventions
Learning Java step by step makes the language feel less intimidating, and building small programs like the calculator is a great way to practice.
Top comments (0)