• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type is missing, chapter 4, on page 184 (Java OCA 8 Programmer I Study Guide)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 184, chapter 4, variable type is missing in line 14 of example
should be
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the positive side, the book clearly states this line does not compile But of course, it's intended to not compile because of mixing class and instance variables, not because of a missing (primitive) data type.

Let me test your knowledge a little bit Assuming total and count are both class variables of type int as inwhich of the following will compile
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: Let me test your knowledge a little bit Assuming total and count are both class variables of type int as inwhich of the following will compile

Thanks for your question, Roel. It is good that I am obliged to remember previous topic which I have already read At first, I think all lines will compile except line1. Line1 doesn't compile so i have already learned from OCA book (on page 55, Numeric Promotion section) I am a bit confused line5 & line6 and think that these compile for autoboxing. Then I test these codes in Netbeans and saw that line6 doesn't compile, it must be cast to long
We can't declare Long variable as this
We must add "L" after value or cast to long
 
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
Mushfiq Mammadov wrote:I am a bit confused line5 & line6 and think that these compile for autoboxing. Then I test these codes in Netbeans and saw that line6 doesn't compile, it must be cast to long

Exactly!

It's one of the most common mistakes with regard to autoboxing people make. You might think autoboxing will turn an int into a Long because you can assign an int to a long. But that's (as you discovered) not true. This topic has a very extensive discussion about wrapper classes, autoboxing and using the == operator. It's definitely worth reading.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually make mistake questions about "==" operator, especially for determining String equality.
Thanks for topic, Roel, I will read it absolutely.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35269
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:On the positive side, the book clearly states this line does not compile

. It was intended to be of type double.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: You might think autoboxing will turn an int into a Long because you can assign an int to a long. But that's (as you discovered) not true.

I came across this point in OCA book (chapter 4, page 196, second paragraph) today. Everythin is written clearly in it, java will only do one conversion.
 
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
Mushfiq Mammadov wrote:
Roel De Nijs wrote: You might think autoboxing will turn an int into a Long because you can assign an int to a long. But that's (as you discovered) not true.

I came across this point in OCA book (chapter 4, page 196, second paragraph) today. Everythin is written clearly in it, java will only do one conversion.

It's indeed explained very well. Just like to add: besides being an Object, Integer IS-A Number as well. So this code compiles:And now the 1 million dollar question: what's the output of this program?

In this post you'll find 15 little code snippets with overloaded methods and it's up to you to determine which method will be invoked. Good luck!
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: Just like to add: besides being an Object, Integer IS-A Number as well.

I see Number at first time although since JDK1, thanks for new information, Roel

Roel De Nijs wrote:
So this code compiles:And now the 1 million dollar question: what's the output of this program?

If Number is Integer play(4); calls play(Number n) and play(4L); calls play(Long l). Similiar example as this is explained very well in the last section of Overloading Method topic, I don't remember page exactly because my book is in the work. At first overloading method find exact type. In this example play(4); find int type at first, then Integer, then Object. So Integer is Number therefore calls play(Number n).
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: In this post you'll find 15 little code snippets with overloaded methods and it's up to you to determine which method will be invoked. Good luck!

Example 7 and 15 are more interesting, I make mistake with those and learnt from my mistake. Thanks a lot, Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic