tostring

Why toString() method works differently between Array and ArrayList object in Java

大憨熊 提交于 2019-11-28 19:40:14
String[] array = {"a","c","b"}; ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); System.out.println(array); System.out.println(list); For list [a, b, c] is output while for array some address is output. When we want to output the array values, we can use Arrays.toString(array); which works just like list . I just wonder why we can't call toString() directly on array to get the values. Isn't it more intuitive and convenient to do so? What results in the different treatments on Array and ArrayList ? The main difference between an array and an

What is the best standard style for a toString implementation? [closed]

萝らか妹 提交于 2019-11-28 19:08:51
We have a lot of objects for which we like to implement a simple toString to output attributes of the object. Some of these attributes may be complex objects themselves. Is there any standard, or simply just a best practice for a style? I'm thinking something like: [SimpleClassName] { prop1:value, prop2:value } In which case a nested value would look like: [SimpleClassName] { prop1:value, prop2:[NestedObject] { prop3:value}} We are using Java but I find myself asking the same question in most languages! Wouter Coekaerts Personally, I find the mix of [] and {} not so easy to get an immediate

ToString() function in Go

瘦欲@ 提交于 2019-11-28 17:42:07
The strings.Join function takes slices of strings only: s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) But it would be nice to be able to pass arbitrary objects which implement a ToString() function. type ToStringConverter interface { ToString() string } Is there something like this in Go or do I have to decorate existing types like int with ToString methods and write a wrapper around strings.Join ? func Join(a []ToStringConverter, sep string) string Attach a String() string method to any named type and enjoy any custom "ToString" functionality: package main import "fmt"

Difference between .ToString and “as string” in C#

房东的猫 提交于 2019-11-28 17:09:00
What is the difference between using the two following statements? It appears to me that the first "as string" is a type cast, while the second ToString is an actual call to a method that converts the input to a string? Just looking for some insight if any. Page.Theme = Session["SessionTheme"] as string; Page.Theme = Session["SessionTheme"].ToString(); If Session["SessionTheme"] is not a string , as string will return null . .ToString() will try to convert any other type to string by calling the object's ToString() method. For most built-in types this will return the object converted to a

Overriding toString method

和自甴很熟 提交于 2019-11-28 14:26:11
I am using .toString to return a string representation of an object, i.e. jcb.engineMove(move.toString()); will produce e2e4. What I am trying to do is to extract the text of this object (e2e4) as a string. After Googling I came across overriding the toString method so I came up with this: @Override public String toString() { String s = ""; int newRank = getRank(); int newFile = getFile(); final Move move = new Move(rank, file, newRank, newFile); s+="" + move; return s; } My questions are fairly basic: is this the right approach How do I call this routine when trying to get the text of the

Java 之 Object 类

核能气质少年 提交于 2019-11-28 13:51:47
一、Object 概述    java.lang.Object 类是 Java 语言中的根类,即所有类的父类。   在对象实例化的时候,最终找的父类就是 Object。   如果一个类没有特别指定父类,那么默认则继承自 Object 类。   Demo: 1 public class MyClass /*extends Object*/ { 2 // ... 3 }    Object 类当中包含的方法有11个,下面主要来介绍其中的两个: public String toString()`:返回该对象的字符串表示。 public boolean equals(Object obj):指示其他某个对象是否与此对象“相等”。 二、toString方法    方法:     public String toString() :返回该对象的字符串表示。   toString 方法返回该对象的字符串表示,其实该字符串内容就是对象的类型+@+内存地址值。   由于 toString 方法返回的结果是内存地址,而在开发中,需要按照对象的属性得到相应的字符串表现形式,因此也需要进行重写。   覆盖重写:    如果不希望使用 toString 的默认行为,则可以对它进行覆盖重写。   Demo: 1 public class Person { 2 private String name; 3

Creating String representation of lambda expression [duplicate]

懵懂的女人 提交于 2019-11-28 13:29:18
This question already has an answer here: Is it possible to retrieve lambda expression at runtime 2 answers For debugging purposes I am trying to create string representations of lambda expressions (specifically of Predicate s, though it would be interesting for other lambda expressions too) in Java 8. My idea would be something like this: public class Whatever { private static <T> String predicateToString(Predicate<T> predicate) { String representation = ... // do magic return representation; } public static void main(String[] args) { System.out.println(Whatever.<Integer>predicateToString(i -

Getting weird text back with EditText.toString() method in Android project. Why?

时光毁灭记忆、已成空白 提交于 2019-11-28 13:09:51
I appear to have a fundamental gap in my understanding of an EditText object. I have an Activity+Layout with a single EditText object. After I type a few characters into the EditText object and hit the Enter key, I retrieve the text in my onKey() listener. When I use the toString() method to retrieve the text I get back a weird string like: android.widget.EditText@43749ff0 Despite the fact the EditText.mText property does show the string I entered, "123" during my tests. Why is toString() returning a different result and what appears to be some kind of "uninitalize" value? How do I get the

Generate Java Object from toString representation [duplicate]

依然范特西╮ 提交于 2019-11-28 12:10:33
问题 This question already has an answer here: Converting back from toString to Object 7 answers How to get back an object after performing .toString() on it? [duplicate] 4 answers We all know how to implement toString () method. It could be slightly custom implementation and different pattern how we print the object data. Using the generated toString , can we recreate the Object? I am not talking about Serialization here. Let me explain a scenario, You might have an application running happily in

Is it really worth implementing toString() for entity classes

给你一囗甜甜゛ 提交于 2019-11-28 12:05:15
It is consistently advised to override (implement) the toString() method of a class. The Java API documentation itself says "It is recommended that all subclasses override this method.". Bloch, in Effective Java has the item "Always override toString". And only a fool contradicts Bloch, right? I am however coming to doubt this advice: is it really worth implementing toString() for entity classes ? I'll try to lay out my reasoning. An entity object has a unique identity; it is never the same as another object, even if the two entites have equivalent attribute values. That is, (for non-null x ),