IBM中国研究院面试题 Byte b=new Byte("127"); System.out.println(b.toString()==b.toString());可有哪位高手能解释一下为什么会输出false呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 tostring 是不是实际在根据byte的值去new一个字符串呢所以两个的HASH值是不相等的 上帝啊,你新开个贴嘛~~JAVA的字符串是固定的,一旦生成不能更改,而,toString方法每次返回一个新String对象2次toString方法返回2个对象,==比较的是对象的(JVM)内存地址,固然不同 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代码模拟了银行业务排队的哪个。不知道合适不? 这题谁给解释一下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(); } } 2 执行顺序 System.out.println("e"); System.out.println("c"); IllegalStateException 这些题都能做到出来,看来花了几个月时间看Java编程思想没白看… toString() 方法是把byte对象转换成 String类型,也就是引用类型,而且相当于new操作,在堆中新创建了127,在这里 == 比较的是在栈中的b 对于堆中的127 指向的地址,这里是2个地址,虽然值相同,所以返回false。 如果你是System.out.println(b.toString().equals(b.toString()));这样比较,则是true 我觉得好公司肯定都会注重基础的,不过IBM中国研究院也就招个实习生啥的要看着东西吧,我来的时候就这样。这里边除了工龄比较长的是研究生以外,新来的基本都是博士生和海外人士了而且还一个劲儿的说现在招人难啊 2 执行顺序 貌似是: System.out.println("e"); IllegalStateException System.out.println("c"); 关于异常的那个题目,执行顺序应该是这样的:1.首先在method2()里面打印出e,然后抛出异常2.method1()试图捕获在method2()里面的异常,但是因为异常类型不一样,所以没有捕获住,然后method1()也抛出异常,但是method1()里面有一个finally语句,在抛出异常之前先要执行finally语句里面的代码综上所述:结果应该是ecIllegalStateException... 对的。系统处理语句是在finally之后的 Byte b=new Byte("127");System.out.println(b.toString()==b.toString()); 小弟愚见,两个字符串相比较是equals()吧 数组的移除问题? httpclient4.1.2做proxy里遇到的一个问题 java中的byte为什么会出错 byte b =0xff??? exec(String[])调用系统命令时的参数urp? 这异常是什么原因,引起,要如何解决, 我也问个采鸟问题? java的学习 Byte ---> Int 如何屏蔽鼠标左键功能。谢谢。 JBUILDER使用心得交流 开始目的地址搜索问题 很晚很冷 。。。问个简单的问题,顺便散分....
所以两个的HASH值是不相等的
上帝啊,你新开个贴嘛~~JAVA的字符串是固定的,一旦生成不能更改,而,toString方法每次返回一个新String对象
2次toString方法返回2个对象,==比较的是对象的(JVM)内存地址,固然不同
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代码模拟了银行业务排队的哪个。
不知道合适不?
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();
}
}
System.out.println("e");
System.out.println("c");
IllegalStateException
这里边除了工龄比较长的是研究生以外,新来的基本都是博士生和海外人士了而且还一个劲儿的说现在招人难啊
System.out.println("e");
IllegalStateException
System.out.println("c");
1.首先在method2()里面打印出e,然后抛出异常
2.method1()试图捕获在method2()里面的异常,但是因为异常类型不一样,所以没有捕获住,然后method1()也抛出异常,但是method1()里面有一个finally语句,在抛出异常之前先要执行finally语句里面的代码
综上所述:结果应该是
e
c
IllegalStateException...
System.out.println(b.toString()==b.toString());
小弟愚见,两个字符串相比较是equals()吧