Skip to main content

Command Palette

Search for a command to run...

Object Oriented Programming in Java

Offered by Coursera

Updated
1 min readView as Markdown
R
Enthusiatic Software Developer with over 4.11 years of experience in building and optimizing Web applications. Proficient in backend development using Java, Spring Boot, Monolithic and Microservices architecture within agile methodologies.

You will be able to understand...

  1. Is Java pass by values or pass by reference ?
  2. How memory scope does works for methods and members ?

Important Note: Java is pass by value.

What is the output from the following code?

public class Question2 {
   public static void main(String[] args) {
      int a = 5;
      int b = 7;
      swap(a,b);
      System.out.println(a + ", " + b);
   }
   public static void swap(int a, int b) {
      int temp = a;
      a = b;
      b = temp;
   }
}

A) 5, 7

B) 7, 5 //This response is incorrect. Java is pass by value, so the swap method only swaps the parameters a and b, not the original values of a and b in main.

Output: 5, 7

Java Memory Models

key points:

  1. main method's scope
  2. Constructor's scope
  3. The Heap

(what about getter, setter, static, class?)

Example: 1. image.png

2.- What is the output here? image.png

3. image.png

4. image.png

5. image.png

6. image.png

7. image.png