Byte b=new Byte("127");
        System.out.println(b.toString()==b.toString());
可有哪位高手能解释一下为什么会输出false呢?

解决方案 »

  1.   

    tostring 是不是实际在根据byte的值去new一个字符串呢
    所以两个的HASH值是不相等的
      

  2.   


    上帝啊,你新开个贴嘛~~JAVA的字符串是固定的,一旦生成不能更改,而,toString方法每次返回一个新String对象
    2次toString方法返回2个对象,==比较的是对象的(JVM)内存地址,固然不同
      

  3.   


    public class BankQueueThread {

    private static List<CustomerInfo> queue= Collections.synchronizedList(new ArrayList<CustomerInfo>());
    private static List<String> vipqueue= Collections.synchronizedList(new ArrayList<String>());
    private static int num=0;
    private static int rnum=0;
    private SimpleDateFormat datef=new SimpleDateFormat("HH:mm");
    public static void main(String args[]) {
    BankQueueThread bankQueueThread=new BankQueueThread();
    AddUser adduser=bankQueueThread.new AddUser();
    Thread th= new Thread(adduser);
    th.start();

    for(int i=0;i<3;i++){
    OrderNormalQueue orn=bankQueueThread.new OrderNormalQueue();
    th= new Thread(orn);
    th.start();
    }
    OrderVIPQueue orv=bankQueueThread.new OrderVIPQueue();
    th= new Thread(orv);
    th.start();
    }
    //增加普通用户排队
    public synchronized void addUser(int nums){
    String s=""+(++num);
    CustomerInfo customerInfo=new CustomerInfo();
    customerInfo.setType("普通");
    customerInfo.setId(s);
    customerInfo.setFtime(nums);
    customerInfo.setStime(datef.format(new Date()));
    queue.add(customerInfo);
    }
    //增加VIP排队
    public synchronized void addVipUser(int nums){
    String s=""+(++num);
    CustomerInfo customerInfo=new CustomerInfo();
    customerInfo.setType("VIP");
    customerInfo.setId(s);
    customerInfo.setFtime(nums);
    customerInfo.setStime(datef.format(new Date()));
    queue.add(customerInfo);
    vipqueue.add(s);
    }
    //普通窗口假设处理业务
    public synchronized void removieUser(){
    CustomerInfo cus=null;
    if(queue.size()>0){
    rnum++;
    cus=(CustomerInfo)queue.remove(0);

        if(vipqueue.size()>0&&vipqueue.get(0).equals(cus.getId())){
       vipqueue.remove(0);
       System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
       }else{
       System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
       }
    }
    }
    //VIP窗口假设处理业务
    public synchronized void removieVipUser(){
    String d="";
    CustomerInfo cus=null;
    if(vipqueue.size()>0){
      rnum++;
      d=vipqueue.remove(0);
      for(int i=0;i<queue.size();i++){
      cus=(CustomerInfo)queue.get(i);
        if(cus.getId().equals(d)){
      queue.remove(i);
      System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
          break;
        }
      }
    }else if(queue.size()>0){
      rnum++;
      cus=(CustomerInfo)queue.remove(0);
          System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
    }

    } //用户排队
    class AddUser implements Runnable{
            Random rd=new Random();
            int sd=0;
    public void run() {
    // TODO Auto-generated method stub
    while(true){
    sd=rd.nextInt(10);
    if(sd==8){
    addVipUser(sd);
    }else{
    addUser(sd);
    }
    if(queue.size()>=100){
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    }

    }
    //普通窗口业务受理
    class OrderNormalQueue implements Runnable{
    public void run() {
    // TODO Auto-generated method stub
    while(true){

      removieUser();
      try {
    Thread.currentThread().sleep(1000);
      } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      }
    }
    }

    }
    //VIP窗口业务受理
    class OrderVIPQueue implements Runnable{ public void run() {
    // TODO Auto-generated method stub
    while(true){
    removieVipUser();
    try {
    Thread.currentThread().sleep(1000);
      } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      }
    }
    }

    }

    class CustomerInfo{

    private String stime;
    private String id;
    private String type;
    private int ftime;
    public String getStime() {
    return stime;
    }
    public void setStime(String stime) {
    this.stime = stime;
    }
    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getType() {
    return type;
    }
    public void setType(String type) {
    this.type = type;
    }
    public int getFtime() {
    return ftime;
    }
    public void setFtime(int ftime) {
    this.ftime = ftime;
    }
    }
    }
    写了一段JAVA代码模拟了银行业务排队的哪个。
    不知道合适不?
      

  4.   

    这题谁给解释一下
    2.写出下面代码的执行结果:
    package com.ibm.bmcc.test;public class T {
            public static void main(String[] args) {
            method1();
            System.out.println("f");
        }
        
        static void method1(){
            try{
                method2();
                System.out.println("a");
            }catch (NullPointerException e) {
                System.out.println("b");
            }finally{
                System.out.println("c");
            }
            System.out.println("d");
        }
        
        static void method2(){
            System.out.println("e");
            throw new IllegalStateException();
        }
        
        
        
    }
      

  5.   

    2 执行顺序
      System.out.println("e");
      System.out.println("c");
      IllegalStateException
      

  6.   

    这些题都能做到出来,看来花了几个月时间看Java编程思想没白看…
      

  7.   

    toString() 方法是把byte对象转换成 String类型,也就是引用类型,而且相当于new操作,在堆中新创建了127,在这里 == 比较的是在栈中的b 对于堆中的127 指向的地址,这里是2个地址,虽然值相同,所以返回false。  如果你是System.out.println(b.toString().equals(b.toString()));这样比较,则是true
      

  8.   

    我觉得好公司肯定都会注重基础的,不过IBM中国研究院也就招个实习生啥的要看着东西吧,我来的时候就这样。
    这里边除了工龄比较长的是研究生以外,新来的基本都是博士生和海外人士了而且还一个劲儿的说现在招人难啊
      

  9.   

    2 执行顺序 貌似是:
      System.out.println("e");
      IllegalStateException
      System.out.println("c");
      
      

  10.   

    关于异常的那个题目,执行顺序应该是这样的:
    1.首先在method2()里面打印出e,然后抛出异常
    2.method1()试图捕获在method2()里面的异常,但是因为异常类型不一样,所以没有捕获住,然后method1()也抛出异常,但是method1()里面有一个finally语句,在抛出异常之前先要执行finally语句里面的代码
    综上所述:结果应该是
    e
    c
    IllegalStateException...
      

  11.   

    对的。系统处理语句是在finally之后的
      

  12.   

    Byte b=new Byte("127");
    System.out.println(b.toString()==b.toString()); 
    小弟愚见,两个字符串相比较是equals()吧