JAVA - 小知识

JAVA - 小知识


1. int和Integer的区别

  1. Integer是int的包装类,int则是java的一种基本数据类型
  2. Integer变量必须实例化后才能使用,而int变量不需要
  3. Integer实际是对象的引用,当new一个Integer时,实际上是生成一个指针指向此对象;而int则是直接存储数据值
  4. Integer的默认值是null,int的默认值是0
  5. 由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。
    Integer i = new Integer(100);
    Integer j = new Integer(100);
    System.out.print(i == j); //false
  1. Integer是int的包装类,int则是java的一种基本数据类型
  2. Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)
  3. 非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)
    Integer i = new Integer(100);
    Integer j = 100;
    System.out.print(i == j); //false


2. String类型

  1. 数字—>转换成—->String: String a = “”+num;
  2. String 类型 不能用str[i] , 而是用 str.charAt(i)
  3. haystack.substring(i,i+l2).equals(needle) //取子串并判断是否等于needle
  4. return new StringBuffer(s).reverse().toString(); //反向转换字符串
    1. StringBuffer 对该字符串本身进行操作在内存上优于String,是线程安全的。
    2. StringBuffer 和String之间的转换:

      String s = “abc”; StringBuffer sb1 = new StringBuffer(“123”); StringBuffer sb2 = new StringBuffer(s); //String转换为StringBuffer String s1 = sb1.toString(); //StringBuffer转换为String

3. 进制转换

  1. 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();

4.栈

  1. 声明:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值