0

In this code we have an int where we initialized with a value. Now we making this reference to another variable and assign a new value. But this should be reflected in other variable. But it does not. How this java reference is passed by value. Strings are immutable but how this happens in integer

public class Confusedwithintegerandstrings 
{

public static void main(String[] args) 
{
    
    int a=10;
    int c=a;
    System.out.println(c);
    a=20;
    System.out.println(a);
    System.out.println(c);
    
}

}

this is O/P

10

20

10

Community
  • 1
  • 1
seenome
  • 80
  • 1
  • 9
  • Even with a reference type this could result in two different prints. When you assign a new value to `a`, the value of `c` wouldn't be changed even with reference types. On the other hand, if you mutated the object `a` refers to, the object `c` refers to would be changed as well because they'd be the _same object_. – kviiri Dec 07 '13 at 11:54

1 Answers1

3

Actually your title and question mismatched.

Java is always pass by value. This is a correct statement for primitives. The confusion comes here for Object.

Consider this example (Objects)

someObject = someOtherObject

Here while assigning the reference someOtherObject is assigned to someObject and the value assigned is the reference.

Now we making this reference to another variable and assign a new value.

since a and c is a primitive and not an Object, so there in no matter of reference.

When you do this

 int c=a;  // value of a copied to c

Only Objects have references. Primitives are not Objects.

Might be helpful :Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • the reference is passed by value I meant like this. – seenome Dec 07 '13 at 11:50
  • 2
    @seenome, primitive types (such as `int`) are value types, you can't have references to them in Java. You can only have references to objects. Suresh Atta is correct. – kviiri Dec 07 '13 at 11:51
  • 1
    *"Java is always pass by value."*, I think that statement is misleading. Java is pass by value, including value of reference, *and* objects are passed by reference value. Leaving that 2nd bit out leaves reader wondering, how objects can be passed by value... – hyde Dec 07 '13 at 11:54
  • @hyde Makes sense to me. Edited little bit. – Suresh Atta Dec 07 '13 at 12:02
  • @hyde I agree wholeheartedly. Java passes primitives by value and objects by reference. I don't know why people insist on confusing the issue with that "always" statement; perhaps they think, mistakenly, that the simple misleading statement will make things easier to understand. – arcy Dec 07 '13 at 14:44
  • Another point of confusion for beginners, I think, is that String in Java is sort of a primitive but also definitely an object. It is a kind of primitive in that the language supports primitive-like constructs for it (such as +), and hides some primitive-like operations "behind the scenes" in order to keep the strings immutable. – arcy Dec 07 '13 at 14:48
  • @hyde: A variable whose type is `Object` or any subclass thereof is a container for some kind of object identifier. Object identifiers are passed by value just like primitives. If I have a local variable called `foo` of type `Object`, it happens to hold the identity of the 193rd object created since startup, and my code executes `someMethod(foo)`, that method may be able to change the properties of the 193rd object, but nothing it can do will change `foo`. When it returns, `foo` will still hold the identity of the 193rd object, just as it did when the method was called. – supercat Feb 04 '14 at 00:02