(String) or .toString ()?

I have a method with the Object o parameter.

In this method, I know for sure that there is a String in "o" that is not null. No need to check or do anything else. I should consider it exactly as a String object.

Just curious - which is cheaper - cast it to String or use Object.toString() ? Or is it the same time / cpu - / mem-price?

Update: The method accepts Object because it is an implementation of the interface. Cannot change parameter type.

And in general it cannot be null . I just wanted to say that I do not need to check it for emptiness or emptiness. In my case, there is always a non-empty string.

+71
java casting
Mar 24 '09 at 6:51
source share
9 answers

casting to String is cheaper because it does not require calling an external function, just checking the internal type.

+56
Mar 24 '09 at 7:04
source share

I would use the cast. This confirms your "knowledge" that this is a string. If for some reason you end up with an error and someone passes something other than a string, I think it would be better to throw an exception (which the cast will do) than to continue to execute with incorrect data.

+40
Mar 24 '09 at 6:59
source share

According to Silly Performance Representations: x.toString () vs (String) x

In the end, the results are surprisingly clear: it is at least twice as fast to pass an object to a string than to call Object.toString ()

+26
Mar 24 '09 at 10:54
source share

If you know that Object o is a string, I would say just passed it to String and provided it that way. Calling toString () for an object that you know is a string can just add confusion.

If Object o can be anything other than String, you need to call toString ().

+5
Mar 24 '09 at 6:56
source share

I would not worry too much about performance, if this operation is performed even several thousand times per second - there are no noticeable differences.

I would, however, be concerned about the β€œknowledge” of input. You have a method that takes an Object , and you should consider it as such, i.e. You should not know anything about the parameter, except that it adheres to the Object interface, which has the toString() method. In this case, I highly recommend using this method, rather than just doing something.

OTOH, if the input is always either String or null , just change the method to accept String s, and explicitly check for null (what should you do anyway when dealing with non-primitives ...)

+3
Mar 24 '09 at 6:56
source share

There cannot be a "zero line in o". If o is null, it does not contain an empty string, it is just null. Just check o first for null. If you say or call ToString () at zero, you will work.

+1
Mar 24 '09 at 6:55
source share

If what you have in "o" is a string, then there isn’t much difference (probably the cast is faster, but this is a thing of the VM / Library implementation).

If "o" may not be a string, but it is assumed to be a String, then the cast is what you want (but you must force the method to take a string instead of an object).

If "o" can be any type, then you should use toString, but be sure to check the null value first.

 void foo(final Object o) { final String str; // without this you would get a class cast exception // be wary of using instanceof though - it is usually the wrong thing to do if(o instanceof String) { str = (String)o; } } 

or

 void foo(final Object o) { final String str; // if you are 100% sure that o is not null then you can get rid of the else if(o != null) { str = o.toString(); } } 

I would prefer the code of the latter:

 void foo(final Object o) { final String str; if(o == null) { throw new IllegalArgumentException("o cannot be null"); } str = o.toString(); } 
+1
Mar 24 '09 at 7:08
source share

Given that the reference type is Object and all objects have toString (), just call object.toString (). String.toString () just returns this.

  • toString () is less code to enter.
  • toString () is less than bytecode.
  • casting is an expensive VS polymorphic call operation.
  • may crash.
  • Use String.valueOf (object), which simply calls object.toString () if its value is not null.
+1
Mar 24 '09 at 9:21
source share

It seemed strange to me that the cast was slower than looking at the vtable implied by calling tostring.

0
Mar 24 '09 at 7:14
source share



All Articles