java - Why is String's format(Object... args) defined as a static method? -


I wonder why Java 5 and above is a printf using a static method in the client -style formatter provides string like this:

  Public Static String Format (String Format, Object ... Args)  

Instead of

  public string format (object ... args)  

so that we can write "% 02d". Get format (5) For 05 instead of String.format ("% 02d", 5) .

I thought that if I can modify the string class, then I can add it:

  public string format (object ... args) {return format (This, args)}  

to get the same result.

I have found that in C #, a static method is used instead of the example method.

I wonder why they decided to do this, but I did not come for explanation. Example methods give a new example of the trim and substring string, so they should have done the same thing with format .

In addition, DateFormat class also uses it:

  public final string format (date)  

For formatting dates so if we see the example of DateFormat as a formatter, the example of the string can also be used as a formatter.

Any thoughts?

probably "% 02d" .format (5) this indicates The object on which the format method is being called is called a format string.

In this situation, if the format string is also string , then proceed to that point, anybody can argue that all the string S format strings .

Perhaps it can be avoided by saying that a static method can be used in the string class to format the string, in general all the string s.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -