A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

1、面试题:
  第一题:
  byte b1=3,b2=4,b;
  b=b1+b2;
  b=3+4;
  哪句是编译失败的呢?为什么呢?

  第二题:
  byte  by = 130;有没有问题?有问题如何解决?结果是多少呢?

  第三题:
  byte b = 10;
  b++;
  b = b + 1;
  哪句是编译失败的呢?为什么呢?

2、short s = 1; s = s + 1;有没有问题?如果有怎么解决?
   short s = 1; s += 1;有没有问题?如果有怎么解决?

3、需求:
  int a = 10;
  int b = 20;
  请用代码实现交换两个变量,
  即结果为: a = 20;  b = 10;  (3种)


答案:  int a = 1;
  int b = 2;
  int temp = a;
      a = b;
      b = temp;
  System.out.println("a= "+a+", b= "+b);
  System.out.println("--------------------------------");
  int x = 1;
  int y = 2;
  x = x + y;
  y = x - y;
  x = x - y;
  System.out.println("x= "+x+", y= "+y);
  System.out.println("--------------------------------");
  int i = 1;
  int j = 2;
  i = i ^ j;
  j = i ^ j;
  i = i ^ j;
  System.out.println("i= "+i+", j= "+j);
  System.out.println("--------------------------------");
  
class Demo2_Operator {
public static void main(String[] args) {
  /*
  * 位异或运算符的特点

  * ^的特点:一个数据对另一个数据位异或两次,该数本身不变。
  */

  //System.out.println(5 ^ 10 ^ 10);
  //System.out.println(5 ^ 10 ^ 5);

  /*
  * 请自己实现两个整数变量的交换(不需要定义第三方变量)
  * 注意:以后讲课的过程中,我没有明确指定数据的类型,默认int类型。
  */

  int x = 10;
  int y = 5;

  //需要第三方变量,开发推荐用这种
  /*int temp;
  temp = x;
  x = y;
  y = temp;*/

  //不需要定义第三方变量,有弊端,有可能会超出int的取值范围
  /*x = x + y;    //10 + 5 = 15
  y = x - y;    //15 - 5 = 10
  x = x - y;    //15 - 10 = 5*/

  //不需要第三方变量,通过^来做
  x = x ^ y;    // 10 ^ 5
  y = x ^ y;    // 10 ^ 5 ^ 5 y = 10
  x = x ^ y;    // 10 ^ 5 ^ 10  x = 5

  System.out.println("x = " + x + ",y = " + y);
}
}

4、最有效率的算出2 * 8的结果
class Demo3_Operator {
public static void main(String[] args) {
  /*
  *  <<:左移 左边最高位丢弃,右边补齐0
  *  >>:右移 最高位是0,左边补齐0;最高为是1,左边补齐1
  *  >>>:无符号右移 无论最高位是0还是1,左边补齐0
  *  最有效率的算出2 * 8的结果
  */

  //左移,向左移动几位就是乘以2的几次幂
  //System.out.println(12 << 1);  //24
  //System.out.println(12 << 2);  //48

  /*
  00000000 00000000 00000000 00001100  12的补码
  (0)0000000 00000000 00000000 000011000  24的补码
(00)000000 00000000 00000000 0000110000  48的补码
  */

  //右移,向右移动几位就是除以2的几次幂
  //System.out.println(12 >> 1);
  //System.out.println(12 >> 2);

  /*
  00000000 00000000 00000000 00001100  12的补码
  000000000 00000000 00000000 0000110(0) 6
  0000000000 00000000 00000000 000011(00) 3
  */

  //最有效率的算出2 * 8的结果
  System.out.println(2 << 3);
}
}

5、
* byte可以作为switch的表达式吗?
* long可以作为switch的表达式吗?
* String可以作为switch的表达式吗?

27 个回复

正序浏览
Java_EE 来自手机 中级黑马 2015-9-12 07:52:13
28#
谢谢楼主的分享
回复 使用道具 举报
给力,赞      
回复 使用道具 举报
ZZBY 中级黑马 2015-9-12 00:45:55
26#
是进入就业班的考试题吗
回复 使用道具 举报
本帖最后由 窗外的雪儿飞 于 2015-9-12 11:32 编辑

Q: 用迭代器遍历集合的时候,用集合修改集合有没有问题?如果有,怎么解决?

A:有,会出现ConcurrentModificationException:并发修改异常。
      出现的原因:迭代器是依赖于集合而存在的。我们在通过迭代器迭代的过程中,用集合往集合中添加了元素,而并没有重新获取迭代器,所以会报错。
      解决方案:
                     A:用集合本身遍历集合,用集合去修改集合。 集合实现,元素添加到末尾。
                     B:用迭代器迭代,用迭代器修改元素。 迭代器实现,遍历到哪里,添加到哪里。
     代码体现:(PS:只是简单的代码而已,复杂的不演示)
public class ListIteratorDemo {
     public static void main(String[] args) {
          List list = new ArrayList();
          list.add("a");
          list.add("b");
          list.add("c");
  //1、用集合实现
          for (int x = 0; x < list.size(); x++) {
                  String s = (String) list.get(x);
                        if ("b".equals(s)) {
                               list.add("d");
                         }
            }
  //2、 用迭代器实现
           ListIterator lit = list.listIterator();
           while (lit.hasNext()) {
                  String s = (String) lit.next();
                  if (list.contains("b")) {
                         lit.add("d");
                   }
            }
            System.out.println("list:" + list);
      }
}
回复 使用道具 举报
本帖最后由 窗外的雪儿飞 于 2015-9-12 11:32 编辑

集合类:请用LinkedList模拟栈数据结构的集合,并测试。
思路:首先定义自己的栈集合,底层用LinkedList类实现。
1、自定义栈集合
import java.util.LinkedList;
public class MyStack {
          private LinkedList link;
          public MyStack() {
                   link = new LinkedList();
          }

          public void add(Object obj) {
                  link.addFirst(obj);
           }

          public Object get() {
                 return link.removeFirst();
           }

          public boolean isEmpty() {
                  return link.isEmpty();
           }
}

2、测试
public class MyStackDemo {
        public static void main(String[] args) {
               MyStack my = new MyStack();
               my.add("hello");
               my.add("world");
               my.add("java");
               while (!my.isEmpty()) {
                        System.out.println(my.get());
                }
         }
}
回复 使用道具 举报
赞一个!!
回复 使用道具 举报
谢谢LZ,这样学习就有针对性了
回复 使用道具 举报
楼主太好了是个好银
回复 使用道具 举报
mark~~~!!!
回复 使用道具 举报
看看。了解下
回复 使用道具 举报
太好啦 这些
回复 使用道具 举报
boboyuwu 来自手机 高级黑马 2015-9-8 13:55:16
17#
二个运行都正常
回复 使用道具 举报
多谢分享啊。一定认真看看。
回复 使用道具 举报
fjb0902 来自手机 中级黑马 2015-9-8 12:45:11
15#
马上面试了,收藏了
回复 使用道具 举报
好东西  学习了
回复 使用道具 举报

1.判断定义为String类型的s1和s2是否相等
                        * String s1 = "abc";
                        * String s2 = "abc";
                        * System.out.println(s1 == s2);                                        
                        * System.out.println(s1.equals(s2));                
                * 2.下面这句话在内存中创建了几个对象?
                        * String s1 = new String("abc");                       
                * 3.判断定义为String类型的s1和s2是否相等
                        * String s1 = new String("abc");                       
                        * String s2 = "abc";
                        * System.out.println(s1 == s2); ?                       
                        * System.out.println(s1.equals(s2)); ?       
                * 4.判断定义为String类型的s1和s2是否相等
                        * String s1 = "a" + "b" + "c";
                        * String s2 = "abc";
                        * System.out.println(s1 == s2); ?                       
                        * System.out.println(s1.equals(s2)); ?       
                * 5.判断定义为String类型的s1和s2是否相等
                        * String s1 = "ab";
                        * String s2 = "abc";
                        * String s3 = s1 + "c";
                        * System.out.println(s3 == s2);
                        * System.out.println(s3.equals(s2)); ?       



                结果分析:true、true   2个   false、true   true、true    false、true
                                1:equals重写了object的equals方法,比较的是字符序列。关于 == ,常量池中如果
                                没有这个字符串对象,就创建一个,如果有直接用即可,所以是两个引用指向同一个地址。
                                2:先存入常量值,因为一旦初始化就不能被改变,然后因为是new,所以在堆内存中创建新的。
                                3:同1分析,一个地址是堆内存的,一个常量池的。
                                4:Java有常量优化机制,a b c先拼接然后判断是否在常量池中存在,存在就直接用,不存在就创建新的。
                                5:变量与常量相加,字符串在底层是先通过创建StringBuffer或者StringBuild对象,将String转成该类,
                                然后通过append方法实现拼接,最后通过toString方法再转为String。
回复 使用道具 举报
楼主好人一路平安
回复 使用道具 举报
顶一下已关注
回复 使用道具 举报
京巨 来自手机 中级黑马 2015-9-8 10:13:34
10#
很好,不错
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马