About Question enthuware.ocajp.i.v8.2.1409 :

Moderator: admin

Post Reply
Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

About Question enthuware.ocajp.i.v8.2.1409 :

Post by Ad9999 » Thu May 21, 2015 7:35 am

Code: Select all

public class Test{     
public static void testInts(Integer obj, int var){         
obj = var++;         
obj++;     
}    

public static void main(String[] args) {       
  Integer val1 = new Integer(5);       
  int val2 = 9;        
  testInts(val1++, ++val2);         
System.out.println(val1+" "+val2);    
   } 
}       
I'm not too sure if I'm understanding this correctly.

When testInts is executed, val1 is set to a new Integer(6), val2 is set to 10.
5 and 10 are then passed as arguments, but since obj is set to point to the var++ object, obj no longer has any relation to val1?

If my thinking is correct then if "obj = var++;" was removed, val1 would be set to a new Integer(7) object, and the result would print 7, 10?

Hopefully I'm understanding this correctly. :)

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Thu May 21, 2015 7:51 pm

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by Ad9999 » Fri May 22, 2015 1:58 am

Actually when removed the result is the same. It seems Integer obj and int var have no relation to val1 and val2 respectively after being assigned their values.

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Fri May 22, 2015 9:26 pm

Very good. You might want to read this to further clear your understanding - http://www.javaranch.com/campfire/StoryPassBy.jsp

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by dannysantos1985 » Tue Dec 01, 2015 2:35 pm

Isn't it true that if the Integer val1 is between -128 and 127 then the val1++ doesn't create a new object?
If its true, you shouldn't say "But val1 is set to point to a new Integer object containing 6."
Please clarify me

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Tue Dec 01, 2015 8:34 pm

It is not a "new" object in the sense that it is created fresh. It is new in the sense that it is not the same val object. It is picked up from the cache.
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by dannysantos1985 » Wed Dec 02, 2015 11:17 am

1)so val1++ always creates another object, even if i is between -128 to 127, correct? [Yes or No only]
2)I used to understand cached but now I don't know. for example, Integer i=2; i++; what happens exactly when we make i++? The i = new Integer(i.intValue() +1) still applies?
2.1)More importantly, how does the cache works in here. I used to think that the cache is a Integer object in which the state is altered (and not created a new object). is that correct? If so, how come val1++ uses another object?
Thank you

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Wed Dec 02, 2015 8:24 pm

1. Not exactly. val1++ always returns another object i.e. an object different from the one val1 is pointing to currently. It does not necessarily "create" another object because it may return an object from the cache of Integer objects. As per Section 5.1.7 of JLS:
If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
Here is a simple test:

Code: Select all

      Integer i = 1;
      Integer j = 2;
      Integer k = 1;
      System.out.println(i==k); //will always print true 
      i++;
      System.out.println(i==j); //will always print true but try with i and j beyond -128 to 127.
2. No, i++ doesn't mean new Integer(i.intValue() +1). It means - first do i.intValue() +1, now apply the boxing conversion on the value as per the rule given above. i.e. if the value is between -128 to 127, then use the cached object otherwise it is up to the JVM implementation.

3. Integer (and other primitive wrapper objects) are immutable. So they don't change at all. It is the reference that points to a different object. In the cache, there are individual Integer objects - one for each int from -128 to 127. Depending on which int value you are trying to box, you will get an Integer object corresponding to that value.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by dannysantos1985 » Thu Dec 03, 2015 11:16 am

So the JVM creates "individual Integer objects - one for each int from -128 to 127" on the startup? and then the program just uses that Integers in case of need?
I think I am understanding now

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Thu Dec 03, 2015 9:02 pm

dannysantos1985 wrote:So the JVM creates "individual Integer objects - one for each int from -128 to 127" on the startup?
Not necessarily at startup because that is not mandated by the specification. But definitely sometime between startup and when they are needed.
If you like our products and services, please help us by posting your review here.

fabioebt
Posts: 2
Joined: Wed Sep 21, 2016 10:14 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by fabioebt » Wed Sep 21, 2016 10:21 pm

Hello everyone, about this question too.
I tested it on eclipse with Java 8 and the output was: 7 11
But in the question is marking the right choice this output: 10 9

Does anyone knows why?

Thank you.

:thumbup:

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Wed Sep 21, 2016 10:28 pm

The choice that is marked as correct is option 4: 6 10 (not 10 9) and that is indeed correct.

IDEs sometimes obfuscate things. So it is best to use command line and notepad when you are a beginner. Make sure your code is exactly same as the one given in the problem statement.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

fabioebt
Posts: 2
Joined: Wed Sep 21, 2016 10:14 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by fabioebt » Wed Sep 21, 2016 11:08 pm

exactly, the correct output is 6 10

I found my error :oops:
When I was debbuging de code, I executed the key shortcut "CTRL + shift + i" in val1++, and it changed the value :oops:

My lack of attention. Sorry about this.

Thank you Paul.

elit3x
Posts: 7
Joined: Tue Oct 04, 2016 6:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by elit3x » Thu Oct 13, 2016 12:32 pm

Is there any instance where changes are made to a variable within a method and those changes are reflected outside of the method without a return statement?

I understand if a function makes changes, returns the changed value, and the caller assigns it to an existing variable those changes will be reflected outside the method.

But, without a return statement, will the result of ++ / -- or += operators ever be transmitted back to the caller since it is essentially a new object?

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Thu Oct 13, 2016 9:07 pm

No, Java uses pass by value for everything. So there cannot be any situation where you change the variable in a method and that change is reflected outside.

Of course, if you use a variable to access and change an object, that change in the object will be visible from anywhere else.
If you like our products and services, please help us by posting your review here.

qmwuzapz
Posts: 3
Joined: Sun Jan 29, 2017 1:54 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by qmwuzapz » Tue Jan 31, 2017 12:48 pm

At the moment we assign obj reference at testInts method to var++
we are changing the reference of obj so it's no longer pointing to the old object of val1 (before increment )?

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by Mihai1977 » Wed Mar 01, 2017 1:52 am

Hi,

Please explain me, in the method "testInts" which has 2 parameters, a wrapper object and a primitive. The line obj++; increase the value of "obj", but why? Because the wrapper object are immutable. Thx.

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Wed Mar 01, 2017 1:58 am

Yes, wrapper objects are immutable. But when you do obj++, it creates a new wrapper object with the incremented value. It is not the same object as the previous one.
If you like our products and services, please help us by posting your review here.

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by Mihai1977 » Wed Mar 01, 2017 2:21 am

Thx for the answer, but I do not understand the difference between String objects and wrapper objects for immutability. For example:

Code: Select all

String s1="1";
String s2=s1.concat("2");
s2.concat("3");
System.out.println(s2);
The answer is "12". Why for wrapper is enough to say "obj++" and not "obj=obj++"? Thx in advance.

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Wed Mar 01, 2017 2:32 am

Because ++ operator is overloaded for numeric types. In other words, Java language allows ++ operator to be used to increment numeric types but not strings. See this discussion: http://stackoverflow.com/questions/1328 ... er-classes

Also, concat is a completely different method. It has nothing to do with ++. s2.concat("3") creates a new String object. You need to assign it to s2 again.
If you like our products and services, please help us by posting your review here.

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by Mihai1977 » Wed Mar 01, 2017 2:47 am

thx. So "obj" is unboxed, the resulted int is incremented and then the incremented int is autoboxed into a new object. Right? :)

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Wed Mar 01, 2017 3:50 am

That is correct.
If you like our products and services, please help us by posting your review here.

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by zel_bl » Wed Mar 06, 2019 3:34 am

To see if I understand it correctly.
val1 object with the boxed value of 5 is created. val2 primitive is assigned value 9.
Method testInts is invoked. Original reference val1 is noted down (on a stack?), then incremented (which means new val1 object is created with the incremented value of 6). Noted reference that refers to the value of 5 is passed as argument one. val2 original is incremented first (is it noted down on a stack?). Copy (incremented) is passed as the argument two.
Inside of the method: Argument one reference (which refers the obj. with the value of 5) is assigned the value of 10 (arg 2), then arg 2 is incremented to 11, and arg 1 reference now refers to the new object with the value of 10.
Arg 1 reference is incremented then, which means it refers to the new object with the value of 11. Method ends. Scope of the arguments ends.
Original val1 referes to the object with the value of 6, and val2 primitive is 10.
I'd appreciate if you could explain the process of passing arguments to methods (and if those args are expressions) on the stack and heap level.
Thanks

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1409 :

Post by admin » Wed Mar 06, 2019 4:55 am

Yes, that is pretty much it.

Explaining the whole process will be a bit too much for me on this forum but any good Java book will explain this. I know OCAJP 8 Fundamentals by Hanumant Deshmukh explains it in Section 8.7.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests