• Post Reply Bookmark Topic Watch Topic
  • New Topic

Long and autoboxing  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was studying and stumbled on this exercise :




What leaves me perplex is that "new Long(5L)" doesnt make it to the stocks array, why?
I am not so sure either what the L stands for does Long(5L) == Long(5) == 5L ?
 
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "L" indicates that the value is a Long literal. So 3L means the long value 3.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaelle Berton wrote: . . . does Long(5L) == Long(5) == 5L ?
No. The first two are different Long objects which return true from their equals() methods, and the third is a primitive recorded as a two's complement 64-bit integer with 0000...0101 as its value where ... replaces 56 0s.

Please always tell us where the question is from, so we know it is not a "cheating" source. There are "cheating" sources, which we prohibit use of.

Get a pencil and paper, and go through the execution, and see what the values are at each stage of the loop. Only that way will you understand how you are getting the output.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:. . . Only that way will you understand how you are getting the output. . . .
You will also know whether the Long valued 5L is in the array or not.
 
Gaelle Berton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question from the SCJP practice exam book (OCJP/SCJP: OCP Java SE 6 Programmer Practice Exams by Kathy Sierra and Bert Bates),
I did re write it to get the output and i the new Long(5L) object isnt in the array for a reason that i dont grasp. So I was wondering whether it was from the Long format, but apparently not.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for telling us the source. Sorry for not acknowledging that earlier; I have been very busy over the weekend.
 
Gaelle Berton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't the last object get added to the array?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the whole execution with a pencil and paper? If you do that all will become clear.
 
Gaelle Berton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eureka!

Yes, clearer, i think i was confused with the for each loop, thinking the first element was an iterator, whereas "ell" is the value of the element at the position in the array, not the position itself, well, i think. Thank you for your patience!
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for-each loop is put there to confuse you. The value ell is indeed the value you have got to, but the return; and break; statements stop the loop. I presume you have worked out whether the 5L was in the array or not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!