今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累也值!

废话不说了,我就说一下今天遇到的面试题,感到自己有点危机感,之后下来我在这里总结总结:

String 与Integer 相关的问题;

第一题:

//Integer n1 = new Integer(11);
//Integer n2 = new Integer(11); //Integer n3 = 11;
//Integer n4 = 11;
//System.out.println(n1 == n2); false
//System.out.println(n1 != n2); true //System.out.println(n3 == n4); true
//System.out.println(n3 != n4); false //String str1 = "1234";
//String str2 = new String("1234"); //System.out.println(str1 == str2); false
//System.out.println(str1.equals(str2)); true //System.out.println("12" + "34" == "1234"); true

第二题:

public class Test1{
String str = new String("good");
char[] ch = {'a','b','c'};
public static void main(String[] args){
Test1 test1 = new Test1();
test1.changer(test1.str,test1.ch);
System.out.print(test1.str+" and ");
System.out.print(test1.ch);
}
public void changer(String str,char ch[]){
str = "test ok";
ch[0] = 'g';
}
}

这个我需要把print给贴出来:

public void print(char s[]) {
write(s);
} private void write(char buf[]) {
try {
synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush) {
for (int i = 0; i < buf.length; i++)
if (buf[i] == '\n')
out.flush();
}
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}

输出为 good and gbc(而不是地址)

总结:

Integer:

Integer.valueOf(11) 比直接 new Integer(11) 性能更好,因为valueOf直接从缓存中返回Integer对象,只有数字在>=-128 && <=127这个区域会直接读缓存,否则调用new Integer新创建一个对象.所以说valueOf性能更好.因为它一开始就被缓存了.

 public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
private static class IntegerCache {
private IntegerCache(){} static final Integer cache[] = new Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}

String

1、String类是final的,不可被继承。

2、String类是的本质是字符数组char[], 并且其值不可改变。

3、String类对象有个特殊的创建的方式,就是直接指定比如String x = “abc”,”abc”就表示一个字符串对象。而x是”abc”对象的地址,也叫做”abc”对象的引用。

4、String对象可以通过“+”串联。串联后会生成新的字符串。

5、Java运行时会维护一个String Pool(String池),JavaDoc翻译很模糊“字符串缓冲区”。String池用来存放运行时中产生的各种字符串,并且池中的字符串的内容不重复。而一般对象不存在这个缓冲池,并且创建的对象仅仅存在于方法的堆栈区。

6、创建字符串的方式很多,归纳起来有三类:

其一,使用new关键字创建字符串,比如String s1 = new String(“abc”);

其二,直接指定。比如String s2 = “abc”;

其三,使用串联生成新的字符串。比如String s3 = “ab” + “c”;

String 是如何创建的

原理1:

当使用任何方式来创建一个字符串对象s=X时,Java运行时(运行中JVM)会拿着这个X在String池中找是否存在内容相同的字符串对象,如果不存在,则在池中创建一个字符串s,否则,不在池中添加。

原理2:

Java中,只要使用new关键字来创建对象,则一定会(在堆区或栈区)创建一个新的对象。

String str1 = “abc”;

步骤:

1) 栈中开辟一块空间存放引用str1;

2) String池中开辟一块空间,存放String常量”abc”;

3) 引用str1指向池中String常量”abc”;

4) str1所指代的地址即常量”abc”所在地址

String str2 = new String(“abc”);

步骤:

1) 栈中开辟一块空间存放引用str2;

2) 堆中开辟一块空间存放一个新建的String对象”abc”;

3) 引用str2指向堆中的新建的String对象”abc”;

4) str2所指代的对象地址为堆中地址,而常量”abc”地址在池中

4.”abc”.equals(str)和str.equals(”abc”)的区别

“abc”.equals(str)避免空指针异常。

如果str为空,str.equals(”abc”)就会报空指针异常。

所以在字符串比较的时候一定要用”abc”.equals(str)。

今天就总结到““后续有更多精彩!

String与Integer问题的更多相关文章

  1. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

  4. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  5. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  6. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  7. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  8. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  9. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

随机推荐

  1. 字体图标库 IcoMoon IconFont Font Awesome的使用

    在项目开发的过程中,我们会经常用到一些图标.但是我们在使用这些图标时,往往会遇到失真的情况,而且图片数量很多的话,页面加载就越慢.所以,我们可以使用字体图标的方式来显示图标,字体图标任意放大缩小不会失 ...

  2. 树莓派 连接 JY901(MPU9250) python 代码

    先说BUG,最近要做项目需要树莓派和陀螺仪,资金充足的话肯定是买一个硬件卡尔曼滤波的传感器类似JY901模块,资金不足的就买MPU6050. 网上关于MPU6050在树莓派上的代码还能用,关于JY90 ...

  3. web service 上传file说明

    之前做过一个接口,PI发布WEB SERVICE给对方调,传附件到SAP... 接口中包含文件名称,文件类型,文件流... 1.SOAPUI新建项目: 文件内容的地方会自动带上一个ID,这个ID对应文 ...

  4. 102. Binary Tree Level Order Traversal二叉树层序遍历

    网址:https://leetcode.com/problems/binary-tree-level-order-traversal/ 参考:https://www.cnblogs.com/grand ...

  5. INTRODUCE OF REPRESENTATIVE CPG-CONTROLLED ROBOTS

    仿生机器人研究是非常有意思的领域,它不仅从自然界中获取灵感,它也为一些生物的研究提供一个手段和基础,正如下图中所展示的.不论人工智能发展的如何迅速,机械结构始终做为着一个承载的平台,其重要性不可忽略. ...

  6. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  7. Cognos集成至portal平台运行报表时只出“#”

    1. 问题描述 报表集成到平台后,运行报表过程中,当多次运行后,页面只显示“#” 2. 问题分析 这是因为浏览器筛选器限制问题 3. 解决方案 在IE浏览器设置中,Internet选项-安全-自定义级 ...

  8. Java获取本地IP地址和主机名

    方式一:通过java.net.InetAddress类获取 public void test1() { try { InetAddress addr = InetAddress.getLocalHos ...

  9. dreamware2018破解

    dreamware2018破解     C:\Program Files\Adobe\Adobe Dreamweaver CC 2018      

  10. jquery 之ajax cache

    最近在做一个企业通讯录的功能,功能很简单,我的本意是做数据存储并且做同步更新,时间原因以及服务端的同事更倾向于简单点的方式,呵呵,于是我们就采取了实时查询的方式. 由于get请求有点多,由于页面的生命 ...