JAVA - 小知识
integer是int的包装类,int则是java的一种基本数据类型
Integer变量必须实例化后才能使用,而int变量不需要
Integer实际是对象的引用,当new一个Integer时,实际上是生成一个指针指向此对象;而int则是直接存储数据值
Integer的默认值是null,int的默认值是0
由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。
立即学习“Java免费学习笔记(深入)”;
Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //falseinteger是int的包装类,int则是java的一种基本数据类型
Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)
非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)
Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j); //false数字—>转换成—->String: String a = “”+num;
String 类型 不能用str[i] , 而是用 str.charAt(i)
haystack.substring(i,i+l2).equals(needle) //取子串并判断是否等于needle
return new StringBuffer(s).reverse().toString(); //反向转换字符串
StringBuffer 对该字符串本身进行操作在内存上优于String,是线程安全的。
StringBuffer 和String之间的转换:
String s = “abc”;
StringBuffer sb1 = new StringBuffer(“123”);
StringBuffer sb2 = new StringBuffer(s); //String转换为StringBuffer
String s1 = sb1.toString(); //StringBuffer转换为String
java.lang.Integer这个API包中有进制转换的函数:
这3个函数都可以将十进制的整数转换成二、一六、八进制数
public static String toBinaryString(int i) // String a = Integer.toBinaryString(n)
public static String toHexString(int i) // String a = Integer.toHexString(n)
public static String toOctalString(int i) // String a = Integer.toOctalString(n)Stack st = new Stack();
声明:

相关推荐:
以上就是JAVA中会疏漏的小知识总结的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号