0

'primitive variables store the actual values, whereas reference variables store the addresses of the objects they refer to' https://stackoverflow.com/a/32049775

I'm trying to wrap my head around primitive and reference types in Java. Here's my thinking:

The integer i1 has the value 5, and if i1 = i2, that means it's also 5, but since they're both distinct variables, their addresses are different. Their value is the same, but saying i1 = i2 doesn't mean they're the same.

The string s1 = "bread", and the object "bread" has an address. Let's say s1 = s2. They have different addresses, yet their value is the same as the address of object "bread". Is this correct thinking? I can see the differences when I put it like that, but I just want to know what you think and what the best way is to understand these differences. Thank you!

0 Answers0