Java面试题每日10问(12)

news2024/9/19 18:12:16

1. What is String Pool?

  • String pool is the space reserved in the heap memory that can be used to store the strings.
  • The main advantage of using the String pool is whenever we create a string literal; the JVM checks the “string constant pool” first.
  • If the string already exists in the pool, a reference to the pooled instance is returned.
  • If the string doesn’t exist in the pool, a new string instance is created and placed in the pool. Therefore, it saves the memory by avoiding the duplicacy.
    在这里插入图片描述

2. What is the meaning of immutable regarding String?

The simple meaning of immutable is unmodifiable or unchangeable.
In Java, String is immutable, i.e., once string object has been created, its value can’t be changed.

class Testimmutablestring{  
 public static void main(String args[]){  
   String s="Sachin";  
   s.concat(" Tendulkar");//concat() method appends the string at the end  
   System.out.println(s);//will print Sachin because strings are immutable objects  
 }  
}  
Sachin

two objects are created but s reference variable still refers to “Sachin” not to “Sachin Tendulkar”.
But if we explicitly assign it to the reference variable, it will refer to “Sachin Tendulkar” object.
在这里插入图片描述

3. Why String objects are immutable in Java?

  • Because Java uses the concept of the string literal.
  • Suppose there are five reference variables, all refer to one object “sachin”.
  • If one reference variable changes the value of the object, it will be affected by all the reference variables.
  • That is why string objects are immutable in java

Following are some features of String which makes String objects immutable.

  1. ClassLoader:
  • A ClassLoader in Java uses a String object as an argument. Consider, if the String object is modifiable, the value might be changed and the class that is supposed to be loaded might be different.

  • To avoid this kind of misinterpretation, String is immutable.

  1. Thread Safe:

    As the String object is immutable we don’t have to take care of the synchronization that is required while sharing an object across multiple threads.

  2. Security:

  • As we have seen in class loading, immutable String objects avoid further errors by loading the correct class.
  • This leads to making the application program more secure. Consider an example of banking software.
  • The username and password cannot be modified by any intruder because String objects are immutable.
  • This can make the application program more secure.
  1. Heap Space:
  • The immutability of String helps to minimize the usage in the heap memory.
  • When we try to declare a new String object, the JVM checks whether the value already exists in the String pool or not.
  • If it exists, the same value is assigned to the new object.
  • This feature allows Java to use the heap space efficiently.

4.How many ways can we create the string object?

1)String Literal
Java String literal is created by using double quotes.

String s="welcome";  
  • Each time you create a string literal, the JVM checks the “string constant pool” first.
  • If the string already exists in the pool, a reference to the pooled instance is returned.
  • If the string doesn’t exist in the pool, a new string instance is created and placed in the pool.
  • String objects are stored in a special memory area known as the string constant pool
    2) By new keyword
String s=new String("Welcome");//creates two objects and one reference variable  
  • JVM will create a new string object in normal (non-pool) heap memory, and the literal “Welcome” will be placed in the constant string pool.
  • The variable s will refer to the object in a heap (non-pool).

5. How many objects will be created in the following code?

String s1="Welcome";  
String s2="Welcome";  
String s3="Welcome";  

Only one object will be created using the above code because strings in Java are immutable.

🔔Note:

The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
在这里插入图片描述
CharSequence Interface
The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by using these three classes.
在这里插入图片描述

6. What is String in Java?

  • Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters.
  • The java.lang.String class is used to create a string object.

7. Why java uses the concept of the string literal?

To make Java more memory efficient (because no new objects are created if it exists already in the string constant pool).

8. How many objects will be created in the following code?

String s = new String("Welcome");  

Two objects, one in string constant pool and other in non-pool(heap).

9.What are the differences between String and StringBuffer?

StringStringBuffer
The String class is immutable.The StringBuffer class is mutable.
The String is slow and consumes more memory when you concat too many strings because every time it creates a new instance.The StringBuffer is fast and consumes less memory when you cancat strings.
The String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.The StringBuffer class doesn’t override the equals() method of Object class.

10.What are the differences between StringBuffer and StringBuilder?

StringBufferStringBuilder
StringBuffer is synchronized, i.e., thread safe. It means two threads can’t call the methods of StringBuffer simultaneously.StringBuilder is non-synchronized,i.e., not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
StringBuffer is less efficient than StringBuilder.StringBuilder is more efficient than StringBuffer.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/165816.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

速度为单GPU1.6倍,kaggle双GPU(ddp模式)加速pytorch攻略

accelerate 是huggingface开源的一个方便将pytorch模型迁移到 GPU/multi-GPUs/TPU/fp16 模式下训练的小巧工具。和标准的 pytorch 方法相比,使用accelerate 进行多GPU DDP模式/TPU/fp16 训练你的模型变得非常简单(只需要在标准的pytorch训练代码中改动不几行代码就可…

linux基功系列之man帮助命令实战

文章目录前言一、man命令介绍二、常用参数2.1 语法2.2 常用参数2.3 man页面的操作命令man命令使用案例1. 直接查看手册2. -aw 参数找到可以被查询的章节2.3 一次性查阅所有章节2.4 搜索手册页2.5 -L 设置查询语言总结前言 linux系统中的命令数量有上千的,即使是常用…

前端——周总结系列二

1 JS数组排序sort()方法 不传参数排序,默认根据Unicode排序 附录 传参数,使用比较函数,自己定义比较规则 简单数组排序 // 升序 function ascSort(a, b) {return a - b; } // 降序 function ascSort(a, b) {return b - a; }数组对象排序…

算法leetcode|31. 下一个排列(rust重拳出击)

文章目录31. 下一个排列:样例 1:样例 2:样例 3:提示:分析:题解:rustgoccpythonjava31. 下一个排列: 整数数组的一个 排列 就是将其所有成员以序列或线性顺序排列。 例如&#xff0…

ROS2机器人编程简述humble-第二章-First Steps with ROS2 .1

ROS2机器人编程简述新书推荐-A Concise Introduction to Robot Programming with ROS2学习笔记流水账-推荐阅读原书。第二章主要就是一些ROS的基本概念,其实ROS1和ROS2的基本概念很多都是类似的。ROS2机器人个人教程博客汇总(2021共6套)如何更…

Linux chgrp 命令

Linux chgrp(英文全拼:change group)命令用于变更文件或目录的所属群组。与 chown 命令不同,chgrp 允许普通用户改变文件所属的组,只要该用户是该组的一员。在 UNIX 系统家族里,文件或目录权限的掌控以拥有…

(一)Jenkins部署、基础配置

目录 1、前言 1.1、Jenkins是什么 1.2、jenkins有什么用 2、 Jenkins安装 2.1、jdk安装 2.2、安装Jenkins 3、Jenkins配置 3.1、解锁Jenkins 3.2、插件安装 3.3、创建管理员 3.4、实例配置 4、汉化 4.1、下载Locale插件 4.2、设置为中文 5、设置中文失效解决步骤 1…

U-Boot 之零 源码文件、启动阶段(TPL、SPL)、FALCON、设备树

最近,工作重心要从裸机开发转移到嵌入式 Linux 系统开发,在之前的博文 Linux 之八 完整嵌入式 Linux 环境、(交叉)编译工具链、CPU 体系架构、嵌入式系统构建工具 中详细介绍了嵌入式 Linux 环境,接下来就是重点学习一…

【Spring6源码・AOP】代理对象的创建

前三篇Spring IOC的源码解析与这一章的AOP是紧密相连的: 【Spring6源码・IOC】BeanDefinition的加载 【Spring6源码・IOC】Bean的实例化 【Spring6源码・IOC】Bean的初始化 - 终结篇 首先介绍我们本章的demo: 一个接口,一个实现&#xf…

【论文速递】ECCV2022 - 开销聚合与四维卷积Swin Transformer_小样本分割

【论文速递】ECCV2022 - 开销聚合与四维卷积Swin Transformer_小样本分割 【论文原文】:Cost Aggregation with 4D Convolutional Swin Transformer for Few-Shot Segmentation 获取地址:https://arxiv.org/pdf/2207.10866.pdf博主关键词: …

紧聚焦涡旋光束app设计-VVB2.0

紧聚焦涡旋光束app设计-VVB2.0前言界面预览功能演示写在最后前言 时隔几个月,花了点时间,将之前用matlab设计的app紧聚焦涡旋光束matlab gui设计进行一次修改,这次发布2.0版本,本次修改的范围主要是将原来的界面进行重做&#xf…

软件设计师中级复习小总结

软件设计师中级复习小总结 计算机与体系结构 K 1024 k 1000 B 字节 b 位 1字节 8位 8bit(位)1Byte(字节) 1024Byte(字节)1KB KB,MB,GB之间的换算关系是:1024KB1MB,1024MB1GB,1024GB1TB K,M&#x…

DevOps 实战概述

一、背景越来越多的团队使用DevOps,个人觉得原因有二,其一市场需求,从瀑布到敏捷的过程能看出市场就是需要团队响应快,小步快跑,风险低效率高,但是敏捷只解决了开发团队的问题并没有解决运维团队的问题&…

16、Javaweb_ajax的JSjQuery实现方式JSON_Java对象互转用户校验案例

AJAX: 1. 概念: ASynchronous JavaScript And XML 异步的JavaScript 和 XML 1. 异步和同步:客户端和服务器端相互通信的基础上 * 客户端必须等待服务器端的响应。在等待的期间客户端不能做其他操作。 * 客户端不需要…

[LeetCode周赛复盘] 第 328 场周赛20230115

[LeetCode周赛复盘] 第 328 场周赛20230115 一、本周周赛总结二、 [Easy] 6291. 数组元素和与数字和的绝对差1. 题目描述2. 思路分析3. 代码实现三、[Medium] 6292. 子矩阵元素加 11. 题目描述2. 思路分析3. 代码实现四、[Medium] 6293. 统计好子数组的数目1. 题目描述2. 思路分…

文献阅读总结--合成生物学工程促进大肠杆菌中莽草酸的高水平积累

题目:Systems engineering of Escherichia coli for high-level shikimate production (ME 2022) 0 前言 本版块内容为记录阅读的文献内容总结经典方法手段。本文内容来自相关文献,在文末做来源进行详细说明对文献中内容不做真实性评价。 1 具体内容 …

标准化和归一化概念澄清与梳理

标准化和归一化是特征缩放(feature scalingscaling)的主要手段,其核心原理可以简单地理解为:让所有元素先减去同一个数,然后再除以另一个数,在数轴上的效果就是:先将数据集整体平移到有某个位置,然后按比例…

【C进阶】动态内存管理

家人们欢迎来到小姜的世界&#xff0c;<<点此>>传送门 这里有详细的关于C/C/Linux等的解析课程&#xff0c;家人们赶紧冲鸭&#xff01;&#xff01;&#xff01; 客官&#xff0c;码字不易&#xff0c;来个三连支持一下吧&#xff01;&#xff01;&#xff01;关注…

Spring 中最常用的 11 个扩展点

目录 1.自定义拦截器 2.获取Spring容器对象 2.1 BeanFactoryAware接口 2.2 ApplicationContextAware接口 3.全局异常处理 4.类型转换器 5.导入配置 5.1 普通类 5.2 配置类 5.3 ImportSelector 5.4 ImportBeanDefinitionRegistrar 6.项目启动时 7.修改BeanDefiniti…

MySQL高级【MVCC原理分析】

1&#xff1a;MVCC1.1&#xff1a;基本概念1). 当前读 读取的是记录的最新版本&#xff0c;读取时还要保证其他并发事务不能修改当前记录&#xff0c;会对读取的记录进行加 锁。对于我们日常的操作&#xff0c;如&#xff1a;select ... lock in share mode(共享锁)&#xff0c…