{"id":3441,"date":"2019-06-20T11:41:45","date_gmt":"2019-06-20T11:41:45","guid":{"rendered":"http:\/\/softlect.in\/?p=3441"},"modified":"2019-06-20T11:56:36","modified_gmt":"2019-06-20T11:56:36","slug":"java-data-types-and-variables","status":"publish","type":"post","link":"http:\/\/softlect.com\/index.php\/java-data-types-and-variables\/","title":{"rendered":"Java Data Types and Variables"},"content":{"rendered":"<h1><a name=\"_Toc179308602\"><\/a><strong>Data Types, Variables, and Arrays<\/strong><\/h1>\n<p>&nbsp;<\/p>\n<p>Java is a Strongly Typed Language which means that in Java every variable has a type, every expression has a type, and every type is strictly defined. All assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. The Java compiler checks all expressions and parameters to ensure that the types are compatible. Any type mismatches are errors that must be corrected before the compiler will finish compiling the class. For example, in C\/C++ you can assign a floating-point value to an integer. In Java, you cannot. .<\/p>\n<p><em>\u00a0<\/em><\/p>\n<h2><a name=\"_Toc179308603\"><\/a><strong>The Simple Types<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Java defines eight simple types of data: <strong>byte<\/strong>, <strong>short<\/strong>, <strong>int<\/strong>, <strong>long<\/strong>, <strong>char<\/strong>, <strong>float<\/strong>, <strong>double<\/strong>, and <strong>boolean<\/strong>. These can be put in four groups:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Integers: This group includes <strong>byte<\/strong>, <strong>short<\/strong>, <strong>int<\/strong>, and <strong>long<\/strong>, which are for whole valued signed numbers.<\/li>\n<li>Floating-point numbers: This group includes <strong>float <\/strong>and <strong>double<\/strong>, which represent numbers with fractional precision.<\/li>\n<li>Characters: This group includes <strong>char<\/strong>, which represents symbols in a character set, like letters and numbers.<\/li>\n<li>Boolean: This group includes <strong>boolean<\/strong>, which is a special type for representing true\/false values.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>They form the basis for all other types of data that you can create. The simple types are defined to have an explicit range and mathematical behavior. Because of Java\u2019s portability requirement, all data types have a strictly defined range. For example, an <strong>int <\/strong>is always 32 bits, regardless of the particular platform.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308604\"><\/a><strong>Integers<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Java defines four integer types: <strong>byte<\/strong>, <strong>short<\/strong>, <strong>int<\/strong>, and <strong>long<\/strong>. All of these are signed, positive and negative values. Java does not support unsigned or positive-only integers. The width and ranges of these integer types vary widely, as shown in this table:<\/p>\n<p>&nbsp;<\/p>\n<table width=\"90%\">\n<tbody>\n<tr>\n<td width=\"79\"><strong>Name<\/strong><\/td>\n<td width=\"53\"><strong>Width<\/strong><\/td>\n<td width=\"380\"><strong>Range<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"79\">long<\/td>\n<td width=\"53\">64<\/td>\n<td width=\"380\">\u20139,223,372,036,854,775,808 to 9,223,372,036,854,775,807<\/td>\n<\/tr>\n<tr>\n<td width=\"79\">int<\/td>\n<td width=\"53\">32<\/td>\n<td width=\"380\">\u20132,147,483,648 to 2,147,483,647<\/td>\n<\/tr>\n<tr>\n<td width=\"79\">short<\/td>\n<td width=\"53\">16<\/td>\n<td width=\"380\">\u201332,768 to 32,767<\/td>\n<\/tr>\n<tr>\n<td width=\"79\">byte<\/td>\n<td width=\"53\">8<\/td>\n<td width=\"380\">\u2013128 to 127<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><\/h3>\n<h3><a name=\"_Toc179308605\"><\/a>byte<\/h3>\n<p>The smallest integer type is <strong>byte<\/strong>. This is a signed 8-bit type that has a range from \u2013128 to 127. Variables of type <strong>byte <\/strong>are especially useful when you\u2019re working with a stream of data from a network or file. Byte variables are declared by use of the <strong>byte <\/strong>keyword. For example, the following declares two <strong>byte <\/strong>variables called <strong>b <\/strong>and <strong>c<\/strong>:<\/p>\n<p>byte b, c;<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308606\"><\/a>short<\/h3>\n<p>short is a signed 16-bit type. It has a range from \u201332,768 to 32,767. Here are some examples of short variable declarations:<\/p>\n<p>short s;<\/p>\n<p>short t;<\/p>\n<p><em>\u00a0<\/em><\/p>\n<h3><a name=\"_Toc179308607\"><\/a>int<\/h3>\n<p>The most commonly used integer type is <strong>int<\/strong>. It is a signed 32-bit type that has a range from \u20132,147,483,648 to 2,147,483,647. In addition to other uses, variables of type <strong>int <\/strong>are commonly employed to control loops and to index arrays. Any time you have an integer expression involving <strong>byte<\/strong>s, <strong>short<\/strong>s, <strong>int<\/strong>s, and literal numbers, the entire expression is <em>promoted <\/em>to <strong>int <\/strong>before the calculation is done. The <strong>int <\/strong>type is the most efficient type, and it should be used most of the time when you want to create a number for counting or indexing arrays or doing integer math.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308608\"><\/a>Long<\/h3>\n<p><strong>long <\/strong>is a signed 64-bit type and is useful for those occasions where an <strong>int <\/strong>type is not large enough to hold the desired value. The range of a <strong>long <\/strong>is quite large. This makes it useful when big, whole numbers are needed. For example, here is a program that computes the number of miles that light will travel in a specified number of days.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Compute distance light travels using long variables.<\/p>\n<p>class Light {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int lightspeed;<\/p>\n<p>long days;<\/p>\n<p>long seconds;<\/p>\n<p>long distance;<\/p>\n<p>\/\/ approximate speed of light in miles per second<\/p>\n<p>lightspeed = 186000;<\/p>\n<p>days = 1000; \/\/ specify number of days here<\/p>\n<p>seconds = days * 24 * 60 * 60; \/\/ convert to seconds<\/p>\n<p>distance = lightspeed * seconds; \/\/ compute distance<\/p>\n<p>System.out.print(&#8220;In &#8221; + days);<\/p>\n<p>System.out.print(&#8221; days light will travel about &#8220;);<\/p>\n<p>System.out.println(distance + &#8221; miles.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program generates the following output:<\/p>\n<p>In 1000 days light will travel about 16070400000000 miles. Clearly, the result could not have been held in an <strong>int <\/strong>variable.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308609\"><\/a><strong>Floating-Point Types<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Floating-point numbers, also known as <em>real <\/em>numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, sine and cosine, result in a value whose precision requires a floating-point type. There are two kinds of floating-point types, <strong>float <\/strong>and <strong>double<\/strong>, which represent single and double-precision numbers, respectively. Their width and ranges are shown here:<\/p>\n<p>&nbsp;<\/p>\n<table width=\"90%\">\n<tbody>\n<tr>\n<td width=\"79\"><strong>Name<\/strong><\/td>\n<td width=\"54\"><strong>Width<\/strong><\/td>\n<td width=\"378\"><strong>Range<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"79\">double<\/td>\n<td width=\"54\">64<\/td>\n<td width=\"378\">4.9e\u2013324 to 1.8e+308<\/td>\n<\/tr>\n<tr>\n<td width=\"79\">float<\/td>\n<td width=\"54\">32<\/td>\n<td width=\"378\">1.4e\u2212045 to 3.4e+038<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308610\"><\/a>float<\/h3>\n<p>The type <strong>float <\/strong>specifies a <em>single-precision <\/em>value that uses 32 bits of storage. Variables of type <strong>float <\/strong>are useful when you need a fractional component. For example, <strong>float <\/strong>can be useful when representing dollars and cents. Here are some example <strong>float <\/strong>variable declarations:<\/p>\n<p>float hightemp, lowtemp;<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308611\"><\/a>double<\/h3>\n<p>Double precision, as denoted by the <strong>double <\/strong>keyword, uses 64 bits to store a value. All math functions, such as <strong>sin( )<\/strong>, <strong>cos( )<\/strong>, and <strong>sqrt( )<\/strong>, return <strong>double <\/strong>values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, <strong>double <\/strong>is the best choice. Here is a short program that uses <strong>double <\/strong>variables to compute the area of a circle:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Compute the area of a circle.<\/p>\n<p>class Area {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>double pi, r, a;<\/p>\n<p>r = 10.8; \/\/ radius of circle<\/p>\n<p>pi = 3.1416; \/\/ pi, approximately<\/p>\n<p>a = pi * r * r; \/\/ compute area<\/p>\n<p>System.out.println(&#8220;Area of circle is &#8221; + a);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308612\"><\/a><strong>Characters<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>In Java, the data type used to store characters is <strong>char<\/strong>. Java uses Unicode to represent characters. <em>Unicode <\/em>defines a fully international character set that can represent all of the characters found in all human languages. For this purpose, it requires 16 bits. Thus, in Java <strong>char <\/strong>is a 16-bit type. The range of a <strong>char <\/strong>is 0 to 65,536. There are no negative <strong>char<\/strong>s. The standard set of characters known as ASCII still ranges from 0 to 127. Since Java is designed to allow applets to be written for worldwide use, it makes sense that it would use Unicode to represent characters. Here is a program that demonstrates <strong>char <\/strong>variables:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate char data type.<\/p>\n<p>class CharDemo {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>char ch1, ch2;<\/p>\n<p>ch1 = 88; \/\/ code for X<\/p>\n<p>ch2 = &#8216;Y&#8217;;<\/p>\n<p>System.out.print(&#8220;ch1 and ch2: &#8220;);<\/p>\n<p>System.out.println(ch1 + &#8221; &#8221; + ch2);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program displays the following output:<\/p>\n<p>ch1 and ch2: X Y<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that <strong>ch1 <\/strong>is assigned the value 88, which is the ASCII and Unicode value that corresponds to the letter <em>X. <\/em>As mentioned, the ASCII character set occupies the first 127 values in the Unicode character set. Even though <strong>char<\/strong>s are not integers, in many cases you can operate on them as if they were integers. This allows you to add two characters together, or to increment the value of a character variable. For example, consider the following program:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ char variables behave like integers.<\/p>\n<p>class CharDemo2 {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>char ch1;<\/p>\n<p>ch1 = &#8216;X&#8217;;<\/p>\n<p>System.out.println(&#8220;ch1 contains &#8221; + ch1);<\/p>\n<p>ch1++; \/\/ increment ch1<\/p>\n<p>System.out.println(&#8220;ch1 is now &#8221; + ch1);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output generated by this program is shown here:<\/p>\n<p>ch1 contains X<\/p>\n<p>ch1 is now Y<\/p>\n<p>&nbsp;<\/p>\n<p>In the program, <strong>ch1 <\/strong>is first given the value <em>X. <\/em>Next, <strong>ch1 <\/strong>is incremented. This results in <strong>ch1 <\/strong>containing <em>Y, <\/em>the next character in the ASCII and Unicode sequence.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308613\"><\/a><strong>Booleans<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Java has a simple type, called <strong>boolean<\/strong>, for logical values. It can have only one of two possible values, <strong>true <\/strong>or <strong>false<\/strong>. This is the type returned by all relational operators, such as <strong>a &lt; b<\/strong>. <strong>boolean <\/strong>is also the type <em>required <\/em>by the conditional expressions that govern the control statements such as <strong>if <\/strong>and <strong>for<\/strong>. Here is a program that demonstrates the <strong>boolean <\/strong>type:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate boolean values.<\/p>\n<p>class BoolTest {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>boolean b;<\/p>\n<p>b = false;<\/p>\n<p>System.out.println(&#8220;b is &#8221; + b);<\/p>\n<p>b = true;<\/p>\n<p>System.out.println(&#8220;b is &#8221; + b);<\/p>\n<p>\/\/ a boolean value can control the if statement<\/p>\n<p>if(b) System.out.println(&#8220;This is executed.&#8221;);<\/p>\n<p>b = false;<\/p>\n<p>if(b) System.out.println(&#8220;This is not executed.&#8221;);<\/p>\n<p>\/\/ outcome of a relational operator is a boolean value<\/p>\n<p>System.out.println(&#8220;10 &gt; 9 is &#8221; + (10 &gt; 9));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output generated by this program is shown here:<\/p>\n<p>b is false<\/p>\n<p>b is true<\/p>\n<p>This is executed.<\/p>\n<p>10 &gt; 9 is true<\/p>\n<p>&nbsp;<\/p>\n<p>when a <strong>boolean <\/strong>value is output by <strong>println( )<\/strong>, \u201ctrue\u201d or \u201cfalse\u201d is displayed. Second, the value of a <strong>boolean <\/strong>variable is sufficient, by itself, to control the <strong>if <\/strong>statement. There is no need to write an <strong>if <\/strong>statement like this:<\/p>\n<p>&nbsp;<\/p>\n<p>if(b == true) &#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>The outcome of a relational operator, such as <strong>&lt;<\/strong>, is a <strong>boolean <\/strong>value. This is why the expression <strong>10 &gt; 9 <\/strong>displays the value \u201ctrue.\u201d<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308614\"><\/a><strong>String Literals<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes. Examples of string literals are<\/p>\n<p>\u201cHello World\u201d<\/p>\n<p>\u201ctwo\\nlines\u201d<\/p>\n<p>\u201c\\\u201dThis is in quotes\\\u201d\u201d<\/p>\n<p>&nbsp;<\/p>\n<p>Table 3-1 shows the character escape sequences. The escape sequences work the same way inside of string literals. One important thing to note about Java strings is that they must begin and end on the same line. There is no line-continuation escape sequence as there is in other languages.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<table width=\"90%\">\n<tbody>\n<tr>\n<td width=\"159\"><strong>Escape Sequence<\/strong><\/td>\n<td width=\"353\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\ddd<\/td>\n<td width=\"353\">Octal character (ddd)<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\uxxxx<\/td>\n<td width=\"353\">Hexadecimal UNICODE character (xxxx)<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\\u2019<\/td>\n<td width=\"353\">Single quote<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\\u201d<\/td>\n<td width=\"353\">Double quote<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\\\<\/td>\n<td width=\"353\">Backslash<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\r<\/td>\n<td width=\"353\">Carriage return<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\n<\/td>\n<td width=\"353\">New line (also known as line feed)<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\f<\/td>\n<td width=\"353\">Form feed<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\t<\/td>\n<td width=\"353\">Tab<\/td>\n<\/tr>\n<tr>\n<td width=\"159\">\\b<\/td>\n<td width=\"353\">Backspace<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\" width=\"512\">Table 3-1. Character Escape Sequences<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308615\"><\/a><strong>Variables<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer. In addition, all variables have a scope, which defines their visibility, and a lifetime.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308616\"><\/a>Declaring a Variable<\/h3>\n<p>In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:<\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>type identifier <\/em>[ = <em>value<\/em>][, <em>identifier <\/em>[= <em>value<\/em>] &#8230;] ;<\/p>\n<p>&nbsp;<\/p>\n<p>The <em>type <\/em>is one of Java\u2019s types, or the name of a class or interface. The <em>identifier <\/em>is the name of the variable. You can initialize the variable by specifying an equal sign and a value. the initialization expression must result in a value of the same or compatible type as that specified for the variable. To declare more than one variable of the specified type, use a comma-separated list. Here are several examples of variable declarations of various types. Note that some include an initialization.<\/p>\n<p>&nbsp;<\/p>\n<p>int a, b, c; \/\/ declares three ints, a, b, and c.<\/p>\n<p>int d = 3, e, f = 5; \/\/ declares three more ints, initializing<\/p>\n<p>\/\/ d and f.<\/p>\n<p>byte z = 22; \/\/ initializes z.<\/p>\n<p>double pi = 3.14159; \/\/ declares an approximation of pi.<\/p>\n<p>char x = &#8216;x&#8217;; \/\/ the variable x has the value &#8216;x&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308617\"><\/a>Dynamic Initialization<\/h3>\n<p>Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared. For example, here is a short program that computes the length of the hypotenuse of a right triangle given the lengths of its two opposing sides:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate dynamic initialization.<\/p>\n<p>class DynInit {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>double a = 3.0, b = 4.0;<\/p>\n<p>\/\/ c is dynamically initialized<\/p>\n<p>double c = Math.sqrt(a * a + b * b);<\/p>\n<p>System.out.println(&#8220;Hypotenuse is &#8221; + c);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here, three local variables <strong>a<\/strong>, <strong>b<\/strong>,and <strong>c <\/strong>are declared. The first two, <strong>a <\/strong>and <strong>b<\/strong>, are initialized by constants. However, <strong>c <\/strong>is initialized dynamically to the length of the hypotenuse. The program uses another of Java\u2019s built-in methods, <strong>sqrt( )<\/strong>, which is a member of the <strong>Math <\/strong>class, to compute the square root of its argument. The key point here is that the initialization expression may use any element valid at the time of the initialization, including calls to methods, other variables, or literals.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308618\"><\/a><strong>The Scope and Lifetime of Variables<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Java allows variables to be declared within any block. A block is begun with an opening curly brace and ended by a closing curly brace. A block defines a <em>scope. <\/em>Thus, each time you start a new block, you are creating a new scope. A scope determines what objects are visible to other parts of your program. It also determines the lifetime of those objects. The scope defined by a method begins with its opening curly brace. However, if that method has parameters, they too are included within the method\u2019s scope. Variables declared inside a scope are not visible that is, accessible to code that is defined outside that scope. Thus, when you declare a variable within a scope, you are localizing that variable and protecting it from unauthorized access and\/or modification. Indeed, the scope rules provide the foundation for encapsulation. Scopes can be nested. For example, each time you create a block of code, you are creating a new, nested scope. When this occurs, the outer scope encloses the inner scope. This means that objects declared in the outer scope will be visible to code within the inner scope. However, the reverse is not true. Objects declared within the inner scope will not be visible outside it. To understand the effect of nested scopes, consider the following program:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate block scope.<\/p>\n<p>class Scope {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int x; \/\/ known to all code within main<\/p>\n<p>x = 10;<\/p>\n<p>if(x == 10) { \/\/ start new scope<\/p>\n<p>int y = 20; \/\/ known only to this block<\/p>\n<p>\/\/ x and y both known here.<\/p>\n<p>System.out.println(&#8220;x and y: &#8221; + x + &#8221; &#8221; + y);<\/p>\n<p>x = y * 2;<\/p>\n<p>}<\/p>\n<p>\/\/ y = 100; \/\/ Error! y not known here<\/p>\n<p>\/\/ x is still known here.<\/p>\n<p>System.out.println(&#8220;x is &#8221; + x);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The variable <strong>x <\/strong>is declared at the start of <strong>main( )<\/strong>\u2019s scope and is accessible to all subsequent code within <strong>main( )<\/strong>. Within the <strong>if <\/strong>block, <strong>y <\/strong>is declared. Since a block defines a scope, <strong>y <\/strong>is only visible to other code within its block. This is why outside of its block, the line <strong>y = 100; <\/strong>is commented out. If you remove the leading comment symbol, a compile-time error will occur, because <strong>y <\/strong>is not visible outside of its block. Within the <strong>if <\/strong>block, <strong>x <\/strong>can be used because code within a block that is, a nested scope has access to variables declared by an enclosing scope. Within a block, variables can be declared at any point, but are valid only after they are declared. Thus, if you define a variable at the start of a method, it is available to all of the code within that method. For example, this fragment is invalid because <strong>count <\/strong>cannot be used prior to its declaration:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ This fragment is wrong!<\/p>\n<p>count = 100; \/\/ oops! cannot use count before it is declared!<\/p>\n<p>int count;<\/p>\n<p>&nbsp;<\/p>\n<p>Variables are created when their scope is entered, and destroyed when their scope is left. This means that a variable will not hold its value once it has gone out of scope. Therefore, variables declared within a method will not hold their values between calls to that method. Also, a variable declared within a block will lose its value when the block is left. Thus, the lifetime of a variable is confined to its scope. If a variable declaration includes an initializer, then that variable will be reinitialized each time the block in which it is declared is entered. For example, consider the next program.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate lifetime of a variable.<\/p>\n<p>class LifeTime {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int x;<\/p>\n<p>for(x = 0; x &lt; 3; x++) {<\/p>\n<p>int y = -1; \/\/ y is initialized each time block is entered<\/p>\n<p>System.out.println(&#8220;y is: &#8221; + y); \/\/ this always prints -1<\/p>\n<p>y = 100;<\/p>\n<p>System.out.println(&#8220;y is now: &#8221; + y);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output generated by this program is shown here:<\/p>\n<p>y is: -1<\/p>\n<p>y is now: 100<\/p>\n<p>y is: -1<\/p>\n<p>y is now: 100<\/p>\n<p>y is: -1<\/p>\n<p>y is now: 100<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, <strong>y <\/strong>is always reinitialized to \u20131 each time the inner <strong>for <\/strong>loop is entered. Even though it is subsequently assigned the value 100, this value is lost. Although blocks can be nested, you cannot declare a variable to have the same name as one in an outer scope. Here is an example that tries to declare two separate variables with the same name. In Java, this is illegal.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ This program will not compile<\/p>\n<p>class ScopeErr {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int bar = 1;<\/p>\n<p>{ \/\/ creates a new scope<\/p>\n<p>int bar = 2; \/\/ Compile-time error \u2013 bar already defined!<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308619\"><\/a><strong>Type Conversion and Casting<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign an <strong>int <\/strong>value to a <strong>long <\/strong>variable. However, not all types are compatible, and thus, not all type conversions are implicitly allowed. For instance, there is no conversion defined from <strong>double <\/strong>to <strong>byte<\/strong>. it is still possible to obtain a conversion between incompatible types. To do so, you must use a <em>cast, <\/em>which performs an explicit conversion between incompatible types.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308620\"><\/a>Java\u2019s Automatic Conversions<\/h3>\n<p>When one type of data is assigned to another type of variable, an <em>automatic type conversion <\/em>will take place if the following two conditions are met:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>The two types are compatible.<\/li>\n<li>The destination type is larger than the source type.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>When these two conditions are met, a <em>widening conversion <\/em>takes place. For example, the <strong>int <\/strong>type is always large enough to hold all valid <strong>byte <\/strong>values, so no explicit cast statement is required. For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other. However, the numeric types are not compatible with <strong>char <\/strong>or <strong>boolean<\/strong>. Also, <strong>char <\/strong>and <strong>boolean <\/strong>are not compatible with each other.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308621\"><\/a>Casting Incompatible Types<\/h3>\n<p>Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign an <strong>int <\/strong>value to a <strong>byte <\/strong>variable? This conversion will not be performed automatically, because a <strong>byte <\/strong>is smaller than an <strong>int<\/strong>. This kind of conversion is sometimes called a <em>narrowing conversion, <\/em>since you are explicitly making the value narrower so that it will fit into the target type. To create a conversion between two incompatible types, you must use a cast. A <em>cast <\/em>is simply an explicit type conversion. It has this general form:<\/p>\n<p>&nbsp;<\/p>\n<p>(<em>target<\/em>&#8211;<em>type<\/em>) <em>value<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>target-type <\/em>specifies the desired type to convert the specified value to. For example, the following fragment casts an <strong>int <\/strong>to a <strong>byte<\/strong>. If the integer\u2019s value is larger than the range of a <strong>byte<\/strong>, it will be reduced to <strong>byte<\/strong>\u2019s range.<\/p>\n<p>&nbsp;<\/p>\n<p>int a;<\/p>\n<p>byte b;<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>b = (byte) a;<\/p>\n<p>&nbsp;<\/p>\n<p>A different type of conversion will occur when a floating-point value is assigned to an integer type: <em>truncation<\/em>. As you know, integers do not have fractional components. Thus, when a floating-point value is assigned to an integer type, the fractional component is lost. For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. The 0.23 will have been truncated. Of course, if the size of the whole number component is too large to fit into the target integer type, then that value will be reduced. The following program demonstrates some type conversions that require casts:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate casts.<\/p>\n<p>class Conversion {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>byte b;<\/p>\n<p>int i = 257;<\/p>\n<p>double d = 323.142;<\/p>\n<p>System.out.println(&#8220;\\nConversion of int to byte.&#8221;);<\/p>\n<p>b = (byte) i;<\/p>\n<p>System.out.println(&#8220;i and b &#8221; + i + &#8221; &#8221; + b);<\/p>\n<p>System.out.println(&#8220;\\nConversion of double to int.&#8221;);<\/p>\n<p>i = (int) d;<\/p>\n<p>System.out.println(&#8220;d and i &#8221; + d + &#8221; &#8221; + i);<\/p>\n<p>System.out.println(&#8220;\\nConversion of double to byte.&#8221;);<\/p>\n<p>b = (byte) d;<\/p>\n<p>System.out.println(&#8220;d and b &#8221; + d + &#8221; &#8221; + b);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program generates the following output:<\/p>\n<p>&nbsp;<\/p>\n<p>Conversion of int to byte.<\/p>\n<p>i and b 257 1<\/p>\n<p>Conversion of double to int.<\/p>\n<p>d and i 323.142 323<\/p>\n<p>Conversion of double to byte.<\/p>\n<p>d and b 323.142 67<\/p>\n<p>&nbsp;<\/p>\n<p>When the value 257 is cast into a <strong>byte <\/strong>variable, the result is the remainder of the division of 257 by 256 (the range of a <strong>byte<\/strong>), which is 1 in this case. When the <strong>d <\/strong>is converted to an <strong>int<\/strong>, its fractional component is lost. When <strong>d <\/strong>is converted to a <strong>byte<\/strong>, its fractional component is lost, <em>and <\/em>the value is reduced modulo 256, which in this case is 67.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308622\"><\/a>Automatic Type Promotion in Expressions<\/h3>\n<p>In addition to assignments, there is another place where certain type conversions may occur: in expressions. In an expression, the precision required of an intermediate value will some times exceed the range of either operand. For example, examine the following expression:<\/p>\n<p>&nbsp;<\/p>\n<p>byte a = 40;<\/p>\n<p>byte b = 50;<\/p>\n<p>byte c = 100;<\/p>\n<p>int d = a * b \/ c;<\/p>\n<p>&nbsp;<\/p>\n<p>The result of the intermediate term <strong>a * b <\/strong>easily exceeds the range of either of its <strong>byte <\/strong>operands. Java automatically promotes each <strong>byte <\/strong>or <strong>short <\/strong>operand to <strong>int <\/strong>when evaluating an expression. This means that the sub expression <strong>a * b <\/strong>is performed using integers not bytes. Thus, 2,000, the result of the intermediate expression, <strong>50 * 40<\/strong>, is legal even though <strong>a <\/strong>and <strong>b <\/strong>are both specified as type <strong>byte<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308623\"><\/a>The Type Promotion Rules<\/h3>\n<p>In addition to the elevation of <strong>byte<\/strong>s and <strong>short<\/strong>s to <strong>int<\/strong>, Java defines several <em>type promotion rules <\/em>that apply to expressions. They are as follows. First, all <strong>byte <\/strong>and <strong>short <\/strong>values are promoted to <strong>int<\/strong>. Then, if one operand is a <strong>long<\/strong>, the whole expression is promoted to <strong>long<\/strong>. If one operand is a <strong>float, <\/strong>the entire expression is promoted to <strong>float<\/strong>. If any of the operands is <strong>double<\/strong>, the result is <strong>double<\/strong>. The following program demonstrates how each value in the expression gets promoted to match the second argument to each binary operator:<\/p>\n<p>&nbsp;<\/p>\n<p>class Promote {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>byte b = 42;<\/p>\n<p>char c = &#8216;a&#8217;;<\/p>\n<p>short s = 1024;<\/p>\n<p>int i = 50000;<\/p>\n<p>float f = 5.67f;<\/p>\n<p>double d = .1234;<\/p>\n<p>double result = (f * b) + (i \/ c) &#8211; (d * s);<\/p>\n<p>System.out.println((f * b) + &#8221; + &#8221; + (i \/ c) + &#8221; &#8211; &#8221; + (d * s));<\/p>\n<p>System.out.println(&#8220;result = &#8221; + result);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>In the line from the program:<\/p>\n<p>&nbsp;<\/p>\n<p>double result = (f * b) + (i \/ c) &#8211; (d * s);<\/p>\n<p>&nbsp;<\/p>\n<p>In the first sub expression, <strong>f * b<\/strong>, <strong>b <\/strong>is promoted to a <strong>float <\/strong>and the result of the sub expression is <strong>float<\/strong>. Next, in the sub expression <strong>i \/ c<\/strong>, <strong>c <\/strong>is promoted to <strong>int<\/strong>, and the result is of type <strong>int<\/strong>. Then, in <strong>d * s<\/strong>, the value of <strong>s <\/strong>is promoted to <strong>double<\/strong>, and the type of the sub expression is <strong>double<\/strong>. Finally, these three intermediate values, <strong>float<\/strong>, <strong>int<\/strong>, and <strong>double<\/strong>, are considered. The outcome of <strong>float <\/strong>plus an <strong>int <\/strong>is a <strong>float<\/strong>. Then the resultant <strong>float <\/strong>minus the last <strong>double <\/strong>is promoted to <strong>double<\/strong>, which is the type for the final result of the expression.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179308624\"><\/a><strong>Arrays<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>An <em>array <\/em>is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308625\"><\/a>One-Dimensional Arrays<\/h3>\n<p>A <em>one-dimensional array <\/em>is, essentially, a list of like-typed variables. To create an array, you first must create an array variable of the desired type. The general form of a onedimensional array declaration is<\/p>\n<p>&nbsp;<\/p>\n<p><em>type var-name<\/em>[ ];<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>type <\/em>declares the base type of the array. The base type determines the data type of each element that comprises the array. Thus, the base type for the array determines what type of data the array will hold. For example, the following declares an array named <strong>month_days <\/strong>with the type \u201carray of int\u201d:<\/p>\n<p>&nbsp;<\/p>\n<p>int month_days[];<\/p>\n<p>&nbsp;<\/p>\n<p>Although this declaration establishes the fact that <strong>month_days <\/strong>is an array variable, no array actually exists. In fact, the value of <strong>month_days <\/strong>is set to <strong>null<\/strong>, which represents an array with no value. To link <strong>month_days <\/strong>with an actual, physical array of integers, you must allocate one using <strong>new <\/strong>and assign it to <strong>month_days<\/strong>. <strong>new <\/strong>is a special operator that allocates memory.<\/p>\n<p>The general form of <strong>new <\/strong>as it applies to one-dimensional arrays appears as follows:<\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>array-var <\/em>= new <em>type<\/em>[<em>size<\/em>];<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>type <\/em>specifies the type of data being allocated, <em>size <\/em>specifies the number of elements in the array, and <em>array-var <\/em>is the array variable that is linked to the array. That is, to use <strong>new <\/strong>to allocate an array, you must specify the type and number of elements to allocate. The elements in the array allocated by <strong>new <\/strong>will automatically be initialized to zero. This example allocates a 12-element array of integers and links them to <strong>month_days<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>month_days = new int[12];<\/p>\n<p>&nbsp;<\/p>\n<p>After this statement executes, <strong>month_days <\/strong>will refer to an array of 12 integers. Further, all elements in the array will be initialized to zero. Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using <strong>new<\/strong>, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated. Once you have allocated an array, you can access a specific element in the array by specifying its index within square brackets. All array indexes start at zero. For example, this statement assigns the value 28 to the second element of <strong>month_days<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>month_days[1] = 28;<\/p>\n<p>&nbsp;<\/p>\n<p>The next line displays the value stored at index 3.<\/p>\n<p>&nbsp;<\/p>\n<p>System.out.println(month_days[3]);<\/p>\n<p>&nbsp;<\/p>\n<p>Putting together all the pieces, here is a program that creates an array of the number of days in each month.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate a one-dimensional array.<\/p>\n<p>class Array {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int month_days[];<\/p>\n<p>month_days = new int[12];<\/p>\n<p>month_days[0] = 31;<\/p>\n<p>month_days[1] = 28;<\/p>\n<p>month_days[2] = 31;<\/p>\n<p>month_days[3] = 30;<\/p>\n<p>month_days[4] = 31;<\/p>\n<p>month_days[5] = 30;<\/p>\n<p>month_days[6] = 31;<\/p>\n<p>month_days[7] = 31;<\/p>\n<p>month_days[8] = 30;<\/p>\n<p>month_days[9] = 31;<\/p>\n<p>month_days[10] = 30;<\/p>\n<p>month_days[11] = 31;<\/p>\n<p>System.out.println(&#8220;April has &#8221; + month_days[3] + &#8221; days.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>When you run this program, it prints the number of days in April. Java array indexes start with zero, so the number of days in April is <strong>month_days[3] <\/strong>or 30. It is possible to combine the declaration of the array variable with the allocation of the array itself, as shown here:<\/p>\n<p>&nbsp;<\/p>\n<p>int month_days[] = new int[12];<\/p>\n<p>&nbsp;<\/p>\n<p>Arrays can be initialized when they are declared. The process is much the same as that used to initialize the simple types. An <em>array initializer <\/em>is a list of comma-separated expressions surrounded by curly braces. The commas separate the values of the array elements. The array will automatically be created large enough to hold the number of elements you specify in the array initializer. There is no need to use <strong>new<\/strong>. For example, to store the number of days in each month, the following code creates an initialized array of integers:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ An improved version of the previous program.<\/p>\n<p>class AutoArray {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,<\/p>\n<p>30, 31 };<\/p>\n<p>System.out.println(&#8220;April has &#8221; + month_days[3] + &#8221; days.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>When you run this program, you see the same output as that generated by the previous version. Java strictly checks to make sure you do not accidentally try to store or reference values outside of the range of the array. The Java run-time system will check to be sure that all array indexes are in the correct range. For example, the run-time system will check the value of each index into <strong>month_days <\/strong>to make sure that it is between 0 and 11 inclusive. If you try to access elements outside the range of the array negative numbers or numbers greater than the length of the array, you will cause a run-time error. Here is one more example that uses a one-dimensional array. It finds the average of a set of numbers.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Average an array of values.<\/p>\n<p>class Average {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};<\/p>\n<p>double result = 0;<\/p>\n<p>int i;<\/p>\n<p>for(i=0; i&lt;5; i++)<\/p>\n<p>result = result + nums[i];<\/p>\n<p>System.out.println(&#8220;Average is &#8221; + result \/ 5);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308626\"><\/a>Multidimensional Arrays<\/h3>\n<p>In Java, <em>multidimensional arrays <\/em>are actually arrays of arrays. To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a two-dimensional array variable called <strong>twoD<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>int twoD[][] = new int[4][5];<\/p>\n<p>&nbsp;<\/p>\n<p>This allocates a 4 by 5 array and assigns it to <strong>twoD<\/strong>. Internally this matrix is implemented as an <em>array <\/em>of <em>arrays <\/em>of <strong>int<\/strong>. A conceptual view of a 4 by 5, two-dimensional array The following program numbers each element in the array from left to right, top to bottom, and then displays these values:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate a two-dimensional array.<\/p>\n<p>class TwoDArray {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int twoD[][]= new int[4][5];<\/p>\n<p>int i, j, k = 0;<\/p>\n<p>for(i=0; i&lt;4; i++)<\/p>\n<p>for(j=0; j&lt;5; j++) {<\/p>\n<p>twoD[i][j] = k;<\/p>\n<p>k++;<\/p>\n<p>}<\/p>\n<p>for(i=0; i&lt;4; i++) {<\/p>\n<p>for(j=0; j&lt;5; j++)<\/p>\n<p>System.out.print(twoD[i][j] + &#8221; &#8220;);<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program generates the following output:<\/p>\n<p>&nbsp;<\/p>\n<p>0 1 2 3 4<\/p>\n<p>5 6 7 8 9<\/p>\n<p>10 11 12 13 14<\/p>\n<p>15 16 17 18 19<\/p>\n<p>&nbsp;<\/p>\n<p>When you allocate memory for a multidimensional array, you need only specify the memory for the first leftmost dimension. You can allocate the remaining dimensions separately. For example, this following code allocates memory for the first dimension of <strong>twoD <\/strong>when it is declared. It allocates the second dimension manually.<\/p>\n<p>&nbsp;<\/p>\n<p>int twoD[][] = new int[4][];<\/p>\n<p>twoD[0] = new int[5];<\/p>\n<p>twoD[1] = new int[5];<\/p>\n<p>twoD[2] = new int[5];<\/p>\n<p>twoD[3] = new int[5];<\/p>\n<p>&nbsp;<\/p>\n<p>While there is no advantage to individually allocating the second dimension arrays in this situation, there may be in others. For example, when you allocate dimensions manually, you do not need to allocate the same number of elements for each dimension. As stated earlier, since multidimensional arrays are actually arrays of arrays, the length of each array is under your control. For example, the following program creates a twodimensional array in which the sizes of the second dimension are unequal.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Manually allocate differing size second dimensions.<\/p>\n<p>class TwoDAgain {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int twoD[][] = new int[4][];<\/p>\n<p>twoD[0] = new int[1];<\/p>\n<p>twoD[1] = new int[2];<\/p>\n<p>twoD[2] = new int[3];<\/p>\n<p>twoD[3] = new int[4];<\/p>\n<p>int i, j, k = 0;<\/p>\n<p>for(i=0; i&lt;4; i++)<\/p>\n<p>for(j=0; j&lt;i+1; j++) {<\/p>\n<p>twoD[i][j] = k;<\/p>\n<p>k++;<\/p>\n<p>}<\/p>\n<p>for(i=0; i&lt;4; i++) {<\/p>\n<p>for(j=0; j&lt;i+1; j++)<\/p>\n<p>System.out.print(twoD[i][j] + &#8221; &#8220;);<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program generates the following output:<\/p>\n<p>&nbsp;<\/p>\n<p>0<\/p>\n<p>1 2<\/p>\n<p>3 4 5<\/p>\n<p>6 7 8 9<\/p>\n<p>&nbsp;<\/p>\n<p>The use of uneven or, irregular multidimensional arrays is not recommended for most applications, because it runs contrary to what people expect to find when a multidimensional array is encountered. However, it can be used effectively in some situations. For example, if you need a very large two-dimensional array that is sparsely populated that is, one in which not all of the elements will be used, then an irregular array might be a perfect solution. It is possible to initialize multidimensional arrays. To do so, simply enclose each dimension\u2019s initializer within its own set of curly braces. The following program creates a matrix where each element contains the product of the row and column indexes. Also notice that you can use expressions as well as literal values inside of array initializers.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Initialize a two-dimensional array.<\/p>\n<p>class Matrix {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>double m[][] = {<\/p>\n<p>{ 0*0, 1*0, 2*0, 3*0 },<\/p>\n<p>{ 0*1, 1*1, 2*1, 3*1 },<\/p>\n<p>{ 0*2, 1*2, 2*2, 3*2 },<\/p>\n<p>{ 0*3, 1*3, 2*3, 3*3 }<\/p>\n<p>};<\/p>\n<p>int i, j;<\/p>\n<p>for(i=0; i&lt;4; i++) {<\/p>\n<p>for(j=0; j&lt;4; j++)<\/p>\n<p>System.out.print(m[i][j] + &#8221; &#8220;);<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>When you run this program, you will get the following output:<\/p>\n<p>&nbsp;<\/p>\n<p>0.0 0.0 0.0 0.0<\/p>\n<p>0.0 1.0 2.0 3.0<\/p>\n<p>0.0 2.0 4.0 6.0<\/p>\n<p>0.0 3.0 6.0 9.0<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, each row in the array is initialized as specified in the initialization lists. The following program creates a 3 by 4 by 5, three-dimensional array. It then loads each element with the product of its indexes. Finally, it displays these products.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate a three-dimensional array.<\/p>\n<p>class threeDMatrix {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int threeD[][][] = new int[3][4][5];<\/p>\n<p>int i, j, k;<\/p>\n<p>for(i=0; i&lt;3; i++)<\/p>\n<p>for(j=0; j&lt;4; j++)<\/p>\n<p>for(k=0; k&lt;5; k++)<\/p>\n<p>threeD[i][j][k] = i * j * k;<\/p>\n<p>for(i=0; i&lt;3; i++) {<\/p>\n<p>for(j=0; j&lt;4; j++) {<\/p>\n<p>for(k=0; k&lt;5; k++)<\/p>\n<p>System.out.print(threeD[i][j][k] + &#8221; &#8220;);<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program generates the following output:<\/p>\n<p>&nbsp;<\/p>\n<p>0 0 0 0 0<\/p>\n<p>0 0 0 0 0<\/p>\n<p>0 0 0 0 0<\/p>\n<p>0 0 0 0 0<\/p>\n<p>0 0 0 0 0<\/p>\n<p>0 1 2 3 4<\/p>\n<p>0 2 4 6 8<\/p>\n<p>0 3 6 9 12<\/p>\n<p>0 0 0 0 0<\/p>\n<p>0 2 4 6 8<\/p>\n<p>0 4 8 12 16<\/p>\n<p>0 6 12 18 24<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc179308627\"><\/a>Alternative Array Declaration Syntax<\/h3>\n<p>There is a second form that may be used to declare an array:<\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>type<\/em>[ ] <em>var-name;<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p>Here, the square brackets follow the type specifier, and not the name of the array variable. For example, the following two declarations are equivalent:<\/p>\n<p>&nbsp;<\/p>\n<p>int al[] = new int[3];<\/p>\n<p>int[] a2 = new int[3];<\/p>\n<p>&nbsp;<\/p>\n<p>The following declarations are also equivalent:<\/p>\n<p>&nbsp;<\/p>\n<p>char twod1[][] = new char[3][4];<\/p>\n<p>char[][] twod2 = new char[3][4];<\/p>\n<p>&nbsp;<\/p>\n<p>This alternative declaration form is included as a convenience, and is also useful when specifying an array as a return type for a method.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data Types, Variables, and Arrays &nbsp; Java is a Strongly Typed Language which means that in Java every variable has a type, every expression has a type, and every type&hellip; <\/p>\n","protected":false},"author":1,"featured_media":3471,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3441"}],"collection":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/comments?post=3441"}],"version-history":[{"count":2,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3441\/revisions"}],"predecessor-version":[{"id":3480,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3441\/revisions\/3480"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media\/3471"}],"wp:attachment":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media?parent=3441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/categories?post=3441"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/tags?post=3441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}