Difference between x.toString () and x + ""

I'll be back in college, one of my professors. taught us to just do x + ""how fast conversion from basic types to strings.
I don’t remember what class it was in, I had it for some Java and C ++ courses (I have not used them for some time), but I use it in C # /. Net, now this is what I primarily develop recently.

Is there any advantage to using .toString()over +""for basic types like int, decimal, float ...? In what cases would it be better .toString()?

Note. They also showed me .toString()that the professor just recommended +"", because he was shorter, and I just did it since then without asking him.

+3
source share
8 answers

Well, as a side note, it depends on what x is. If x is primitive in Java, you should call .toString()using one of your shells, for example

Integer.toString(x)

I would say that using toString () is usually better, because x + "", at least in Java, says you want to add two lines together.

Like in this example:

 public static void main(String[] args)
 {
   int x = 3;
   String s = x + "";   
 }

It ends in bytecode like:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_3
   1:   istore_1
   2:   new #2; //class java/lang/StringBuilder
   5:   dup
   6:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   9:   iload_1
   10:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   13:  ldc #5; //String 
   15:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   18:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   21:  astore_2
   22:  return

Therefore, he must create a StringBuilder to add "" and the value of String x together. Although there is not much loss in efficiency, it is not easy to use the toString function.

Compare using toString:

 public static void main(String[] args)
 {
   int x = 3;
   String s = Integer.toString(x); 
 }

What ends up like:

public static void main(java.lang.String[]);
Code:
 0: iconst_3
 1: istore_1
 2: iload_1
 3: invokestatic    #2; //Method java/lang/Integer.toString:(I)Ljava/lang/String;
 6: astore_2
 7: return

, .toString , - , String x, x + " - String x, " ".

: -, # , . , # .ToString() , , , .

+14

, , .

, , x + "" # ( + x string) - string.Concat(x, ""), x.ToString .

, x + "" , x.ToString. , x , , x , + x ( point, , x ToString, , , , , ).

, , . , ; , x + "" .NET, . , , .NET, , .NET, .

, , Java, , Integer.toString(x) x int, # .NET ( "" ) object, ToString ( GetType GetHashCode).

+3

Java (int, double ..) .toString(), . , - -

x + "";

Integer.toString(x);

++ x + "" , . - boost::lexical_cast .

... #, .: -)

+2

x+"". toString - Object, , - .

, .

0

, x+"" : x.toString() StringBuffer.append() ( , Java). , JIT , x.toString(), , .

0

. , - . , . , .

0

Java,

, x+"" null. x.toString() a NullPointerException, x+"" "null". , , .

0

# - , , .ToString .

, , , x - null. , .

public static string NullSafeToString<T>(this T value) where T : class {
  return value == null ? "" : value.ToString();
}

, , , . , , z ?

int x = 42;
int y = 15;
string z = x + y + "";

int x = 42;
int y = 15;
string z = x + y.ToString();

The latter, most likely, at first glance can be understood by the average developer, who did not find time for C # memory priority. Therefore, I would prefer this because he is less likely to be misunderstood.

0
source

Source: https://habr.com/ru/post/1786265/


All Articles