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
- Intermission
- Problem Solution
- Alternative Problem Solving Techniques
- Happy Learning
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 Patreon. Other ways to support the site including signing up for the mailing list and joining the Discord.
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:
- Beware of Division by Zero in Java
- Be Careful When Modifying Data While Using a Java Iterator
- The “else if” Keyword Doesn’t Exist in Java
Otherwise, take care! See you next time.
Recent Code Posts
Unpacking the Jargon Around Compilers, Interpreters, and More
Today, we're going to get a little pedantic and try to define concepts like compiler and interpreter. Of course, I ultimately don't think it matters what the exact definitions are for practical...
Generally, people think of Python as a messy language because it lacks explicit typing and static type checking. But, that's not quite true in modern times. Surely, we can take advantage of type...