Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

CH8 PG.439 format() and printf()  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that these methods are described as using a Locale object, but looking at the Java documentation for SE8 I noticed that these only take a String as a formatting text tool and not a Locale which actually makes more sense.

This was the only reference to formatting I found, but I misunderstood this text before rereading this chapter a second time and really looking at what was said. Perhaps the below comment makes sense, but I still find it confusing if you're not sure what is meant because it makes no mention of Locale objects, just format being referenced from chapter 5. Perhaps the errata can be updated to include something like "The Locale object is not part of the format() and printf() methods, and is instead handled by a String formatter"
Various Chapter 8 Ignore the references to format being in chapter 5. You do not need to know String formatting on the exam. Old text Seb Cano 2016-02-20 Pending


Formatter javadoc:

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax

 
Marshal
Posts: 58830
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always supply full details of the book, to prevent copyright problems, and also details of the question. I don't have that book, so I might not know what the question says.
In this case, however, you have provided lots of details about the answer, so I think I can guess what the problem is. The System.out.printf(), System.out.format(), String#format() and Formatter#format() methods all work the same way. I believe they all call the Formatter version behind the scenes. If you go through those links, you will see they all do use a Locale.

Two of those four API links wrote:The locale always used is the one returned by Locale.getDefault()...

In the case of Formatter, you would have to look at its constructors; all the constructors not taking a Locale parameter say:-

The locale used is the default locale . . . .

If you look up System.out.printf(), it says it does the same as System.out.format().

So, when we have investigated all versions of format() and printf(), we find they use the default Locale unless you have specified another Locale.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!