13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.2 Fundamental Language Elements79System.out.println("The answer is "+val);“But what if val is not a String?” we hear you asking. D<strong>on</strong>’t worry, theJava compiler is smart enough to know, that when you are adding with aString argument it must c<strong>on</strong>vert the other argument to a String, too. So forany Object, it will implicitly call its toString() method. For any primitivetype (e.g., int or boolean), the compiler will c<strong>on</strong>vert it to a String, too.The third of the three output methods, printf(), sounds very familiarto C/C++ programmers, but be warned:• It is <strong>on</strong>ly available in Java 5.0 7 and after.• It is similar but not identical to the C/C++ versi<strong>on</strong>.Perhaps the most significant enhancement to printf() is its additi<strong>on</strong>alsyntax for dealing with internati<strong>on</strong>alizati<strong>on</strong>. It’s all well and good to translateyour Strings to a foreign language, but in doing so you may need to changethe word order and thus the order of the arguments to printf(). For example,the French tend to put the adjective after rather than before the noun (as we doin English). We say “the red ballo<strong>on</strong>” and they say “le ballo<strong>on</strong> rouge.” If yourprogram had Strings for adjective and noun, then a printf() like this:String format = "the %s %s\n";System.out.printf(format, adjective, noun);wouldn’t work if you translate just the format String:String format = "le %s %s\n";System.out.printf(format, noun, adjective);You’d like to be able to do the translati<strong>on</strong> without changing the code in yourprogram. 8 With the Java versi<strong>on</strong> of printf(), there is syntax for specifyingwhich argument corresp<strong>on</strong>ds to which format field in the format string. It uses7. Remember, you’ll need the -source 5.0 opti<strong>on</strong> <strong>on</strong> the command line.8. Java has good support for internati<strong>on</strong>alizati<strong>on</strong>, another topic for which we d<strong>on</strong>’t have thetime. The ability to translate the strings without otherwise modifying the program is a crucialpart to internati<strong>on</strong>alizati<strong>on</strong>, and the printf() in Java 5.0 is certainly a help in this regard. Ina similar vein, the Eclipse IDE, covered in Chapter 10, includes a feature to take all stringc<strong>on</strong>stants and c<strong>on</strong>vert them to external properties at a stroke, making internati<strong>on</strong>alizati<strong>on</strong> mucheasier to do.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!