Page 1 of 1

enthuware.ocpjp.v7.2.1295

Posted: Mon May 04, 2015 5:19 am
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.

Re: enthuware.ocpjp.v7.2 . 1295

Posted: Mon May 04, 2015 8:15 am
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.

Re: enthuware.ocpjp.v7.2.1295

Posted: Wed Apr 20, 2016 1:51 pm
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

Re: enthuware.ocpjp.v7.2.1295

Posted: Thu Jan 05, 2017 9:58 am
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) {

:)

Re: enthuware.ocpjp.v7.2.1295

Posted: Wed Apr 18, 2018 10:03 pm
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.

Re: enthuware.ocpjp.v7.2.1295

Posted: Wed Apr 18, 2018 10:53 pm
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.

Re: enthuware.ocpjp.v7.2.1295

Posted: Thu Aug 23, 2018 1:54 pm
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.

Re: enthuware.ocpjp.v7.2.1295

Posted: Thu Feb 20, 2020 3:24 am
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.

Re: enthuware.ocpjp.v7.2.1295

Posted: Tue Dec 22, 2020 4:46 am
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.

Re: enthuware.ocpjp.v7.2.1295

Posted: Tue Dec 22, 2020 10:34 pm
by admin
Point taken. Toughness level has been increased.
thanks for the feedback!