2013-05-31 55 views
5

返回元组我写了一个Java类:从Java方法

public class Tuple<X, Y> { 
    public final X x; 
    public final Y y; 
    public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
    } 
} 

但是当我创建这样一个功能:

public Tuple<boolean, String> getResult() 
{ 
    try { 
     if(something.equals(something2)) 
      return new Tuple(true, null); 
    } 
    catch (Exception e){ 
     return new Tuple(false, e.getMessage()); 
} 

不过,我得到以下编译错误:

unexpected type 
    required: reference 
    found: boolean 

我能做什么?

+1

_“required:reference; found:boolean”_这是告诉你它发现'boolean'类型,它期望一个“引用”类型,即某种对象。 –

回答

10

泛型不适用于原始类型。使用Boolean而不是boolean

public Tuple<Boolean, String> getResult() { 
    //your code goes here... 
} 
+0

谢谢你的工作。 – a1204773

+0

@Loclip不用客气。 –

0

这就是为什么像Boolen,Integer,Long等类引入。在java中有几个apis需要一个对象而不是一个原语。