Why do Scanner and String behave differently in Java function calls?

0
14
Asked By CuriousCoder93 On

I'm trying to build an assembler and ran into some confusion with how reference types work in Java. I created a `Scanner` object from a file, and when I passed it to a function, any changes made to the `Scanner` inside that function affected the original object. I'm not well-versed in Java, so I initially thought that creating new objects was similar to C++ pointers, thinking that this reference behavior was the issue.

To test my theory, I did the same thing with a `String`. However, I found that changes made to the `String` inside the function didn't affect the original. Here's the code I used:

For the `String` example:
```java
String yoMama = new String("This is a String obj");
dename(yoMama);
```
This outputs "This is a String obj" as expected.

And for the `Scanner`:
```java
Scanner scnr = new Scanner(yoMama);
dename(scnr);
```
This outputs "This_is_not_a_String_obj", which indicates the original object was modified.

So, are `Scanner` objects treated as global by default in Java, or is there a different explanation for this behavior?

2 Answers

Answered By PointerPro On

You're spot on that both types are reference types, but here's the key: `Scanner` behaves like a pointer in C++ because you can modify its state, while `String` does not allow for modification—it's immutable. When you change the `Scanner`, the reference in both the calling and called functions point to the same object, hence both reflect the changes. But when you attempt to 'change' a `String`, you just create a whole new `String` object! This leads to the results you're seeing.

JavaJunkie -

Right, so even though it's confusing, the crucial part is that in Java, all objects are passed by value (of the reference), and since `String` is immutable, any attempt to change it results in creating a new object instead of modifying the existing one.

CodeNerd123 -

Exactly! This behavior is something all Java programmers grapple with at first. Once you get it, you’ll find it easier to manage objects and understand their references in Java.

Answered By SyntaxSavvy On

Both `String` and `Scanner` objects are reference types in Java, which means they are passed by reference, but not in the way you might think. When you're passing them to a function, you're passing a copy of their reference by value.

In the case of the `Scanner`, you’re working with the same object in both the original method and the called method, so any changes you make to it reflect in both places. Meanwhile, strings are immutable. When you try to change a `String`, you’re actually creating a new `String` object instead of modifying the original. That's why the changes to your `String` inside the function don't affect the original string. So really, the difference comes down to how each type behaves in terms of mutability and reference management!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.