Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is not mandatory that Java make any construct similar to anything else, even C++. Anything adopted must be so because the developers accept it. Besides, arguments like "they made it similar to something else" do not explain why they don't just make an instance method version, they do this with primitive wrappers (in addition to the instance toString() method, they have the static version).</p> <p>This is what I think:</p> <p>On a normal case, both forms are equivalent, but suppose that we have something like:</p> <pre><code>String invalidFormat = "%invalid"; // or something else that is invalid </code></pre> <p>and then we call:</p> <pre><code>String.format(invalidFormat, anything); // "anything" is indeed anything.... </code></pre> <p>the invalid become the argument and Java clarify this by throwing an instance of IllegalArgumentException (even in the case of Formatter, there are many kinds).</p> <p>However, in something like:</p> <pre><code>invalidFormat.format(anything); </code></pre> <p>the invalid one is no longer the argument. The problem now lies with the instance it is called on and so is probaby better described as an "invalid state" (not Java's "IllegalStateException", which has a completely different usage). But because Strings are immutable, this so called "state" will never change, so it will always remain invalid as a format, even though it is a valid simple string.</p> <p>Compare this with, say, java.lang.Long. Both versions of toString will never throw any exception, so they are both equivalent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload