java + concat

concat()方法 (concat() method)

  • concat() is a method used for concatenation of strings.

    concat()是用于串联字符串的方法。

  • We pass only one string argument in concat() and concat it with other string.

    我们仅在concat()中传递一个字符串参数,然后将其与其他字符串连接。

Example:

例:

public class PassingArgument {public static void main(String[] args) {String str = "Java", lang = "Language";
System.out.println(str.concat(lang));
}
}

Output

输出量

D:\Programs>javac PassingArgument.java
D:\Programs>java PassingArgument
JavaLanguage

  • We should remember one thing at the time of passing the argument in concat() method. We can only pass a string and if we pass any other type of argument then we will get an error.

    concat()方法中传递参数时,我们应该记住一件事。 我们只能传递一个字符串,如果我们传递任何其他类型的参数,那么我们将得到一个错误。

  • concat() method throws NullPointer Exception when a string is concatenated with 'null'.

    当字符串与'null'连接时, concat()方法将引发NullPointer异常

Example:

例:

public class ConcatNull {public static void main(String[] args) {String str1 = "We will get an exception string concatenate with null in case of concat()";
String str2 = null;
// It raises an NullPointer Exception
System.out.println(str1.concat(str2));
}
}

Output

输出量

D:\Programs>javac ConcatNull.java
D:\Programs>java ConcatNull
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.String.concat(String.java:1936)
at ConcatNull.main(ConcatNull.java:7)

The performance of concat() is high as compare to '+' because it generates a new object when string length is greater than 0.

'+'相比, concat()的性能较高,因为它在字符串长度大于0时会生成一个新对象。

'+'运算符 ('+' operator)

  • '+' is an operator used for concatenation of strings.

    “ +”是用于字符串连接的运算符。

  • We can take any number of strings argument with '+' and merge it with all other strings.

    我们可以将任意数量的字符串参数加'+'并将其与所有其他字符串合并。

Example:

例:

public class PassingArgument {public static void main(String[] args) {String str = "Java", lang = "Language";
System.out.println(str + lang);
}
}

Output

输出量

D:\Programs>javac PassingArgument.java
D:\Programs>java PassingArgument
JavaLanguage

  • We don't need to remember anything at the time of passing argument in '+' operator. We can pass any type of argument if we pass other types of argument then we will not get any error.

    '+'运算符中传递参数时,我们不需要记住任何事情。 如果我们传递其他类型的参数,那么我们可以传递任何类型的参数,那么我们将不会收到任何错误。

  • '+' operator doesn't raise an exception when a string is concatenated with 'null'.

    当字符串与'null'连接时, '+'运算符不会引发异常。

Example:

例:

public class ConcatNull {public static void main(String[] args) {String str1 = "We will not get any exception when string concatenate with null in case of '+'";
String str2 = null;
// It will not raises any NullPointer Exception
System.out.println(str1 + str2);
}
}

Output

输出量

D:\Programs>javac ConcatNull.java
D:\Programs>java ConcatNull
We will not get any exception when string concatenate with null in case of '+'null

The performance of '+' operator is low as compare to concat() because it always generates new object whether string length is greater than 0 or less than 0.

concat()相比, '+'运算符的性能较低,因为无论字符串长度大于0还是小于0,它始终会生成新对象。

翻译自: https://www.includehelp.com/java/differences-between-concat-method-and-plus-operator-in-java.aspx

java + concat

java + concat_Java中concat()方法和加号(+)运算符之间的区别相关推荐

  1. java concat用法_java中concat()方法的使用说明

    concat()方法介绍: 将几个字符串连接到一起. 例如: s = s.concat(str1);//将字符串str1接到字符串s后面 s = s.concat(str2);//将字符串str1接到 ...

  2. Java快速入门学习笔记9 | Java语言中的方法

    有人相爱,有人夜里开车看海,有人却连LeetCode第一题都解不出来!虽然之前系统地学习过java课程,但是到现在一年多没有碰过Java的代码,遇到LeetCode不知是喜是悲,思来想去,然后清空自己 ...

  3. JSP中调用java类中的方法

    JSP中调用java类中的方法 1.新建一个项目,在src文件夹下添加一个包:如:test 2.再在包中添加一个类:如 package test; public class conDatabase { ...

  4. Java ArrayList中retainAll()方法具有什么功能呢?

    转自: Java ArrayList中retainAll()方法具有什么功能呢? 下文笔者讲述java中ArrayList方法的功能简介说明,如下所示: retainAll()方法的功能:用于保留 a ...

  5. thymeleaf 调用java,thymeleaf模板引擎调用java类中的方法(附源码)

    前言 由于开源了项目的缘故,很多使用了My Blog项目的朋友遇到问题也都会联系我去解决,有的是把问题留在项目的issue里提出,有的是在我的私人博客里留言,还有的则是直接添加我的qq来找我讲自己遇到 ...

  6. Java集合中contains方法的效率对比

    Java集合中contains方法的效率对比 Java集合List.Set中均有对集合中元素是否存在的判断方法contains(Object o):Map中有对key及value是否存在的判断方法co ...

  7. Java:使用split方法时忽略中英文的符号区别

    Java:使用split方法时忽略中英文的符号区别 split(",|,")

  8. IO多路复用中select、poll、epoll之间的区别

    本文来说下IO多路复用中select.poll.epoll之间的区别 文章目录 什么是IO多路复用 为什么有IO多路复用机制 同步阻塞(BIO) 同步非阻塞(NIO) IO多路复用(现在的做法) 3种 ...

  9. SQL Server中唯一索引和唯一约束之间的区别

    This article gives you an overview of Unique Constraints in SQL and also the Unique SQL Server index ...

  10. Java 8 中的方法引用,轻松减少代码量,提升可读性!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 1. 引言 Java8中最受广大开发中喜欢的变化之一是因为 ...

最新文章

  1. 基于RNN的NLP机器翻译深度学习课程 | 附实战代码
  2. 阿里P7试用期被淘汰,主管给出的理由让人意想不到
  3. 使用远程工具连接提示**Host *** is not allowed to connect to this mysql server**拒绝连接错误
  4. Java命令行界面(第2部分):args4j
  5. UNIX网络编程学习笔记(代码超详细解析)(持续更新)
  6. SAP License:客户特别总帐统驭科目某天余额取数逻辑
  7. 时序分析基本概念介绍——STA概述
  8. C语言九九乘法表(五种输出形式)
  9. mysql数据库xp下载64位_navicat premium 64位
  10. 基于 HPSocket , 实现 socket 通讯
  11. Android 自定义搜索框(带搜索图标、清除图标、语音图标)
  12. oracle px execute reply,关于昨天的PX Deq: Execute Reply重新开贴请教
  13. 【c语言】判断整数x是否是同构数。若是同构数,函数返回1;否则返回0.
  14. 钱从哪里来--读书笔记
  15. keil stm32标准库放在哪里_STM32(1)——使用Keil MDK以及标准外设库创建STM32工程...
  16. 待解决问题-流体力学
  17. 智慧城市App解决方案
  18. [概率论]图像里的“白噪声”——电视机搜不到台时雪花斑点的形成原因 (不信谣,不传谣,与宇宙微波背景辐射没有任何关系)
  19. 2019第十届蓝桥杯省赛C/C++B组题解
  20. 基于java我爱短视频管理系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署

热门文章

  1. Pycharm如何打开Django项目
  2. GreenDao清空数据库的方法
  3. K3 WISE修改单据表头字段默认值
  4. Typora(就是个浏览器)自定义设置。附带:Typora免费版链接
  5. Java物联网中间件_物联网中间件技术——Niagara介绍.pdf
  6. STM32F407主控板PCB
  7. 争分夺秒的一晚和赛尔的烂网络
  8. 英语词根词缀+联想法记忆单词
  9. python处理Excel数据串行串列问题
  10. Java中list集合去重方法