简体   繁体   中英

incompatible types: java.lang.Object cannot be converted to java.lang.String?

I am currently new to Java and have no prior programming experience, I am currently trying to code a solution to a problem proposed during my university interview :)

//Winner rabbit variable to hold the winner of the 'race'
    String winner;
    winner = yettoracequeue.element();

Background about problem:

  1. assign the first item in queue to a string variable
  2. remove first item from queue
  3. assign the second item in queue to another string variable
  4. compare both variables and assign the answer to a new variable

I do not understand why yettoracequeue.element() is considered an object when the result is a string, eg Rabbit, and hence I am unable to assign it to the String variable that is winner.

TIA :)

Edit:

This is the full code

package queuepart;
import java.util.*;
public class QueuePart {

  static String nextline = System.getProperty("line.separator");

  public static void main(String[] args) {
    //Step 1: Create LinkedList() to assign to yettoracequeue
    Queue yettoracequeue = new LinkedList();

    //Step 2: add rabbits to queue
    int rabbitno = 1;
    yettoracequeue.add("Rabbit" + rabbitno);
    rabbitno++; 
    yettoracequeue.add("Rabbit" + rabbitno);
    rabbitno++; 
    yettoracequeue.add("Rabbit" + rabbitno);
    rabbitno++; 
    yettoracequeue.add("Rabbit" + rabbitno);
    rabbitno++; 
    yettoracequeue.add("Rabbit" + rabbitno);
    rabbitno++; 
    yettoracequeue.add("Rabbit" + rabbitno);

    System.out.println(nextline + "Items in the queue" + yettoracequeue + nextline);

    //Find first item in queue
    System.out.println(nextline + "First item in queue is " + yettoracequeue.element());

    //Assign First item in queue to racer
    String winner = yettoracequeue.element();

  }
}

You need a typecast:

    winner = (String) yettoracequeue.element();

Explanation: the way you have declared the yettoracequeue variable, what you have is a Queue of objects; that is, a queue that could contain any kind of object. You have added String objects to the queue, but you could have put any type of object into it.

So when you call yettoracequeue.element() , the compiler only knows that the object is going to be an instance of java.lang.Object or some subclass. (That is because every object is an instance of java.lang.Object or some subclass!)

But when you assign the value to winner , the system needs to know that the object you assign is really a String . (If it was something else, then String specific operations on it would not work.)

So what does the type-cast do?

Well (String) yettoracequeue.element() does a runtime type check. It checks that the object returned by the method call is really a String:

  • If the runtime type check succeeds, then it treats the type of the expression as String ... and the assignment is valid.

  • If the runtime type check fails, then the runtime system throws a ClassCastException ... and your program will typically fail.


Actually, there is a better way to solve the problem in this particular case. The Queue type is actually a generic type; ie you can give it a type parameter. It looks like this:

    Queue<String> yettoracequeue = new LinkedList<>();

I have now declared that yettoracequeue is a queue that contains String objects. If I do that then:

  • When I try to add (say) an Integer object to the queue, I will get a compilation error.

  • When I call yettoracequeue.element() the compiler will know that the queue only contains String objects, and won't insist on the type cast when I assign the result to a String variable.

The problem in your code is that the queue is a raw type as opposed to a queue of String . To avoid the type error, you have to provide the generic type argument:

Queue<String> yettoracequeue = new LinkedList<String>();
   // ^^^^^^

You can read more about Generic Types in the Tutorial by Oracle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM