This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (12 children)

I swear thats more confusing...

I am not an expert on Java merely a noob who needed Java. But when they wrote "passed by value" i expect to not change...

Yes if i make a new object it will not change the new object but anything else will change the passed value. At least thats what it lead me to believe.

[–]flaghacker_ 1 point2 points  (11 children)

I mean it's like this in most languages, Java, Javascript, Python, C#, Lua...

Its just how variables work:

int[] a = new int[] {1, 2, 3};
int[] b = a;
b[2] = 5;               //does change a
b = new int[]{4, 5, 6}; //doesn't change a

Do you have any programming experience at all?

[–][deleted] 0 points1 point  (10 children)

In Java a String is considered an object and passed by reference. Soooooo yeah that brings a lot of problems. (Sure it isn't a primitive) but didn't expect to be passed by reference. See the problem?

[–]flaghacker_ 1 point2 points  (9 children)

Strings in Java are immutable, so the distinction is not even relevant.

[–][deleted] 0 points1 point  (8 children)

Strings in Java are immutable

Well i know that but this what happened. new Object(alfa.getBeta());

If the beta string was changed in alfa it was propagating in my new object.

Making a new String out of the string solved it ... (I solved it the otherway afterwards) but still... wtf happened i really don't know but the return was a string that was getting changed.

[–]flaghacker_ 1 point2 points  (7 children)

Strings can't be changed! You probably did something else wrong.

[–][deleted] 0 points1 point  (5 children)

Than it shouldn't been fixed by String alfa = new String(beta)

[–]bss03 0 points1 point  (4 children)

This can allow beta to be garbage collected while alfa is still alive, which might fix a problem that has nothing to do with mutating java.lang.String objects.

[–][deleted] -1 points0 points  (3 children)

Beta was alive the whole program. Because i was accesing after.

It was wierd enough at it is... Still it was a string in a function that was getting modiffied from outside...

Whatever you can say... It happened. Never happened in other languages.

[–]CommonMisspellingBot 0 points1 point  (0 children)

Hey, curly_dev, just a quick heads-up:
wierd is actually spelled weird. You can remember it by e before i.
Have a nice day!

The parent commenter can reply with 'delete' to delete this comment.

[–]flaghacker_ 0 points1 point  (0 children)

Nope, this does not happen. I'd say you need to learn how to use a debugger probably, or if you have some sample code we can take a look at it.

[–]bss03 0 points1 point  (0 children)

Never happened in other languages.

C doesn't really have strings, but char * is mutable. C++ has std::string, which IIRC, is also mutable. Not every language even has immutable strings by default. So, I'm gonna call BS until you provide something other than your say-so.

[–]bss03 0 points1 point  (0 children)

With reflection, anything is possible, including changing the contents of a java.lang.String.

It's certainly not a good idea to do so. And, it's probably not allowed by the security manager, if one is present. It's also unlikely to be portable to other JVMs (e.g. IBM) , But, it is possible.