You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different code fragments, without losing your place, while you are reading about them.

Introduction

This is the third in a series of lessons designed to teach you about tuples.

The syntax for creating a tuple with only one element is rather ugly, but is required to avoid ambiguity. In particular, it is necessary to follow the single tuple item with a comma as shown in the first boldface line in Listing 3.

Why is this comma necessary?

Had I written that line simply as follows without the extra comma,

t2 = "a"

the result would have been to create a new variable named t2 whose contents would be the string "a".

Not a tuple

This would not indicate a tuple at all. Thus, the extra comma is required to make a single-item tuple unique and to distinguish it from other possibilities.

Output for the single-item tuple

Listing 4 shows the output produced by the code in Listing 3. The single-item tuple is shown in the first boldface line. As is always the case, the tuple is displayed in parentheses.

However unlike previous sample programs, in this case, literal parentheses are used to cause the tuple named t2 to be doubly nested.

In particular, as shown by the first boldface portion of code in Listing 5, the tuple named t2 and the string "Z" are used to create a tuple, which in turn, is nested in the tuple assigned to the variable named t3.

What does a doubly-nested tuple look like?

The double nesting is evidenced by the extra parentheses in the boldface portion of the output shown in Listing 6.

Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.