tostring

toString() method within System.out.println() a double call?

泪湿孤枕 提交于 2019-11-30 12:14:48
A professor of mine once said that the following code should never be done: System.out.println(object.toString()); He said (and I believe cited "Effective Java") it causes a double call. Since the print statement calls the toString method of an object, it would be less efficient to have the toString method called twice. The preferred method would be to just use: System.out.println(object); Obviously this way looks better in code and would save time. I will always do it like this no matter what, but my question is "Is this actually more EFFICIENT?". In looking through the PrintStream

Using string representations of enum values in switch-case

故事扮演 提交于 2019-11-30 11:37:09
Why is it not possible to use enum values as strings in a switch case? (Or what is wrong with this:) String argument; switch (argument) { case MyEnum.VALUE1.toString(): // Isn't this equal to "VALUE1" ? // something break; case MyEnum.VALUE2.toString(): // something else break; You can only use strings which are known at compile time. The compiler cannot determine the result of that expression. Perhaps you can try String argument = ... switch(MyEnum.valueOf(argument)) { case VALUE1: case VALUE2: case MyEnum.VALUE1.toString(): // Isn't this equal to "VALUE1" ? No, not necessarily: you are free

Does StringBuilder become immutable after a call to ToString?

这一生的挚爱 提交于 2019-11-30 11:26:37
I distinctly remember from the early days of .NET that calling ToString on a StringBuilder used to provide the new string object (to be returned) with the internal char buffer used by StringBuilder. This way if you constructed a huge string using StringBuilder, calling ToString didn't have to copy it. In doing that, the StringBuilder had to prevent any additional changes to the buffer, because it was now used by an immutable string. As a result the StringBuilder would switch to a "copy-on-change" made where any attempted change would first create a new buffer, copy the content of the old

Fastest way to convert a BigInteger to a decimal (Base 10) string?

冷暖自知 提交于 2019-11-30 09:24:40
问题 Answers So Far So here is the code breakdown. //Time: ~7s (linear loop algorithm) //100,000! (456,574 decimal digits) BigInteger bigIntVar = computeFactorial(100000); //The first three here are just for comparison and are not actually Base 10. bigIntVar.ToBase64String() //Time: 00.001s | Base 64 | Tetrasexagesimal bigIntVar.ToString("x") //Time: 00.016s | Base 16 | Hexadecimal bigIntVar.ToBinaryString() //Time: 00.026s | Base 02 | Binary bigIntVar.ToQuickString() //Time: 11.200s | Base 10 |

JavaScript中的toString( ) 与 valueOf( )方法、隐式转换

梦想的初衷 提交于 2019-11-30 08:16:41
说明 所有的对象都继承有toString() 和 valueOf() 方法,对象到字符串,对象到数字的转换,会通过调用待转换对象的这两个方法中的一个来完成。 解释 toString( )方法的作用是: 返回一个反映这个对象的字符串,而很多类都定义了不同版本的toString( )。 ({}.toString()); //=> "[object Object]" [1,2].toString(); //=> "1,2" true.toString(); //=> "true" new Date(1970,0,1).toString(); //=> "Thu Jan 01 1970 00:00:00 GMT+0800 (CST)" Error("一个错误信息").toString(); //=> "Error: 一个错误信息" (function (x){return x}).toString(); //=> "function (x){return x}" /\d/.toString(); //=> "/\\d/" 或者 "/\d/" 浏览器不同返回也可能会不同 valueOf() 方法,它的作用是: 如果对象存在任意原始值,它就默认将对象转换为表示它的原始值,如果对象是复合值,而且大多数对象无法真正表示为一个原始值,因此默认的valueOf( )方法简单地返回对象本身

Java之Lombok

时间秒杀一切 提交于 2019-11-30 07:05:48
1. Lombok背景介绍 官方介绍如下: Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code. 大致意思是Lombok通过增加一些“处理程序”,可以让java变得简洁、快速。 2.Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。例如开发中经常需要写的javabean,都需要花时间去添加相应的getter/setter,也许还要去写构造器、equals等方法,而且需要维护,当属性多时会出现大量的getter/setter方法,这些显得很冗长也没有太多技术含量,一旦修改属性,就容易出现忘记修改对应方法的失误。 Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。出现的神奇就是在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法。这样就省去了手动重建这些代码的麻烦,使代码看起来更简洁些。 Lombok的使用跟引用jar包一样,可以在官网(https://projectlombok

datetime.tostring month and day language

我是研究僧i 提交于 2019-11-30 06:55:54
问题 i have a list of email addresses of people that have different nationalities (for each person i have the iso code) when i send the email to all these people, in the text of the mail i need to to convert a datetime field to a string formatted in their specific culture. for this i'm doing CultureInfo ci = new CultureInfo(ISO); myStringDate = myDate.ToString(ci.DateTimeFormat.ShortDatePattern); and work perfect, but if i use LongDatePattern instead short, for displaying date like "Monday, 13

What is the meaning of ToString(“X2”)? [duplicate]

陌路散爱 提交于 2019-11-30 04:09:11
This question already has an answer here: ToString(“X”) produces single digit hex numbers 2 answers I'm studying MD5 encryption, and have found this code using Google: public string CalculateMD5Hash(string input) { // Primeiro passo, calcular o MD5 hash a partir da string MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hash = md5.ComputeHash(inputBytes); // Segundo passo, converter o array de bytes em uma string haxadecimal StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i

Printing F# discriminated union

眉间皱痕 提交于 2019-11-30 04:04:49
问题 I am writing a F# program which parses a string into a AST type which is a discriminated union. When I use fsi (on Mono + Mac OS X) to run my code, the AST is printed out in a nice format. But when I use printfn "%s" <| ast.ToString() I get something like FSI_0002.Absyn+clazz . Writing a ToString method for all the discriminated union types would be a big chore. How do I make the value print the way fsi does it? 回答1: Have you tried printfn "%A" ast ? The %A specifier takes into consideration

JS权威指南需要注意的知识点(1-6章)

走远了吗. 提交于 2019-11-30 03:36:42
客官快来看一看了,都给你浓缩好了,确定不进来搂一眼嘛,走过路过不要错过哟 in运算符   in运算符希望它的左操作数是一个字符串或可以转化为字符串,希望它的右操作数是一个对象,如果右侧的对象拥有一个名为左操作数值的属性名,则表达式返回true,否则为fasle  var obj = {X:1,Y:2} "X" in obj // true "Z" in obj //false "toString" in obj //true:对象继承了toString()方法 var data = [7,8,9] "0" in data //true: 查找的是下标0 1 in data //true:查找的下标1 3 in data // fasle: 下标3不存在"toString" in obj //true:对象继承了toString()方法   注意点:无论是对象还是数组,toString等从原型链上继承的方法也能查找的到,但右操作数为数组时,若为数字或者可转化成数字的字符串则查找的是下标 逻辑与(&&)   在赋值的时候需要要特别注意: 左假返左,左真返右 let a = false && 1 //a:false let a = true && false //a:false let a = true && 1 // a: 1   即赋值时&&左边为假,则直接返回左边