1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
How can we pass multiple or variable number of arguments to a method on each invocation call?
We can use varargs by declaring the method parameter with three dots ...
.
public void childrenNames(String... names) {
for (int i = 0; i < names.length; i++)
System.out.println(names[i]);
}
Is Java both pass by reference and pass by value ?
Java is pass by value only. Even reference data types are passed by value.
Give an example of pass by value?
void passByValue() {
float gravity = 9.8f;
receiveByVale(gravity);
System.out.println("Gravity acceleration = " + gravity); // still 9.8
}
void receiveByVale(float gravity) {
// Attempt to change gravity
gravity = 10f;
}
What is Pass by Value?
Pass by Value means when a method is called, the copy of the actual parameter value is passed to the method. Changes made to the parameter inside the method do not affect the original variable.
Pass by Value nghĩa là khi gọi phương thức, một bản sao của giá trị tham số được truyền vào. Việc thay đổi tham số bên trong phương thức không ảnh hưởng đến biến gốc bên ngoài.
Pass by Reference
Pass by Reference means giving the method the actual location of the variable, so when the method changes it, the original variable changes too.
Pass by Reference là khi đưa cho phương thức địa chỉ thật của biến, nên khi thay đổi bên trong phương thức thì biến gốc cũng thay đổi theo.
What is Method Overloading?
Method Overloading means defining multiple methods in the same class with the same name but different parameter lists (different number, types, or order of parameters). This allows methods to perform similar tasks with different inputs.
Method Overloading là việc định nghĩa nhiều phương thức trong cùng một Class với cùng tên nhưng khác nhau về danh sách tham số (số lượng, kiểu hoặc thứ tự tham số). Điều này giúp các phương thức có thể xử lý các loại đầu vào khác nhau.
Consider the following overloaded methods and determine which method will be invoked for the call myOverloadedMethod(5)
?
void myOverloadedMethod(long arg) {
System.out.println("Method with long invoked");
}
void myOverloadedMethod(int arg) {
System.out.println("Method with int invoked");
}
Java ưu tiên tìm method có kiểu tham số chính xác nhất (exact match).
Ở đây, int
là kiểu chính xác với 5
.
Nếu không có method nhận int
, Java mới tìm phương thức nhận kiểu rộng hơn như long
.
Can the main method be overloaded?
Phương thức main có thể bị nạp chồng (overload) trong Java không?
Yes, the main method can be overloaded by defining multiple methods named main
with different parameter lists. However, the JVM only calls the standard main signature public static void main(String[] args)
to start the program.
Có, phương thức main có thể được nạp chồng bằng cách định nghĩa nhiều phương thức main với tham số khác nhau. Nhưng JVM chỉ gọi main với chữ ký chuẩn public static void main(String[] args)
khi chạy chương trình.