stringbuilder

2020年Java面试100题

别来无恙 提交于 2019-12-19 01:54:10
一、Java 基础 1. JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境。 JRE:Java Runtime Environment 的简称,java 运行环境,为 java 的运行提供了所需环境。 具体来说 JDK 其实包含了 JRE,同时还包含了编译 java 源码的编译器 javac,还包含了很多 java 程序调试和分析的工具。简单来说:如果你需要运行 java 程序,只需安装 JRE 就可以了,如果你需要编写 java 程序,需要安装 JDK。 2. == 和 equals 的区别是什么? == 解读 对于基本类型和引用类型 == 的作用效果是不同的,如下所示: 基本类型:比较的是值是否相同; 引用类型:比较的是引用是否相同; 代码示例: String x = "string"; String y = "string"; String z = new String("string"); System.out.println(x==y); // true System.out.println(x==z); // false System.out.println(x.equals(y)); // true System.out.println(x.equals(z));

What does “ StringBuilders are not thread-safe” mean?

廉价感情. 提交于 2019-12-18 13:28:39
问题 I have read some articles about the pros and cons of String and StringBuilder in the Java Programming language. In one of the articles, the author mentioned that: StringBuilder is not Thread-safe , so in multiple threads use StringBuffer . Unfortunately, I cannot understand what it means. Could you please explain the difference between String , StringBuilder and StringBuffer especially in the context of "Thread safety". I would appreciate it if you could describe with the code example. 回答1:

When and Why Should I Use TStringBuilder?

拟墨画扇 提交于 2019-12-18 12:55:28
问题 I converted my program from Delphi 4 to Delphi 2009 a year ago, mainly to make the jump to Unicode, but also to gain the benefits of all those years of Delphi improvements. My code, of course, is therefore all legacy code. It uses short strings that have now conveniently all become long Unicode strings, and I've changed all the old ANSI functions to the new equivalent. But with Delphi 2009, they introduced the TStringBuilder class, presumably modelled after the StringBuilder class of .NET. My

Java CharAt() and deleteCharAt() performance

筅森魡賤 提交于 2019-12-18 11:23:35
问题 I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the complexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ? 回答1: For String , StringBuffer , and StringBuilder , charAt() is a constant-time operation. For StringBuffer and StringBuilder , deleteCharAt() is a linear-time operation. StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is that the

Why use StringBuilder? StringBuffer can work with multiple thread as well as one thread?

南楼画角 提交于 2019-12-18 10:59:23
问题 Suppose our application have only one thread. and we are using StringBuffer then what is the problem? I mean if StringBuffer can handle multiple threads through synchronization, what is the problem to work with single thread? Why use StringBuilder instead? 回答1: StringBuffers are thread-safe, meaning that they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time. Thus, StringBuffer objects are generally safe to use

老生常谈:StringBuilder 和 StringBuffer 的那些事儿

一世执手 提交于 2019-12-18 10:46:29
老生常谈:StringBuilder 和 StringBuffer 的那些事儿 作为一个老生常谈的话题,相信大家对于 StringBuilder 和 StringBuffer 一定不陌生,因为它们经常出现在面试的题目当中。下面我们就来聊一聊关于 StringBuilder 和 StringBuffer 的那些事儿。 1、为什么会有 StringBuilder 和 StringBuffer ? 首先,我们来看一个字符串拼接的例子: String result = new String ( "Hello, " ) + new String ( "I " ) + new String ( "am " ) + new String ( "HappyFeet!" ) ; 对 JVM 编译优化有过研究的人应该知道,这段代码在编译之后就等价于: String result = new StringBuilder ( ) . append ( new String ( "Hello, " ) ) . append ( new String ( "I " ) ) . append ( new String ( "am " ) ) . append ( new String ( "HappyFeet!" ) ) . toString ( ) ; 这是 JVM 的早期编译优化。这样做的目的是什么呢

Nice looking table with StringBuilder

血红的双手。 提交于 2019-12-18 09:57:03
问题 I know that "printf" method can use string formatting. My question is : Is there a way to create a nice looking table with StringBuilder class? For example: |Id|Category|Level|Space|Type|Adress|Dimension|Limits| And under that row, i must add the values of each columns! Doing something like that : example but with StringBuilder So the community want to see my answer (which i don't understand why ... but any way i will put it !) public String toString(){ StringBuilder s = new StringBuilder();

StringBuilder.ToString() throws OutOfMemoryException

别来无恙 提交于 2019-12-18 05:44:14
问题 I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException . StringBuilder SB = new StringBuilder(); for(int i =0; i<=5000; i++) { SB.Append("Some Junk Data for testing. My Actual Data is created from different sources by Appending to the String Builder."); } try { string str = SB.ToString(); // Throws OOM mostly Console.WriteLine("String Created Successfully"); } catch(OutOfMemoryException ex) { StreamWriter

Is using a StringBuilder a right thing to do in F#?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 03:01:59
问题 StringBuiler is a mutable object, F# encourages employing immutability as much as possible. So one should use transformation rather than mutation. Does this apply to StringBuilder when it comes to building a string in F#? Is there an F# immutable alternative to it? If so, is this alternative as efficient? A snippet 回答1: I think that using StringBuilder in F# is perfectly fine - the fact that sb.Append returns the current instance of StringBuilder means that it can be easily used with the fold

java outOfMemoryError with stringbuilder

▼魔方 西西 提交于 2019-12-18 02:49:48
问题 I'm getting a java outOfMemoryError when I call this method - i'm using it in a loop to parse many large files in sequence. my guess is that result.toString() is not getting garbage collected properly during the loop. if so, how should i fix it? private String matchHelper(String buffer, String regex, String method){ Pattern abbrev_p = Pattern.compile(regex);//norms U.S.A., B.S., PH.D, PH.D. Matcher abbrev_matcher = abbrev_p.matcher(buffer); StringBuffer result = new StringBuffer(); while