3

Since I have learned Java I was taught that Java only passes by value. But my Uni Professor discusses that Java is passed by reference for object and string. I am really confused right now and need some advice.

Comments
  • 2
    @rutee07 Java is cancerous but if you want to have a CS degree you need to know at least the basic of it
  • 6
    Java passes you the value for primitive data types like int (or integer? Can't remember), bool, byte etc. All the types that are lowercase and predefined.

    For everything that is stored on the heap (Obiects, Strings (not a primitive), Array, etc.) it passes you a reference.

    Note that Integer is not a primitive data type. Instance of Integer are objects (so reference) store an int that is usually unboxed and boxed again on the fly. There is also a fancy word for it that I forgot.
  • 2
    No it doesn't pass Only by Value, but instead @OnlyBytes .

    Jokes aside, like @OnlyBytes said, don't confuse your head
  • 1
    Well, if objects were passed by value you couldn't pass it to a function and let that function change that object (edit: and expect to work with the modified object after the method call), right? You'd have to return a new object or use some kind of special notation to explicitly pass a reference. It's pretty intuitive if you think about how it works in practice and what the implications are 👍
  • 1
    @irene That's true. Is it wrong of me to make a distinction between "value types" and "reference types", even though both are passed by value? I'm curious, since this is a bit out of my comfort zone.
  • 2
    @ihatecomputers That is not my point. Yes, you can pass an object's reference and change the value of the object. But the reference itself is passed by value
  • 1
    @AI-Overlord You don't sound confused any more 😏
  • 2
    @ihatecomputers I spent a little time reading the java language specification. I think I figured out something about the data accessing mechanism of java but I still don't get the exact detail :D please help me
  • 2
    @ihatecomputers And why the hell do you hate computers btw?
  • 1
    @AI-Overlord lol I'm just as confused as you are.

    Man, computers are a piece of shit. They are just too complex and there's one million ways that stuff can fail, it's like you're staring into the tesseract at the end of Interstellar but it's made out of 1's and 0's.

    You know when someone reported a bug for the first time like three years ago and it keeps happening but no one has been able to replicate it, but you have evidence of it occurring? Goddamnit, I hate computers.
  • 1
    @AI-Overlord not really most cs degress now interested in data science so either R should python should be preferred. If you want to build a software then yeah, learning OOP is required
  • 2
    Java passes by value.
    But in case of objects and String it passes object' or String memory address location. Though technically it still passes by value the location but when used you will end in using that object.
    So people say it passes object by reference.
  • 1
    @devTea Many universities have Java as a mandatory course.
  • 1
    Java always passes by value. For primitives that value is the primitive value. For any object that value is a reference to that object [NOT a copy of the object. Just a ref]
  • 1
    @irene that is quite a generalization :D
Add Comment