Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 2 Review Question 5 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Saumyaraj Zala
Ranch Hand
Posts: 75
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Should not the line no 6 be like :

The datatype of variable on the left hand side should be same as the type of array/Iterable. Integer and int are not o the same typr then why will the code compile?
Am I missing something that I will learn in next chapters?

Moreover wanted to clarify that in unary operator i++, the value first gets incremented and then it returns the origunal(old) value. Is my understanding correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saumyaraj Zala wrote:The datatype of variable on the left hand side should be same as the type of array/Iterable. Integer and int are not o the same typr then why will the code compile?

Since Java 5 you have the autoboxing feature. In short: if you have a primitive value, Java will convert it automatically to the relevant wrapper class for you (when needed). And it works the other way around as well: if you have a wrapper class object, Java converts it automatically to the relevant primitive type for you (when needed). That's called unboxing and that's what happens here in this code snippet.

Saumyaraj Zala wrote:Am I missing something that I will learn in next chapters?

It is explained in the study guide at page 136. And also in this forum you find useful and valuable explanations on autoboxing: e.g. here, here and here. If you have carefully read (and understood) all these topics, you will definitely ace every question on autoboxing on the actual exam

Saumyaraj Zala wrote:Moreover wanted to clarify that in unary operator i++, the value first gets incremented and then it returns the origunal(old) value. Is my understanding correct?

Everything you need to know about the post-increment operator can be found in this topic. It has excellent explanations and many illustrative code snippets. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic