stringbuffer

Why StringBuffer has a toStringCache while StringBuilder not?

无人久伴 提交于 2019-12-05 09:29:04
In JDK 8, StringBuffer class has a toStringCache , while StringBuilder doesn't. /** * A cache of the last value returned by toString. Cleared * whenever the StringBuffer is modified. */ private transient char[] toStringCache; But why? One possible reason I can think of is that StringBuffer is already synchronized so a cache can be implemented easier. Or maybe historically StringBuffer was implemented this way so old code depends heavily on this feature? Given modern JVM with escape analysis and biased locking, is the difference relevant anymore? It might help to consider the historical context

Passing string buffer to java program in IntelliJ debug/run

半腔热情 提交于 2019-12-05 00:04:05
How does one accomplish equivalent of running following line on command line in IntelliJ or Eclipse .... : java MyJava < SomeTextFile.txt I've attempted to provide location of the file in Program Arguments field of Run/Debug Configuration in IntelliJ As @Maba said we can not use Input redirection operator (any redirection operator) in eclipse/intellij as there no shell but you can simulate the input reading from a file through stdin like the below InputStream stdin = null; try { stdin = System.in; //Give the file path FileInputStream stream = new FileInputStream("SomeTextFile.txt"); System

Null pointer access: The variable can only be null at this location

霸气de小男生 提交于 2019-12-04 09:32:25
for(int i=0;i<n;i++){ for(int j=0;j<26;j++){ if(str.charAt(i)== strChar.charAt(j) ) * strSet1.append(str.charAt(i)); } * strSet2.append(str.charAt(i)); } Exception: Exception in thread "main" java.lang.NullPointerException at AterSeries.main(AterSeries.java:33) why this code gives null pointer exception warning: Null pointer access: The variable strSet1 can only be null at this location Null pointer access: The variable strSet2 can only be null at this location Are strSet1 and strSet2 initialized before this? If they are null, you'd get a NullPointerException . * EDIT * You cannot call .append

Writing multiline JTextArea to txt file

与世无争的帅哥 提交于 2019-12-04 04:10:17
问题 so I Have a JTextArea for user input and then when they click a button it writes it to a text file, I have both setLineWrap and setWrapStyleWord set to true for the JTextArea . I would like to write to the text file the exact way it appears in the text box. I have tried to do a replace("\n", System.getProperty("line.separator")) on the String holding the contents of the JTextArea which works, but only if the user actually hits the return key when typing the input, but if the user just keeps

Difference between StringBuilder and StringBuffer

六眼飞鱼酱① 提交于 2019-12-04 03:11:24
问题 What is the main difference between StringBuffer and StringBuilder ? Is there any performance issues when deciding on any one of these? 回答1: StringBuffer is synchronized, StringBuilder is not. 回答2: StringBuilder is faster than StringBuffer because it's not synchronized . Here's a simple benchmark test: public class Main { public static void main(String[] args) { int N = 77777777; long t; { StringBuffer sb = new StringBuffer(); t = System.currentTimeMillis(); for (int i = N; i --> 0 ;) { sb

Prevent exposure of sensitive data against PCI standards - response.getWriter().write(xml.toString())

…衆ロ難τιáo~ 提交于 2019-12-02 21:20:59
问题 I am fixing code against the code audit report. It says "PREVENT EXPOSURE OF SENSITIVE DATA" against the line having the syntax response.getWriter().write(xml.toString()) . The whole code is below. String alertId = request.getParameter("alertId") != null ? request.getParameter("alertId") : ""; String desc=AAAA.getBBBB(Long.parseLong(AAAA.getCCCC(alertId))); StringBuffer xml = new StringBuffer(); xml.append("<?xml version=\"1.0\"?>"); xml.append("<parent>"); xml.append("<child>"); xml.append("

Prevent exposure of sensitive data against PCI standards - response.getWriter().write(xml.toString())

て烟熏妆下的殇ゞ 提交于 2019-12-02 07:57:16
I am fixing code against the code audit report. It says "PREVENT EXPOSURE OF SENSITIVE DATA" against the line having the syntax response.getWriter().write(xml.toString()) . The whole code is below. String alertId = request.getParameter("alertId") != null ? request.getParameter("alertId") : ""; String desc=AAAA.getBBBB(Long.parseLong(AAAA.getCCCC(alertId))); StringBuffer xml = new StringBuffer(); xml.append("<?xml version=\"1.0\"?>"); xml.append("<parent>"); xml.append("<child>"); xml.append("<alertDesc>"); xml.append(desc); xml.append("</alertDesc>"); xml.append("</child>"); xml.append("<

java内存分配和String类型的深度解析

拟墨画扇 提交于 2019-12-01 18:25:34
一、引题 在java语言的所有数据类型中,String类型是比较特殊的一种类型,同时也是面试的时候经常被问到的一个知识点,本文结合java内存分配深度分析关于String的许多令人迷惑的问题。下面是本文将要涉及到的一些问题,如果读者对这些问题都了如指掌,则可忽略此文。 1、java内存具体指哪块内存?这块内存区域为什么要进行划分?是如何划分的?划分之后每块区域的作用是什么?如何设置各个区域的大小? 2、String类型在执行连接操作时,效率为什么会比StringBuffer或者StringBuilder低?StringBuffer和StringBuilder有什么联系和区别? 3、java中常量是指什么?String s = "s" 和 String s = new String("s") 有什么不一样? 本文经多方资料的收集整理和归纳,最终撰写成文,如果有错误之处,请多多指教! 二、java内存分配 1、JVM简介 Java 虚拟机 (Java Virtual Machine 简称JVM)是运行所有Java程序的抽象计算机,是 Java语言 的运行环境,它是Java 最具吸引力的特性之一。J ava虚拟机有自己完善的 硬体 架构,如 处理器 、 堆栈 、 寄存器 等,还具有相应的 指令 系统。JVM屏蔽了与具体 操作系统 平台相关的信息,使得Java 程序

Difference between StringBuilder and StringBuffer

社会主义新天地 提交于 2019-12-01 16:55:50
What is the main difference between StringBuffer and StringBuilder ? Is there any performance issues when deciding on any one of these? sblundy StringBuffer is synchronized, StringBuilder is not. polygenelubricants StringBuilder is faster than StringBuffer because it's not synchronized . Here's a simple benchmark test: public class Main { public static void main(String[] args) { int N = 77777777; long t; { StringBuffer sb = new StringBuffer(); t = System.currentTimeMillis(); for (int i = N; i --> 0 ;) { sb.append(""); } System.out.println(System.currentTimeMillis() - t); } { StringBuilder sb =

A Set in java never allows duplicates, but it takes StringBuffer objects with the same argument. Why?

本秂侑毒 提交于 2019-12-01 05:18:30
public static void main(String[] args) { HashSet set = new HashSet(); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); System.out.println(set); } Output: [abc,abc,abc,abc] Here in above code I added object of StringBuffer("abc") many times and Set adds it but Set never adds duplicates. StringBuffer does not override Object#equals() and Object#hashCode() , so identity of StringBuffer instances is based not on the contents of the buffer, but by the object's address in memory.* * That identity is based on an