Skip to Content
Java クックブック、第 4 版
book

Java クックブック、第 4 版

by Ian F. Darwin
May 2025
Intermediate to advanced
638 pages
10h 22m
Japanese
O'Reilly Media, Inc.
Content preview from Java クックブック、第 4 版

第3章. 文字列とモノ

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

3.0 はじめに

文字列は、あらゆるプログラミング・タスクの必然的な一部である。ユーザへのメッセージの印刷、ディスクやその他の外部メディア上のファイルの名前付け、人の名前、住所、所属などだ。文字列の用途はたくさんあり、ほとんど数え切れないほどだ(実際、数字が必要な場合は第5章で説明する)。

C言語のようなプログラミング言語から来た人は、String 、Javaでは定義された型(クラス)であることを覚えておく必要があるだろう。つまり、文字列はオブジェクトであり、メソッドを持っている。文字列は文字の配列ではないので(コンテナはあるが)、配列と考えるべきではない。fileName.endsWith(".gif") 」や extension.equals(".gif")(および等価性".gif".equals(extension) )のような操作はありふれたものである。1

Java 11と12では、 indent(int n) ,stripLeading()stripTrailing(),Stream<T> lines() ,isBlank(),transform() など、いくつかの新しいString メソッドが追加されたことに、Javaの旧来からのユーザーは注意する必要がある。 これらのほとんどは、明らかな機能を提供する。最後のメソッドは、文字列に関数インターフェースのインスタンス(レシピ9.0を参照)を適用し、その演算結果を返すことができる。

java.io パッケージの詳細についてはまだ説明していないが(第10章で詳し く説明する)、これらのプログラミングの一部ではテキスト・ファイルを読み取れ る必要がある。java.io をよく知らなくても、テキスト・ファイルを読み取る例から、BufferedReader でデータの塊を読み取ることができること、そしてこのクラスがとても便利な readLine() メソッドを持っていることがわかるだろう。

逆に、System.out.println() は通常、文字列やその他の値をターミナルや標準出力に出力するために使われる。ここでは、文字列の連結がよく使われる:

System.out.println("The answer is " + result);

文字列連結の注意点として、いくつもの文字列を連結する場合、数字と文字が先頭に連結されると、Javaの優先順位規則により、連結の前に追加されてしまう。だから、この作為的な例で私がやったようにはしないことだ:

int result = ...;
System.out.println(result + '=' + " the answer.");

result が整数であるとすると、次のようになる。 result + '=' (result added to the equals sign, which is of the numeric type +char)は有効な 数値式であり、int 型の1つの値になる。変数result が値42を持ち、ユニコード(またはASCII)コード表の文字= が値61を持つとすると、2行の断片が出力される:

103 the answer.

間違った値で等号がない!より安全な方法としては、括弧を使う、等号を二重引用符で囲む、StringBuilderレシピ3.2参照)、 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

最新の Java レシピ

最新の Java レシピ

Ken Kousen
Javaによる関数型プログラミング ―Java 8ラムダ式とStream

Javaによる関数型プログラミング ―Java 8ラムダ式とStream

Venkat Subramaniam, 株式会社プログラミングシステム社
MySQL クックブック、第 4 版

MySQL クックブック、第 4 版

Sveta Smirnova, Alkin Tezuysal

Publisher Resources

ISBN: 9798341650336