In: Computer Science
When are values passed as parameters?
a. When parameters are primitives or wrappers
b. When parameters are user-defined objects
c. When parameters are objects
What is the result of a == b?
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
}
a. True
b. False
c. Compile Error
I have explained all the three parameters along with examples in order to make it clear.
Second question , answer is false .
I have provided reasoning .
If you have nay problem, feel free to ask.
Explanation:
Ans (a)
When parameters are primitive , then we can pass values with primitive data types like (int , short int , long , double , float , char , boolean, byte )
For eg:
int Sum( int a , int b)
return a+b;
Here a and b are primitive parameters
Ans (b)
When parameters are user defined objects , they are passed using constructor function.
For eg:
Class sum()
{
int sum( int a , int b)
{
this.a = 5;
this.b = 7;
return a+b;
}
Here a and b are user defined objects.
Ans (c)
Objects as parameters can also be passed in java , in any function.
For eg:
Class student {
import java.util.Scanner;
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name, int age){
this.name = name;
this.age = age;
}
public Student copyObject(Student std){
this.name = std.name;
this.age = std.age;
return std;
}
public void displayData(){
System.out.println("Name : "+this.name);
System.out.println("Age : "+this.age);
}
Here copyobject() function takes objects of student class (std) as parameter.( I have boldened it to highlight)
Q- Result of a==b
(b) False
The result is false because these two arrays are reference to two different objects and when we use comparison operator(==), it compares reference variables. Therefore the result is false .
But if we want to compare two arrays in java, a method called Arrays . equals() is provided . We may pass arrays as parameters , in order to compare.