Parameters and Arguments, Constructors (OOP - JAVA)
Parameters and Arguments
Consider following code lines,
A a = new A(); << this is the way to create a object as we know
int b = 7; << int is class of Integer class, b is a variable, 7 is the value
String str = "duneth";
How to write the main method in java
public static void main(String[] args){
}
Methods
void myHeight( ){
}
- A method has a return type and a method name
above example: return type - void, method name - myHeight
consider following example,
void addNumbers( ) {
int a = 2;
int b = 7;
int c = a+b;
System.out.print(c);
}
( ) parameter is used for get the input and output values
return type is used for return the output or input
consider following example,
void area(int x, int y){
}
area(7,15);
- (int x, int y) is a parameter list
- area(7,15) is a argument list
When we add values to the argument list, it will transfer to the parameters for storing and then return the values as we wish(void or something).
consider the following example codes,
void strValue(int x, String y){
}
strValue(8,"abc12");
We should give arguments carefully considering the order of the variables inside the parameters
void area(int x, int y){
int z = x*y;
System.out.print(z);
}
area(7,15);
if we use void return type it can return any data types
consider following code lines,
eg: int area(int x, int y){
int z = x*y;
return(z);
System.out.print(z);
}
area(7,15);
eg: int m(int z){
return (z);
}
m(5);
If we look at above codes the return type is not void. It is int. So we should add return keyword inside the method to return the value.
Consider the following code line and find the bug.
String aBCD(String a, int b){
return "as2", 8;
}
aBCD("as2",8);
The problem of the above code is we have give two types of data(int and String) in arguments. but return type is String. So it will be occurred errors.
CONSTRUCTORS
Constructor is a specific method. It have some characteristics.
- We have to build a constructor using class name
- It doesn't contain any return type
eg: class Duneth{
Duneth(){
}
}
We can't run the constructor every time. We can call the constructor when we create a object.
Constructor is a method which does not contain any return type, has a name same to the class, can be called when creating a object.
Constructor Calling
When an application is running, firstly the constructor is working.
We added the things which we wish to do at the beginning of the program (application) into the constructor.
Even it has not any return type, It can pass the values as arguments to the parameters. So, constructor is able to use parameters and arguments.
Consider the following code,
public class Main{
Main{
System.out.println("java");
String name = "programming language is ";
}
public static void main(String [] args){
Main m = new Main();
}
}
class - Main
constructor - Main() {
}
main method - public static void main(String [] args){
}
object - Main m = new Main();
see the below picture carefully 👀
A class, A constructor, A main method, An object should be there.
.png)
Comments
Post a Comment