• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

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

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 184, chapter 4, variable type is missing in line 14 of example
should be
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic