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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lwj123   /  2015-2-6 09:49  /  3910 人查看  /  67 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

try catch finally 语句中如果try中有return 而finally中也有的话,try中的是执行不到的。
回复 使用道具 举报
水水更健康!~别蜂窝啊.
回复 使用道具 举报
wenyu 中级黑马 2015-2-10 10:03:14
43#
邓士林 发表于 2015-2-6 11:05
还是说说==和equals,因为今天我才明白。
【==是比较对象的时候比较内存,equals底层也是用==实现】这句话 ...

我觉得这个问题只要理解了常量池的问题就不难理解了吧。栈和堆里的东西最终还是指向常量池的,只是堆里的东西是两次创建的不同String,在堆里的的地址不同,但都是指向常量池的同一个字符串常量
回复 使用道具 举报
  1. short a = 1;
  2. a = a + 1;//报错
  3. a += 1; // 通过
复制代码

a += 1之所以通过是因为使用复合赋值运算符时编译器已自动强转

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

回复 使用道具 举报
Ansel 中级黑马 2015-2-16 01:24:52
45#
菜鸟一个,完全看不懂
回复 使用道具 举报
  1. package pack;
  2. class Demo implements Runnable{
  3.         public void run(){
  4.                 for(int x=0; x<60; x++){
  5.                         System.out.println(Thread.currentThread().getName()+" :"+x);
  6.                         // Thread.yield();
  7.                 }
  8.         }
  9. }
  10. class joinDemo{
  11.         public static void main(String[] args)throws InterruptedException{
  12.                 Demo d=new Demo();
  13.                 Thread t1=new Thread(d);
  14.                 Thread t2=new Thread(d);
  15.                 t1.start();
  16.                 t1.join();
  17.                 t2.start();
  18.         }
  19. }
复制代码

join:当t2线程执行到了t1线程的join方法时,t2就会等待,等t1线程都执行完,t2才会执行。
join可以用来临时加入线程。
发小了个小现象。就是要插入的线程必须放在其他线程的上面并且后面要跟的是join。这样才能正确的打印完你要插入的线程。
不知道我说的对不对。不过挺有意思的。
回复 使用道具 举报
最近发现的JDK7的新功能:
  1. // 可以在数值字面值中使用下划线,如:
  2. int i = 1_000_000;
  3. double d = 9.000_001;
  4. // 以上都是合法的代码,编译器会自行忽略掉下划线
  5. //还有,Java终于支持字面值的二进制表示了:
  6. int bi = 0b101;// 我都想不通,这么简单实用的功能,C语言不支持,JAVA到了第7版才支持
复制代码

点评

版主这么晚还在论坛发技术分,实在让人感动啊  发表于 2015-2-17 01:26

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

回复 使用道具 举报
  1. // private方法都隐式地被指定为final的,但是当你覆盖一个private方法时,却能顺利通过编译:
  2.     class Father {
  3.         void Method1() {}
  4.         final void Method2() {}
  5.         private void Method3() {} // 相当于 private final void method3() {}
  6.     }
  7.    
  8.     class Son extends Father {
  9.         void Method1() {} // 通过。正常覆盖
  10.         void Method2() {} // 报错。无法覆盖被final修饰的方法
  11.         void Method3() {} // 通过。
  12.         /*  其实,父类中的private方法对子类是不可见的,
  13.             子类中的Method3()并没有覆盖父类中的方法而是一个新的方法,
  14.             只不过恰好与父类中的private方法同名。
  15.         
  16.             之前我还不太理解为什么在覆盖时,子类方法的权限不能比父类方法的小,
  17.             理解上述例子之后才知道原因所在。
  18.         */
  19.     }
复制代码
回复 使用道具 举报
  1. double x=10
  2. while(x!=9.00)
  3. {
  4. x-=0.01
  5. }
复制代码

死循环,浮点数比较,精确到具体的二进制位,各个二进制位相等才算相等,浮点数表示的是近似值,不能用于比较

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1. for(int x=10;x>1;;)
  2. int x=15
复制代码

正确
  1. for(int x=10;x>1;;)
  2. x=15
复制代码

错误,循环中的变量在括号外无效
回复 使用道具 举报
Integer m = 128                                        Integer m = 127;
Integer n = 128;                                        Integer n = 127;
m==n?false,两个对象                                m==n?true,指向了同一个
当数值在byte范围内时,对于新特性,如果该数值已经存在,则不会再开辟空间。

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

回复 使用道具 举报
terator对集合类中的任何一个实现类,都可以返回一个Iterator对象。就和循环一样,好处是可以适用于任何一个类,而且比直接用index访问快一点。
回复 使用道具 举报
Shey 中级黑马 2015-3-16 10:48:55
53#
我就来说说基本类型包装类的valueOf吧,下面用Integer为例:

  1.                 /*1*///true
  2.                 Integer i1 = Integer.valueOf(1);
  3.                 Integer i2 = Integer.valueOf(1);
  4.                 System.out.println(i1==i2);
  5.                
  6.                 /*2*///false
  7.                 i1 = new Integer(1);
  8.                 i2 = new Integer(1);
  9.                 System.out.println(i1==i2);
  10.                
  11.                 /*3*///false
  12.                 i1 = new Integer(1);
  13.                 i2 = Integer.valueOf(1);
  14.                 System.out.println(i1==i2);
  15.                
  16.                 //了解过自动拆箱和装箱的同学都知道 Integer i = 1 其实就等同于 Integer i = Integer.valueOf(1);所以只有这三种
复制代码
其实我是查阅了源码才有根本上的理解的,如下:
  1. public static Integer valueOf(int i) {
  2.         if (i >= IntegerCache.low && i <= IntegerCache.high)
  3.             return IntegerCache.cache[i + (-IntegerCache.low)];
  4.         return new Integer(i);
  5.     }
复制代码

上面是Integer类里面valueOf的源码,Interger类中有一个内部内为IntegerCache意思就是缓存嘛,我们先不说它源码,只看cache;
  1. private static class IntegerCache {
  2.         static final int low = -128;
  3.         static final int high;
  4.         static final Integer cache[];
  5.         ......
  6.     }
复制代码

代码的意思是显而易见的cache是Integer类型的数组,cache本身就是缓存的意思,所以valueOf就是在指定范围内返回cache缓存中的值,超出范围则new一个,若当1存在于缓存cache中,上文的3个结果也就明了了IntegerCache.low==-128我们都知道了,下面我去深究下IntegerCache.high到底有多大,源码附上:
  1. static {
  2.     // high value may be configured by property
  3.     int h = 127;-------------------------------------------------------------------1
  4.     String integerCacheHighPropValue =
  5.         sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  6.     if (integerCacheHighPropValue != null) {
  7.         try {
  8.             int i = parseInt(integerCacheHighPropValue);
  9.             i = Math.max(i, 127);
  10.             // Maximum array size is Integer.MAX_VALUE
  11.             h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  12.         } catch( NumberFormatException nfe) {
  13.             // If the property cannot be parsed into an int, ignore it.
  14.         }
  15.     }
  16.     high = h;-----------------------------------------------------------------------2

  17.     cache = new Integer[(high - low) + 1];
  18.     int j = low;
  19.     for(int k = 0; k < cache.length; k++)
  20.         cache[k] = new Integer(j++);

  21.     // range [-128, 127] must be interned (JLS7 5.1.7)
  22.     assert IntegerCache.high >= 127;
  23. }
复制代码

这是IntegerCache中的静态代码块,在类加载的时候执行一次,我们直接看---1和---2处,中间代码不管,整个结构就只是给high赋值,循环建立从low到high大小的cache缓存数组,很简单吧,下面再看看1---2中间部分,我用注释的方式写出:
  1. String integerCacheHighPropValue =
  2.     sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");//读取javaInteger缓存最大值配置,默认127
  3. if (integerCacheHighPropValue != null) {
  4.     try {
  5.         int i = parseInt(integerCacheHighPropValue);
  6.         i = Math.max(i, 127); //若自行配制的最大值小于127,就取127
  7.         // Maximum array size is Integer.MAX_VALUE
  8.         h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  9.         //这是Integer前面定义的常量public static final int MAX_VALUE = 0x7fffffff;
  10.         //若自行配制的最大值上限还是有限制的最大为:Integer.MAX_VALUE - (-low) -1
  11.     } catch( NumberFormatException nfe) {
  12.         // If the property cannot be parsed into an int, ignore it.
  13.     }
  14. }
  15. assert IntegerCache.high >= 127; //断言函数,自行百度
复制代码
最后:-D可以设置java.lang.Integer.IntegerCache.high
谢阅




评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

回复 使用道具 举报
感觉都好厉害,我这个新手0基础的怎搞,何时才能赶上
回复 使用道具 举报
java规定私有的东西不能被外界访问,要真想访问也有一道后门那就是通过暴力反射强暴它一把!
就要调用setAccessable方法,将其改为true。
回复 使用道具 举报
逗号操作符;
  1. public class Test {
  2.        
  3.         public void method() {
  4.                 for(int i=0, j=i+2; i<10; i++, j=j*2) {
  5.                         System.out.println("i = " + i + " j = " + j);
  6.                 }
  7.         }
  8. }
复制代码

for循环中,“,”后面的语句会独立执行。
     

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

回复 使用道具 举报
还没看到这里,==再说。
回复 使用道具 举报
System.out.println(char[] chs)输出结果为字符数组中的所有字符,而不是数组的地址。通过源码,发现,其底层是:OutputStream中的write(int a),所以结果是所有字符。
回复 使用道具 举报
123
您需要登录后才可以回帖 登录 | 加入黑马