Java中的String 和char[] ,int和Integer
来源:互联网 发布:詹姆斯生涯数据排名 编辑:程序博客网 时间:2024/05/27 21:47
public class StringVSChar { public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; char[] ch = { 'a', 'b', 'c' }; String s3 = new String(ch); String s4=new String("abc"); char[] c = s1.toCharArray();//true if (s1.equals(s2)) { System.out.println(true); } else { System.out.println(false); } //false if (s1.equals(ch)) { System.out.println(true); } else { System.out.println(false); } //false if (c.equals(ch)) { System.out.println(true); } else { System.out.println(false); } //true if (s1.equals(s3)) { System.out.println(true); } else { System.out.println(false); } //trueif (s1.equals(s4)) { System.out.println(true); }else { System.out.println(false); }//trueif (s1==s2) { System.out.println(true); } else { System.out.println(false); }//false if (s1==s4) { System.out.println(true); } else { System.out.println(false); } //false if (s1==s3) { System.out.println(true); } else { System.out.println(false); } } }/***********************************************************/
从结果可以看出要弄清几点:
1.String类型可以和char[]进行相互转换,但两种是不同的类型;
2.弄清楚创建的对象在内存中是处于栈还是堆;
3.String中重写了equal(),比较的是两个对象的内容是否一样,如果不重写,和==一样都是比较的对象的引用,new出来的对象都是放在堆中的。/***********************************************************/
public class IntVSInteger { public static void main(String[] args) { Integer i1 = -18; Integer i2 = -18; Integer i3=new Integer(-18); Integer i4=new Integer(-18); //true if (i1 == i2) { System.out.println(true); } else { System.out.println(false); } //true if (i1.equals(i2)) { System.out.println(true); } else { System.out.println(false); } //false if (i3==i4) { System.out.println(true); } else { System.out.println(false); } //trueif (i3.equals(i4)) { System.out.println(true); } else { System.out.println(false); } } }/***********************************************************/
经调试,在Java中,integer的-128~127是系统预留在栈中的共享数据,这个范围之外的赋值,用==比较就是false.
参考:http://meohao.iteye.com/blog/777747
阅读全文
0 0
- Java中的String 和char[] ,int和Integer
- Java String,int,Integer,char 类型转换
- Java中的String和Integer
- Java中的int和Integer的区别
- java中的Integer和int比较
- Java Integer和int
- java中int和integer,String,StringBuffer,StringBuilder的区别
- JAVA中的类型和对象(Integer和Int)
- java中String,int,Integer,char、double类型转换
- java中String,int,Integer,char、double类型转换
- java中String,int,Integer,char、double类型转换
- Java中int和Integer
- Java int 和 Integer 区别
- Java--String 和 char[]
- java char 和string
- java中int和integer比较,integer和integer比较
- java Integer.ValueOf(int)和new Integer(int)性能比较
- int和string以及char *和string的互相转换
- Android--JSON解析技术
- Quorum
- 6. Cordova多平台运行
- sqoop 安装
- Spring Boot
- Java中的String 和char[] ,int和Integer
- “有权查看使用情况的应用”
- 这是我尝试发表的博文
- 如何消除margin-top对父元素的影响
- winows 快速打开软件
- 解决 Toast 的重复显示问题
- 如何在Predix上建立一个简单的Node.js 程序
- there are no staged files
- 000026:使用赋值运算符为变量赋值