enthuware.ocpjp.v7.2.1295

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

enthuware.ocpjp.v7.2.1295

Post by itsriaz »

Hi could you please help how to get rid of the compile warning.
I changed the helpPeople method's argument type to Person

Code: Select all

 public void helpPeople(Queue<Person> people, Queue<Person> helped) { 
but it gives an error
The method offer(Person) in the type Queue<Person> is not applicable for the arguments (String)
and gives a suggestion to change the return type of getName to Person but again Person is not applicable for the argument (String)

Thanks in advance.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocpjp.v7.2 . 1295

Post by admin »

Not sure what exactly are you trying to do. Please post the exact code that you are trying, the warning message, and what is your objective.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by zhengye1 »

For reference later,
the original code is

Code: Select all

import java.util.*;
class Person{
    private String name; 
    public Person(String name) {
        this.name = name;
        
    } 
    public String getName() {
        return name;
        
    } 
    public void setName(String name) { 
        this.name = name; 
        
    } 
    public String toString() { return name; }
}

public class Helper{
    public void helpPeople(Queue people, Queue<String> helped){
        do{
            Person p = (Person)people.poll();
            System.out.println("Help : " + p + " ");
            helped.offer(p.getName());
        }while(!people.isEmpty());
    }
    public static void main(String[] args){
        Queue<Person> q = new LinkedList<Person>();
        q.offer(new Person("Pope"));
        q.offer(new Person("John")); 
        Queue<Person> helpedQ = new LinkedList<Person>(); 
        Helper h = new Helper(); 
        h.helpPeople(q, helpedQ);
    }
}
When you run from command line with -Xlint:unchecked, the warning message will be

Code: Select all

Helper.java:24: warning: [unchecked] unchecked call to offer(E) as a member of the raw type Queue
            helped.offer(p.getName());                                                           
                        ^                                                                        
  where E is a type-variable:                                                                    
    E extends Object declared in interface Queue                                                 
1 warning 
One way to fix is , for helpedQ, change to the definition to

Code: Select all

Queue<String> helpedQ = new LinkedList<>();
(Or LinkedList<String>, doesn't matter).

Also change the type of parameters in helpPeople to

Code: Select all

public void helpPeople(Queue people, Queue<String> helped)


This way it works because original helpedQ, it suppose contains the Person object, but offer takes String as a parameter. Also change the type of parameter of helpPeople because you need to let the compiler know helped queue is actually contains the String object.

Correct me if I am wrong

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by jagoneye »

zhengye1 wrote:Correct me if I am wrong
Yes you are right. Paul, you should also add the explanation for the line
help.offer(p.getName()) in the software I only noticed that after luckily clicking on the discuss link. The above line in the question means that
'helpdQ' will accept any object hence here a string is being added and it does not cause any runtime errors but when you change the method declaration to
public void helpPeople(Queue<Human> people, Queue<Human> helped)
you also need to change the line we discussed to:
helped.offer(p); because now the compiler will check for the type and
if you don't change the line then the compiler will complain that you should change the type of helperQ to <String>.
Also to suppress the compile time warning in the original program you can tag
the helpPeople method as following:
@SuppressWarnings("Unchecked")
public void helpPeople(Queue<Human> people, Queue<Human> helped) {

:)

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by shamran99 »

Hi,

I understood the fact that the type safety of generic classes is checked only at the compilation time. So the line

Code: Select all

helped.offer(p.getName());
compiles with just a warning message. But I believe the same line should throw a ClassCast exception at run time with the error message saying...

The method offer(Person) in the type Queue<Person> is not applicable for the arguments (String)

But it adds the string objects to the queue. Why its not preventing the add operation by throwing an exception?
Does that mean the Queue<Person> can be added with different types of objects too?

Please explain me.

Regards,
Shamran.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by admin »

Remember that no generic related information is present in the objects at run time. So even if, at compile time, you write the code to create a Queue<Person> object, at run time, the JVM only creates a Queue object. There is no generic type information in this Queue object. Since Queue allows any object to be inserted, the JVM has no problem when you insert a String in it.
If you like our products and services, please help us by posting your review here.

nowkarol
Posts: 5
Joined: Sun Aug 05, 2018 5:41 am
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by nowkarol »

It clarified this for me when I realized that adding

Code: Select all

Person person = helpedQ.poll();
throws ClassCastException and

Code: Select all

String person = helpedQ.poll();
doesn't compile in this case. So whole heap pollution thing appears only after read from polluted collection.

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by bvrulez »

Offer() adds something to the END of the queue. I got this wrong and spent some time googling basics. Here it is for other.

hrozhek
Posts: 2
Joined: Mon Nov 30, 2020 6:32 am
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by hrozhek »

I have only one question - why it is marked as easy? As for me it deserves to be a real brainer as it ruins basic concepts of the generics and in fact shows how you can (but should not to) avoid it restrictions.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocpjp.v7.2.1295

Post by admin »

Point taken. Toughness level has been increased.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests