String a;
Object b;
if(b instanceof String)
   a = (String)b;

解决方案 »

  1.   

    首先一个错误,String不是基础类型,而是java中的一个类!
    java的基础类型有int,float,long,char,byte,double等等
      

  2.   

    我是楼主。现从新更正。
    String a 的值是 “String” 或 “int” 。他的值是动态从数据库取得的。表示一个字段的java对应类型;
    String b 的值是从HashMap中得到后作的类型转换;
    我现在要将 b 的类型转换成 a 的值所表示的类型;
      

  3.   

    int vInt;
    String vString
    try{
    if(a.equals("String"))
      vString = (String)b;
    else if(a.equals("int"))
      vInt = Integer.parseInt((String)b);
    }catch(Exception e) {
      e.printStackTrace();
    }
      

  4.   

    package untitled1;import java.util.*;public class TestType {
        private String a = null;
        private String b = null;
        private HashMap hashMap = null;    public TestType() {
            hashMap = new HashMap();
            hashMap.put("temp1", "I am String");
            hashMap.put("temp2", "12345");
            //while (true) {
                Random rd = new Random();
                int c = rd.nextInt(2);
                if (c == 0)
                    a = "String";
                else if (c == 1)
                    a = "int";
                else
                    a = "NULL";
            //}
        }    public static void main(String[] args) {
            TestType tt = new TestType();
            if (tt.a.equals("String")) {
                tt.b = (String)tt.hashMap.get("temp1");
                String finalB = tt.b;
                System.out.println("String: "+tt.b);
            }
            else if (tt.a.equals("int")) {
                tt.b = (String)tt.hashMap.get("temp2");
                int finalB = Integer.parseInt(tt.b);
                System.out.println("int: "+finalB);
            }
            else
                System.out.println("I don't konw!");
        }
    }
      

  5.   

    对不起,请你使用Integer代替intint 和 String 永不可造型。不过,
    Integer.value(object).intValue 为 int类型,但不是对象字符串, Integer为对象,它俩也没有强行造型转换的理由(应使用这两对象的自带函数)就是说,String 能出现的地址(如表达式,参数),int永远不能出现,Integer倒是可以,
    如System.out.println(Object);
    如果Object 为字符中,打出来字符串
    如果Object 为Integer,打出来的为Integer.toString(),也就是"1", "2"等等的。
      

  6.   

    更正:
    对不起,请你使用Integer代替intint 和 String 永不可相互造型。
      

  7.   

    int c = Integer.parseInt(a);