プリンキピア

第7章高水準プログラミング言語

Complex Data Types複合データ型
We have learned that some of the data types of a language are primitive types, meaning that data of that type can be directly represented in the registers and memory locations of the machine. However, languages usually contain complex data types as well.
英語のヒント
言語のデータ型の一部は基本型であり、その型のデータは、マシンのレジスタやメモリ位置で直接表せると学んだ。ただし、通常、言語には複合データ型もある。
A complex data type is one consisting of multiple primitive types used as its building blocks which is why we also call them composite types. These multiple types may be of the same type, as in a complex data type known as an array, or they may consist of collections of different data types in one construct, such as a C# class.
英語のヒント
複合データ型は、複数の基本型を部品として構成される型であり、そのため合成型とも呼ぶ。配列という複合データ型のように、構成する型がすべて同じ場合もあれば、C#のクラスのように、一つの構造の中に、異なるデータ型の集まりを含む場合もある。
Arrays
配列
An array is a typical composite type that is used as a data container. A great way to visualize an array is as a shelf unit, a connected structure where we can place items on each element. An instance of an array in this case could be a bookshelf that is meant to contain books (a book would be another composite type).
英語のヒント
配列は、データの入れ物として使う、典型的な合成型である。配列を思い浮かべるには、各部分へ物を置ける、つながった構造である棚にたとえるとよい。この場合、本を収めるための本棚が、配列の一つの実体に当たる。本そのものも、別の合成型である。
An array is a named variable that references a block of contiguous memory locations, and each “shelf” of the array is an element, which occupies exactly as many of the contiguous bytes as it takes to accommodate a value of the data type being stored. In the simplest type of array structure, an indexed array, the shelves are numbered with an index, starting at zero, or the lowest memory location. In a strongly typed language, all elements of an array must be of the same data type which means that every element will be of a uniform length in bytes.
英語のヒント
配列は、連続したメモリ位置のまとまりを参照する、名前を持つ変数である。配列の各「棚」を要素と呼ぶ。各要素は、保存するデータ型の値を収めるために必要な分だけ、連続するバイトを占める。最も単純な配列構造である添字付き配列では、棚に添字で番号を付け、0、すなわち最も低いメモリ位置から始める。強い型付けの言語では、配列の全要素を同じデータ型にする必要がある。つまり、どの要素も、バイト数で測った長さが同じになる。
Figure 7.10 illustrates an array in any number of HLLs including Java, C, and C#. We start off with the array declaration, which gives the data type of each of its elements, names the array variable numbers, indicates it is an array with the opening and closing square brackets ([]), and assigns five values to it with what is known as an array initializer (values separated by commas placed between curly braces). We can see that the length of the array is 5, the indexes run from 0 to 4, and each of the elements are contiguous in memory and are 4 bytes in length. The following statement assigns an element of the array to a variable:
英語のヒント
図7.10は、Java、C、C#など、多くのHLLにある配列を示す。まず配列宣言から始める。これは、各要素のデータ型を示し、配列変数をnumbersと名付け、開き角括弧と閉じ角括弧([])で配列であることを示し、配列初期化子によって五つの値を与える。初期化子は、波括弧の間に、コンマで区切った値を置いたものである。配列の長さは5、添字は0から4で、各要素はメモリ上で連続し、長さは4バイトであることが分かる。次の文は、配列の一つの要素を変数へ代入する。
int myNumber = numbers[5];
Figure 7.10

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 7.10 Each value in an array is assigned an index and a memory address. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.10 配列内の各値には、添字とメモリアドレスが割り当てられる。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
Strings
文字列
Strings, which are another composite data type, are arrays of characters in most HLLs. In object-oriented languages, they are quite a bit more complex. They are implemented in some HLLs as an array which holds individual characters as its elements. The OOP languages usually implement strings as objects with built-in functions (methods). Here is an example of a string in Java:
英語のヒント
もう一つの合成データ型である文字列は、HLLの大半では文字の配列である。オブジェクト指向言語では、かなり複雑になる。一部のHLLでは、個々の文字を要素として持つ配列として実装する。OOP言語では、通常、組み込み関数、つまりメソッドを持つオブジェクトとして実装する。次は、Javaの文字列の例である。
String myUniversity = "Union Technical College";
Reference Types and Pointers
参照型とポインター
In our study of variables and data types so far, we have examined the concept of a variable being a name-value pair. With primitive types, the memory location referenced by the name stores the actual value of the variable directly at that spot.
英語のヒント
ここまでの変数とデータ型の学習では、変数を、名前と値の組として見る考え方を調べてきた。基本型では、名前が参照するメモリ位置に、その変数の実際の値を直接保存する。
In the case of complex data types, things are not quite so simple. Let us take the example of a string. We can store a name in the string such as “Jimmy” to start. Now let us say that we change the value to “Johnathan” at some later time. The memory required to store the value has now changed and perhaps it will no longer fit in the original location. We have learned that the C language has a primitive data type known as a pointer. It is used for that reason.
英語のヒント
複合データ型では、それほど単純ではない。文字列の例を考えよう。最初に「Jimmy」のような名前を文字列へ保存する。その後、値を「Johnathan」に変えたとしよう。必要なメモリ量が変わり、元の場所には収まらなくなるかもしれない。C言語には、ポインターという基本データ型があると学んだ。ポインターは、このような理由で使う。
Pointers are variables that hold actual computer memory addresses (references). They match the word size of the machine, which is typically 64 bits. We call a variable that holds memory addresses a reference variable. There is no such thing in Java or C. Therefore, when we create an array or a string in these languages, the value that is actually stored in the variable is the memory address of the place where the complex object exists. So, in the case of our string example, if we change the name, we can just change the value in its variable to be a different memory address—refer to Figure 7.3.
英語のヒント
ポインターは、コンピューターの実際のメモリアドレス、つまり参照を保持する変数である。大きさはマシンのワード長と一致し、通常は64ビットである。メモリアドレスを保持する変数を、参照変数という。JavaやCには、そのようなものはない。したがって、これらの言語で配列や文字列を作るとき、変数に実際に保存する値は、その複合オブジェクトが存在する場所のメモリアドレスになる。先ほどの文字列の例なら、名前を変えるときは、その変数の値を、別のメモリアドレスへ変えるだけでよい。図7.3を参照しよう。