stringbuilder

Using single StringBuilder - throws OutofmemeoryException

孤者浪人 提交于 2019-12-11 03:20:00
问题 do like the below code, to read each line and perform a concatenation based on an input list listValues. BufferedReader br = null; StringBuilder sb = new StringBuilder(""); InputStream in = new FileInputStream(new File(file)); br = new BufferedReader(new InputStreamReader(in), 102400); for (String input; (input= br.readLine()) != null;) { for (int i = 0; i < listValues.size(); i++) { sb.append(input.substring(1, 5)); } map.put(sb.toString(), someOtherValue); sb.delete(0, sb.length()); } Using

Why do I need to reset setText() in a JLabel to prevent errors?

六眼飞鱼酱① 提交于 2019-12-10 23:55:27
问题 As a follow-up to this question that I posted earlier, I am wondering about the cause of the issue I had. The problem was that I was getting this error when updating a JLabel with a lot of HTML text. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.text.html.StyleSheet$ListPainter.paint(Unknown Source) at javax.swing.text.html.ListView.paintChild(Unknown Source) at javax.swing.text.BoxView.paint(Unknown Source) at javax.swing.text.html.BlockView.paint

Java类加载过程

一世执手 提交于 2019-12-10 23:35:33
目录 一、class文件介绍 1.1 文件结构 1.2 简单示例读取class文件 二、类加载步骤 2.1 类加载过程 三、总结 熟悉的面试题,现在看来也显然易见! 类加载过程已经是老生常谈模式了,我曾在讲解tomcat的书中、在Java基础类书、JVM的书、在Spring框架类的书中、以及各种各样的博客和推文中见过,我虽然看了又忘了,但总体还是有些了解,曾经自以为这不是什么大不了的过程。但时间总会教你做人,看得越多,越觉得以前理解不足。 此笔记记录,虚拟机中Java类加载的过程,各个过程发生的时机,具体做了什么事情,例如,在方法区或者堆分配了哪些内存,创建了哪些常量等。由于Java文件会预先编译,得到class文件,虚拟机的类加载,是对class文件进行的操作,所以不可避免的涉及到class文件的解读,只有知道class文件中有什么,虚拟机才能加载对应的内容。 一、class文件介绍 ​ 不可能完全解读class文件,《虚拟机规范第二版》花了一百多页写class文件,这是class的核心,如果要完全理解,可能还得去复习复习编译原理,词法分析语法分析代码生成之类的。 1.1 文件结构 文件结构定义:u1 = 1个字节,u2 = 2个字节,u4 = 4个字节,u8 = 8个字节; ClassFile { u4 magic; // 魔法数 u2 minor_version; //

How many memory locations will it take to have a string concatenation?

可紊 提交于 2019-12-10 15:00:07
问题 How many memory locations will it take to have a string concatenation? String myStringVariable = "Hello"; In following two statements : String s = "ABC" + "Hello" + "DEF"; and String s = "ABC"; s = s + "Hello"; s = s + "DEF"; and String s = "ABC" + myStringVariable + "DEF"; Which will consume more memory? In which of the case StringBuilder is useful to the most? 回答1: First statement will be converted by compiler into String s = "ABCDEF"; so there will be no concatination Second statement will

String Concat using constants - performance

末鹿安然 提交于 2019-12-10 14:46:36
问题 Assume I have the following string constants: const string constString1 = "Const String 1"; const string constString2 = "Const String 2"; const string constString3 = "Const String 3"; const string constString4 = "Const String 4"; Now I can append the strings in two ways: Option1: string resultString = constString1 + constString2 + constString3 + constString4; Option2: string resultString = string.Format("{0}{1}{2}{3}",constString1,constString2,constString3,constString4); Internally string

String concatenation with the + symbol

∥☆過路亽.° 提交于 2019-12-10 09:41:27
问题 Today I was reading Antonio's Blog about toString() performance and there is a paragraph: What used to be considered evil yesterday (“do not concatenate Strings with + !!!“), has become cool and efficient! Today the JVM compiles the + symbol into a string builder (in most cases) . So, do not hesitate, use it. Now I am confused, because he is saying Today the JVM compiles the + symbol into a string builder (in most cases) , but I have never heard or seen(code) anything like this before. Could

Autofac 小试

怎甘沉沦 提交于 2019-12-10 04:24:10
ContainerBuilder builder = new ContainerBuilder(); ////builder.RegisterType<GetTest>().As<IBufrTest>(); //builder.RegisterType<GetTest>().AsImplementedInterfaces(); //builder.RegisterType<gettest2>().As<IBufrTest>(); string dllFIle = System.IO.Directory.GetCurrentDirectory() + "\\DemoBufr.dll"; builder.RegisterAssemblyTypes(Assembly.LoadFile(dllFIle)).AsImplementedInterfaces(); IContainer resorlver = builder.Build(); var test = resorlver.Resolve<IEnumerable<IBufrTest>>(); StringBuilder sbtext = new StringBuilder(); foreach (var item in test) { sbtext.Append(item.getstring()).Append("\r\n"); }

java如何获取mac物理地址

若如初见. 提交于 2019-12-10 03:46:22
前言 原本以为这功能调用一个api就完事了,然而,查了一下没那么简单,网上查的资料实在忍不住要拿出来说说,估计真的都没有试过其他环境,而且,估计连背后的原理都没了解就用了。。 下面先说说找到的几份资料: ps:本机相关ip信息: 第一篇参考 Java获取本机MAC地址 点评:实际运行效果如下: 空指针啊。。。那么我们如果换个ip来试试? 第二篇参考 使用java获取本机mac 看看代码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class getmac{ public static String getLinuxMACAddress() { String mac = null; BufferedReader bufferedReader = null; Process process = null; try { process = Runtime.getRuntime().exec(“ifconfig enp4s0”); bufferedReader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line =

How much does Java optimize string concatenation with +?

余生长醉 提交于 2019-12-10 03:26:21
问题 I know that in more recent Java versions string concatenation String test = one + "two"+ three; Will get optimized to use a StringBuilder . However will a new StringBuilder be generated each time it hits this line or will a single Thread Local StringBuilder be generated that is then used for all string concatenation? In other words can I improve on the performance for a frequently called method by creating my own thread local StringBuilder to re-use or will there be no significant gains by

JavaSE核心——各种类

守給你的承諾、 提交于 2019-12-10 01:35:32
一,Object类 在java的继承体系中,java.lang.Object类位于顶端(是所有对象的直接或间接父类)。如果一个没有使用extends关键字来表明要继承的类,则默认继承java.lang.Object类,Object类定义了“对象”的基本行为,被子类默认继承。 1,toString()方法:返回一个可以表示该对象属性内容的字符串。 Object obj = new Object(); String info = obj.toString() ; Systmem.out.println(info) ; //返回一个可以表示该对象属性内容的字符串。 ①使用toString()方法,因为所有的类都继承了Object类,而toString()方法是Object类定义的,所以我们直接继承了这个方法,Object类的toString()方法帮我们返回了一个字符串。这个字符串的格式是固定的。 ② public static void main(String[] args){ //SOP(toString()) ; //不能直接调用,编译错误 Point p = new Point(1); System.out.println(p) ; //输出p对象的toString()方法返回值。 } 不能直接使用toString()方法,因为该方法不是静态的