Explicit object casting in Java
<p>I'm relatively new to Java, thus the question. Why does Java cast objects and what exactly goes on with the compiler when an object is being cast. This is my code for a simple level order search.</p>

<pre><code>void LevelOrder(Node root)
    {
       Queue&lt;Node&gt; Q = new LinkedList();
       Q.add(root);
       while(!Q.isEmpty()){
           Node p = Q.poll();
           System.out.print(p.data+ " ");
           if(p.left != null) Q.add(p.left);
           if(p.right != null) Q.add(p.right);
       }


    }
</code></pre>

<p>When I run this code on a web-based IDE provided by hacker rank it compiles but gives me the following warning.</p>

<pre><code>Note: Solution.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
</code></pre>

<p>When I cast the node object by changing the line of <code>poll</code> to this,</p>

<pre><code>Node p = (Node)Q.poll();
</code></pre>

<p>It still compiles showing the same warning.</p>

<p>However on my IntelliJ IDE the first code wouldn't even compile and only the second one(with the explicit cast) compiles. </p>

<p>Can someone explain what's going on here exactly.
Aside from resolving the error can someone please explain why one compiler lets this line compile, whereas the other doesn't without explicit casting.</p>

<pre><code>Node p = Q.poll();
</code></pre>