Java Cookbook, 4th Edition by Ian F. Darwin
Author:Ian F. Darwin [Ian F. Darwin]
Language: eng
Format: epub
Publisher: O'Reilly Media, Inc.
Published: 2020-04-24T16:00:00+00:00
Figure 3-1. String, StringBuilder, and C-language strings
Although we haven’t discussed the details of the java.io package yet (we will, in [Link to Come]), you need to be able to read text files for some of these programs. Even if you’re not familiar with java.io, you can probably see from the examples of reading text files that a BufferedReader allows you to read “chunks” of data, and that this class has a very convenient readLine() method.
Going the other way, System.out.println() is normally used to print strings or other values to the terminal or “standard output.” String concatenation is commonly used here, as in:
System.out.println("The answer is " + result);
One caveat with string concatenation is that if you are appending a bunch of things, and a number and a character are concatenated at the front, they are added before concatenation due to Java’s precedence rules. So don’t do as I did in this contrived example:
int result = ...; System.out.println(result + '=' + " the answer.");
Given that result is an integer, then result + '=' (result added to the equals sign, which is of the numeric type char) is a valid numeric expression, which will result in a single value of type int. If the variable result has the value 42, and given that the character = in a Unicode (or ASCII) code chart has the value 61, this prints:
103 the answer.
The wrong value and no equals sign! Safer approaches include using parentheses, using double quotes around the equals sign, using a StringBuilder (see Recipe 3.2) or a MessageFormat (see [Link to Come]), or using String.format() (see [Link to Come]). Of course in this simple example you could just move the = to be part of the string literal, but the example was chosen to illustrate the problem of arithmetic on char values being confused with string contatenation. I won’t show you how to sort an array of strings here; the more general notion of sorting a collection of objects will be taken up in [Link to Come].
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(178252)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(86745)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(86339)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(86131)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74442)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50895)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40265)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40217)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40099)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32736)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32511)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32456)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32391)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32366)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32333)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32260)
What's Done in Darkness by Kayla Perrin(27152)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26533)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26461)