True or False: If Variables Are Changed in a Java Method, They Are Also Changed Outside the Method

True or False: If Variables Are Changed in a Java Method, They Are Also Changed Outside the Method Featured Image

Continuing in our computer science problem series, I figured we’d dive into a couple of fun Java topics: parameter passing and primitive types. Specifically, we’ll be looking at whether or not you can change variables using methods.

Table of Contents

True or False Question

True of False: If `i` and `j` are changed in the following method, so are `a` and `b`.

void gcd(int i, int j) {
  // Code that modifies i and j
}
gcd(a, b)

Select one of the following:

  • A. True
  • B. False

Intermission

While you’re thinking about the answer (and so you don’t accidentally see the answer which is below), let’s talk briefly about how you can help The Renegade Coder grow.

The Renegade Coder, as it currently stands, is a solo operation. I’ve been at this since late 2016, and I’d love to make this style of content my full-time job. If you see value in the work I’ve put into these articles, you can show your support by subscribing to my PatreonOpens in a new tab.. Other ways to support the site including signing up for the mailing list and joining the DiscordOpens in a new tab..

In addition to writing, I dabble a bit in YouTube. Here’s my most popular video:

With that out of the way, let’s try to solve the problem.

Problem Solution

In this problem, the concept being tested is your understanding of primitive types in Java as well as your understanding of parameter passing. In Java, variables are passed to methods by value. For primitive types, that means the value itself is copied. Therefore, whatever happens to `i` and `j` can never be seen by `a` and `b` (i.e., B).

To see this in action, let’s use a simple example:

int a = 5;
int b = 11;

public static void swap(int i, int j) {
  int temp = j;
  j = i;
  i = temp;
}

swap(a, b);

Here, we’ve created a method that swaps the values of `i` and `j`. Unfortunately, this method is useless because the values of `i` and `j` immediately go out of scope as soon as the method ends. As a result, their values are deleted.

`a` and `b`, on the other hand, still exist. However, they are unchanged. The reason is that `a` gets copied into `i` and `b` gets copied into `j`. Therefore, whatever happens to `i` and `j` has no effect on `a` and `b`.

Interestingly enough, this concept extends beyond primitive types. For example, if we used the exact same code but swapped in the `Integer` reference type, we would still have the same problem. If you’re interested in why, I’ve written about this at depth. That said, that’s all I want to cover for this problem. Let’s move on!

Alternative Problem Solving Techniques

If you were approaching this problem without any knowledge of parameter passing or primitive types, you’re in luck. After all, true or false questions are down to a 50/50 chance, so you’re odds of accidentally getting the right answer are pretty good.

Outside of guessing, I’m not sure of any tricks to help get you the right answer for this question. I suppose you might be able to assume that methods work something like the following:

int a = 5;
int b = 11;
// start of method
int i = a;
int j = b;
// code that modifies i and j
// code that deletes i and j

If you think about methods in these terms, then you probably already have the intuition that the variables are copied, not aliased (for primitive types, of course). Therefore, the originals can’t change.

That said, that’s probably a bit of a stretch. Sorry I couldn’t help more!

Happy Learning

Hopefully, this article helped you learn or refresh some concepts—particularly around primitive types and parameter passing. Let me know if you want more content like this.

While you’re here, why not check out some of these related articles:

Otherwise, take care! See you next time.

A Computer Science Problem a Day (3 Articles)—Series Navigation

As an educator, I tend to dislike examination as a form of summative assessment. However, I do like multiple choice questions as a form of formative assessment—especially in the context of peer instruction. If done correctly, multiple choice questions are great way of testing concepts and exposing misconceptions. As a result, I thought it would be fun to put together a little series covering questions I’ve seen or used as a way of teaching computer science concepts. Enjoy!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Code Posts