• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Sybex - OCP 11 - Answers Chapter 5 - Answer 3 - B is valid

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

The answer should be A, B, C, F.

Every non-primitive type is of type Object. String is a non-primitive type, making option B correct. koko is both an Object and a String.

Thanks,

Bassam
 
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bassam:

Please post the specific book title in each case, rather than just Sybex, because there are many variants of the book (different target exams and different book versions).

I am answering based on OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide (Exam 1Z0-815).
Tho that exam has been mushed together with the 816 with a few other changes into the 819, the book is still totally valid with a few notes from the website of the authors, for the 819.

The publisher/authors have granted exemption to post details of questions or small book sections you have questions about on this forum, so don't post content from the book in random places on the web, but here is fine [you could post the whole question].  This enables those forum participants who may not have a copy of that specific book on hand to answer as well as the authors.

This question tests your knowledge of local variable type inference.
While it is true in both Java and some other languages that literally every non-primitive type can be assigned to, or used as, an Object, due to inheriting from it implicitly if no other superclass is specified, or indirectly if one is, we would say that the type of the reference is the specific sub-class of the object, not Object itself.

Where this could make a difference is for example if we had a line something like this later:

Car myCar = (Car) koko;

Were the datatype of the reference koko to be Object, that line would compile just fine and then fail at execution with a runtime exception (ClassCastException), something like:
Exception in thread "main" java.lang.ClassCastException: class String cannot be cast to class Car (String and Car are in unnamed module of loader 'app')
at CrestedPenguin.main(CrestedPenguin.java:20)
As it is, the compiler knows that koko refers to a String due to type inference, not just some random object, and a line such as the above would not even compile, receiving an error:
Cannot cast from String to Car.

This is, in my opinion, good, because it catches whole classes of errors before they ever get to execution, but regardless is how Java works and knowledge of this is reflected on exam questions.

So an object of any given type can be assigned to or used as an Object or any of the object's other superclasses (if any) without a cast, but the reference type is fixed at time of declaration, or in the specific case of Local Variable Type Inference, statically at the time of compiling the line containing 'var myVar = <some type of value>'

This makes a difference in a number of places in Java, one of the simplest being the line I showed that would not compile.
 
Jesse Silverman
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, for reference, since somebody might disagree with my answer if they saw, the relevant parts of this question are:


A. The data type of koko is String
B. The data type of koko is Object

In addition to LVTI, this question is testing that we understand the use of the "Diamond" syntax first introduced in Java 7.
The type of gorillas is List<String>.

LVTI then treats the for expression exactly the same as if we had said:


I mentioned one of the simplest cases that this makes a difference, there are many others.

Fussy individuals working in multiple languages would like to say "The reference type of koko is String" to be more precise, because different languages work differently, but it would be common for people working primarily or exclusively in Java to simply say "The data type".

The rest of the question shows that you recognize two different situations where the LVTI would only be able to conclude that its reference type was some generic Object, rather than a String, but the OP got that part right.

The only question was whether we describe the reference type of a reference as to being both a class and all its superclasses, or just its declared type, so that is what I addressed.
 
Bassam Gemayel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jesse,

Use the instanceof type comparison operator for any non-primitive type.

If (koko instanceof T) is true then koko is of type T.

Bassam
 
Jesse Silverman
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LVTI is just about the declared, compile-time type of a reference variable.



Instanceof looks at the actual object being pointed to by the reference.

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface

 
Jesse Silverman
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Watching a video on doing OOPS right, which happens to be in C++ rather than Java, but I believe the terminologies are the same.

On the slide I am looking at, they make a clear distinction between the "Static (Compile-time) type" and "Dynamic, (Run-Time) type".

This is the distinction I was trying to make.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic