<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2118932319280195164</id><updated>2024-10-25T01:01:41.898+01:00</updated><title type='text'>mmp669-Android Development</title><subtitle type='html'>android app, Java coding, Eclipse</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>32</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-8806380076370594996</id><published>2013-05-20T17:37:00.000+01:00</published><updated>2013-05-20T17:37:23.445+01:00</updated><title type='text'>Java Tutorial 26: Annotations</title><content type='html'>&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
Annotations&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
&lt;em&gt;Annotations&lt;/em&gt;&amp;nbsp;provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.&lt;br /&gt;
Annotations have a number of uses, among them:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Information for the compiler&lt;/b&gt;&amp;nbsp;— Annotations can be used by the compiler to detect errors or suppress warnings.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Compiler-time and deployment-time processing&lt;/b&gt;&amp;nbsp;— Software tools can process annotation information to generate code, XML files, and so forth.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Runtime processing&lt;/b&gt;&amp;nbsp;— Some annotations are available to be examined at runtime.&lt;/li&gt;
&lt;/ul&gt;
Annotations can be applied to a program&#39;s declarations of classes, fields, methods, and other program elements.&lt;br /&gt;
The annotation appears first, often (by convention) on its own line, and may include&amp;nbsp;&lt;i&gt;elements&lt;/i&gt;&amp;nbsp;with named or unnamed values:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@Author(
   name = &quot;Benjamin Franklin&quot;,
   date = &quot;3/27/2003&quot;
)
class MyClass() { }
&lt;/pre&gt;
&lt;/div&gt;
or&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@SuppressWarnings(value = &quot;unchecked&quot;)
void myMethod() { }
&lt;/pre&gt;
&lt;/div&gt;
If there is just one element named &quot;value,&quot; then the name may be omitted, as in:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@SuppressWarnings(&quot;unchecked&quot;)
void myMethod() { }
&lt;/pre&gt;
&lt;/div&gt;
Also, if an annotation has no elements, the parentheses may be omitted, as in:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@Override
void mySuperMethod() { }
&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Documentation&lt;/h2&gt;
Many annotations replace what would otherwise have been comments in code.&lt;br /&gt;
Suppose that a software group has traditionally begun the body of every class with comments providing important information:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Generation3List extends Generation2List {

   // Author: John Doe
   // Date: 3/17/2002
   // Current revision: 6
   // Last modified: 4/12/2004
   // By: Jane Doe
   // Reviewers: Alice, Bill, Cindy

   // class code goes here

}
&lt;/pre&gt;
&lt;/div&gt;
To add this same metadata with an annotation, you must first define the&amp;nbsp;&lt;i&gt;annotation type&lt;/i&gt;. The syntax for doing this is:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@interface ClassPreamble {
   String author();
   String date();
   int currentRevision() default 1;
   String lastModified() default &quot;N/A&quot;;
   String lastModifiedBy() default &quot;N/A&quot;;
   // Note use of array
   String[] reviewers();
}
&lt;/pre&gt;
&lt;/div&gt;
The annotation type definition looks somewhat like an interface definition where the keyword&amp;nbsp;&lt;code&gt;interface&lt;/code&gt;&amp;nbsp;is preceded by the @ character (@ = &quot;AT&quot; as in Annotation Type). Annotation types are, in fact, a form of&amp;nbsp;&lt;i&gt;interface&lt;/i&gt;, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.&lt;br /&gt;
The body of the annotation definition above contains&amp;nbsp;&lt;i&gt;annotation type element&lt;/i&gt;&amp;nbsp;declarations, which look a lot like methods. Note that they may define optional default values.&lt;br /&gt;
Once the annotation type has been defined, you can use annotations of that type, with the values filled in, like this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@ClassPreamble (
   author = &quot;John Doe&quot;,
   date = &quot;3/17/2002&quot;,
   currentRevision = 6,
   lastModified = &quot;4/12/2004&quot;,
   lastModifiedBy = &quot;Jane Doe&quot;,
   // Note array notation
   reviewers = {&quot;Alice&quot;, &quot;Bob&quot;, &quot;Cindy&quot;}
)
public class Generation3List extends Generation2List {

// class code goes here

}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp;To make the information in&amp;nbsp;&lt;code&gt;@ClassPreamble&lt;/code&gt;&amp;nbsp;appear in Javadoc-generated documentation, you must annotate the&amp;nbsp;&lt;code&gt;@ClassPreamble&lt;/code&gt;&amp;nbsp;definition itself with the&lt;code&gt;@Documented&lt;/code&gt;&amp;nbsp;annotation:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;// import this to use &lt;code&gt;@Documented&lt;/code&gt;
import java.lang.annotation.*;

@Documented
@interface ClassPreamble {

   // Annotation element definitions
   
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Annotations Used by the Compiler&lt;/h2&gt;
There are three annotation types that are predefined by the language specification itself:&amp;nbsp;&lt;code&gt;@Deprecated&lt;/code&gt;,&amp;nbsp;&lt;code&gt;@Override&lt;/code&gt;, and&amp;nbsp;&lt;code&gt;@SuppressWarnings&lt;/code&gt;.&lt;br /&gt;
&lt;b&gt;@Deprecated&lt;/b&gt;—the&amp;nbsp;&lt;a class=&quot;APILink&quot; href=&quot;http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.html&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;@Deprecated&lt;/code&gt;&lt;/a&gt;&amp;nbsp;annotation indicates that the marked element is&amp;nbsp;&lt;i&gt;deprecated&lt;/i&gt;&amp;nbsp;and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the&amp;nbsp;&lt;code&gt;@Deprecated&lt;/code&gt;&amp;nbsp;annotation. When an element is deprecated, it should also be documented using the Javadoc&amp;nbsp;&lt;code&gt;@deprecated&lt;/code&gt;&amp;nbsp;tag, as shown in the following example. The use of the &quot;@&quot; symbol in both Javadoc comments and in annotations is not coincidental — they are related conceptually. Also, note that the Javadoc tag starts with a lowercase &quot;d&quot; and the annotation starts with an uppercase &quot;D&quot;.&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;   // Javadoc comment follows
    /**
     * &lt;i&gt;@deprecated&lt;/i&gt;
     * &lt;i&gt;explanation of why it&lt;/i&gt;
     * &lt;i&gt;was deprecated&lt;/i&gt;
     */
    &lt;b&gt;@Deprecated&lt;/b&gt;
    static void deprecatedMethod() { }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;b&gt;@Override&lt;/b&gt;—the&amp;nbsp;&lt;a class=&quot;APILink&quot; href=&quot;http://docs.oracle.com/javase/7/docs/api/java/lang/Override.html&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;@Override&lt;/code&gt;&lt;/a&gt;&amp;nbsp;annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled &quot;Interfaces and Inheritance&quot;).&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;   // &lt;i&gt;mark method as a superclass method&lt;/i&gt;
   // &lt;i&gt;that has been overridden&lt;/i&gt;
   &lt;b&gt;@Override&lt;/b&gt; 
   int overriddenMethod() { }
&lt;/pre&gt;
&lt;/div&gt;
While it&#39;s not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with&amp;nbsp;&lt;code&gt;@Override&lt;/code&gt;&amp;nbsp;fails to correctly override a method in one of its superclasses, the compiler generates an error.&lt;br /&gt;
&lt;b&gt;@SuppressWarnings&lt;/b&gt;—the&amp;nbsp;&lt;a class=&quot;APILink&quot; href=&quot;http://docs.oracle.com/javase/7/docs/api/java/lang/SuppressWarnings.html&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;@SuppressWarnings&lt;/code&gt;&lt;/a&gt;&amp;nbsp;annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the example below, a deprecated method is used and the compiler would normally generate a warning. In this case, however, the annotation causes the warning to be suppressed.&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;   // &lt;i&gt;use a deprecated method and tell&lt;/i&gt; 
   // &lt;i&gt;compiler not to generate a warning&lt;/i&gt;
   &lt;b&gt;@SuppressWarnings(&quot;deprecation&quot;)&lt;/b&gt;
    void useDeprecatedMethod() {
        // deprecation warning
        // - suppressed
        objectOne.deprecatedMethod();
    }
&lt;/pre&gt;
&lt;/div&gt;
Every compiler warning belongs to a category. The Java Language Specification lists two categories: &quot;deprecation&quot; and &quot;unchecked.&quot; The &quot;unchecked&quot; warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled &quot;Generics&quot;). To suppress more than one category of warnings, use the following syntax:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;@SuppressWarnings({&quot;unchecked&quot;, &quot;deprecation&quot;})
&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Annotation Processing&lt;/h2&gt;
The more advanced uses of annotations include writing an&amp;nbsp;&lt;i&gt;annotation processor&lt;/i&gt;&amp;nbsp;that can read a Java program and take actions based on its annotations. It might, for example, generate auxiliary source code, relieving the programmer of having to create boilerplate code that always follows predictable patterns. To facilitate this task, release 5.0 of the JDK includes an annotation processing tool, called&amp;nbsp;&lt;code&gt;apt&lt;/code&gt;. In release 6 of the JDK, the functionality of&amp;nbsp;&lt;code&gt;apt&lt;/code&gt;&amp;nbsp;is a standard part of the Java compiler.&lt;br /&gt;
To make annotation information available at runtime, the annotation type itself must be annotated with&amp;nbsp;&lt;code&gt;@Retention(RetentionPolicy.RUNTIME)&lt;/code&gt;, as follows:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;import java.lang.annotation.*; 

@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationForRuntime {

   // Elements that give information
   // for runtime processing
   
}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Source: oracle&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/8806380076370594996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-26-nested-classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8806380076370594996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8806380076370594996'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-26-nested-classes.html' title='Java Tutorial 26: Annotations'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-6411778586410794766</id><published>2013-05-20T17:15:00.004+01:00</published><updated>2013-05-20T17:15:52.465+01:00</updated><title type='text'>Java Tutorial 25: Nested Classes</title><content type='html'>&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
Nested Classes&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
The Java programming language allows you to define a class within another class. Such a class is called a&amp;nbsp;&lt;i&gt;nested class&lt;/i&gt;&amp;nbsp;and is illustrated here:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;class OuterClass {
    ...
    class NestedClass {
        ...
    }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Terminology:&lt;/strong&gt;&amp;nbsp;Nested classes are divided into two categories: static and non-static. Nested classes that are declared&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;are simply called&amp;nbsp;&lt;i&gt;static nested classes&lt;/i&gt;. Non-static nested classes are called&amp;nbsp;&lt;i&gt;inner classes&lt;/i&gt;.&lt;hr /&gt;
&lt;/div&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;class OuterClass {
    ...
    static class StaticNestedClass {
        ...
    }
    class InnerClass {
        ...
    }
}
&lt;/pre&gt;
&lt;/div&gt;
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the&amp;nbsp;&lt;code&gt;OuterClass&lt;/code&gt;, a nested class can be declared&amp;nbsp;&lt;code&gt;private&lt;/code&gt;,&amp;nbsp;&lt;code&gt;public&lt;/code&gt;,&amp;nbsp;&lt;code&gt;protected&lt;/code&gt;, or&amp;nbsp;&lt;i&gt;package private&lt;/i&gt;. (Recall that outer classes can only be declared&amp;nbsp;&lt;code&gt;public&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;i&gt;package private&lt;/i&gt;.)&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Why Use Nested Classes?&lt;/h2&gt;
There are several compelling reasons for using nested classes, among them:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;It is a way of logically grouping classes that are only used in one place.&lt;/li&gt;
&lt;li&gt;It increases encapsulation.&lt;/li&gt;
&lt;li&gt;Nested classes can lead to more readable and maintainable code.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Logical grouping of classes&lt;/b&gt;—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such &quot;helper classes&quot; makes their package more streamlined.&lt;br /&gt;
&lt;b&gt;Increased encapsulation&lt;/b&gt;—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared&amp;nbsp;&lt;code&gt;private&lt;/code&gt;. By hiding class B within class A, A&#39;s members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.&lt;br /&gt;
&lt;b&gt;More readable, maintainable code&lt;/b&gt;—Nesting small classes within top-level classes places the code closer to where it is used.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Static Nested Classes&lt;/h2&gt;
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.&lt;br /&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp;A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.&lt;hr /&gt;
&lt;/div&gt;
Static nested classes are accessed using the enclosing class name:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;OuterClass.StaticNestedClass
&lt;/pre&gt;
&lt;/div&gt;
For example, to create an object for the static nested class, use this syntax:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();
&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Inner Classes&lt;/h2&gt;
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object&#39;s methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.&lt;br /&gt;
Objects that are instances of an inner class exist&amp;nbsp;&lt;i&gt;within&lt;/i&gt;&amp;nbsp;an instance of the outer class. Consider the following classes:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;class OuterClass {
    ...
    class InnerClass {
        ...
    }
}

&lt;/pre&gt;
&lt;/div&gt;
An instance of&amp;nbsp;&lt;code&gt;InnerClass&lt;/code&gt;&amp;nbsp;can exist only within an instance of&amp;nbsp;&lt;code&gt;OuterClass&lt;/code&gt;&amp;nbsp;and has direct access to the methods and fields of its enclosing instance.&amp;nbsp;&lt;span id=&quot;figure:classes-inner.gif&quot;&gt;The next figure&lt;/span&gt;&amp;nbsp;illustrates this idea.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm0UaJZ-Aiadi4nuKxqTzC3U8O5HvrK3UNnx7S47t-JrLMp6iNwLFSXHjp2FEtdO6PR9eBwOe_WvrW5frtA81WXV5Ceeia-btZLbdmOKEY1vvQTmaBKgS3ipXF2cekBgMefcNKZDSzcEBp/s1600/2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;126&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm0UaJZ-Aiadi4nuKxqTzC3U8O5HvrK3UNnx7S47t-JrLMp6iNwLFSXHjp2FEtdO6PR9eBwOe_WvrW5frtA81WXV5Ceeia-btZLbdmOKEY1vvQTmaBKgS3ipXF2cekBgMefcNKZDSzcEBp/s320/2.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;center&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;FigureCaption&quot; style=&quot;font-family: sans-serif; font-size: smaller; margin-left: 1in; margin-right: 1in; text-align: justify;&quot;&gt;
An Instance of InnerClass Exists Within an Instance of OuterClass&lt;/div&gt;
&lt;/center&gt;
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;OuterClass.InnerClass innerObject = outerObject.new InnerClass();
&lt;/pre&gt;
&lt;/div&gt;
Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes). Both of these will be discussed briefly in the next section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
Inner Class Example&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
To see an inner class in use, let&#39;s first consider an array. In the following example, we will create an array, fill it with integer values and then output only values of even indices of the array in ascending order.&lt;br /&gt;
The&amp;nbsp;&lt;code&gt;DataStructure&lt;/code&gt;&amp;nbsp;class below consists of:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The&amp;nbsp;&lt;code&gt;DataStructure&lt;/code&gt;&amp;nbsp;outer class, which includes methods to add an integer onto the array and print out values of even indices of the array.&lt;/li&gt;
&lt;li&gt;The&amp;nbsp;&lt;code&gt;InnerEvenIterator&lt;/code&gt;&amp;nbsp;inner class, which is similar to a standard Java&amp;nbsp;&lt;i&gt;iterator&lt;/i&gt;. Iterators are used to step through a data structure and typically have methods to test for the last element, retrieve the current element, and move to the next element.&lt;/li&gt;
&lt;li&gt;A&amp;nbsp;&lt;code&gt;main&lt;/code&gt;&amp;nbsp;method that instantiates a&amp;nbsp;&lt;code&gt;DataStructure&lt;/code&gt;&amp;nbsp;object (&lt;code&gt;ds&lt;/code&gt;) and uses it to fill the&amp;nbsp;&lt;code&gt;arrayOfInts&lt;/code&gt;&amp;nbsp;array with integer values (0, 1, 2, 3, etc.), then calls a&amp;nbsp;&lt;code&gt;printEven&lt;/code&gt;&amp;nbsp;method to print out values of even indices of&amp;nbsp;&lt;code&gt;arrayOfInts&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class DataStructure {
    // create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];
    
    public DataStructure() {
        // fill the array with ascending integer values
        for (int i = 0; i &amp;lt; SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }
    
    public void printEven() {
        // print out values of even indices of the array
        InnerEvenIterator iterator = this.new InnerEvenIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + &quot; &quot;);
        }
    }
    
    // inner class implements the Iterator pattern
    private class InnerEvenIterator {
        // start stepping through the array from the beginning
        private int next = 0;
        
        public boolean hasNext() {
            // check if a current element is the last in the array
            return (next &amp;lt;= SIZE - 1);
        }
        
        public int getNext() {
            // record a value of an even index of the array
            int retValue = arrayOfInts[next];
            //get the next even element
            next += 2;
            return retValue;
        }
    }
    
    public static void main(String s[]) {
        // fill the array with integer values and print out only
        // values of even indices
        DataStructure ds = new DataStructure();
        ds.printEven();
    }
}
&lt;/pre&gt;
&lt;/div&gt;
The output is:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;0 2 4 6 8 10 12 14 
&lt;/pre&gt;
&lt;/div&gt;
Note that the&amp;nbsp;&lt;code&gt;InnerEvenIterator&lt;/code&gt;&amp;nbsp;class refers directly to the&amp;nbsp;&lt;code&gt;arrayOfInts&lt;/code&gt;&amp;nbsp;instance variable of the&amp;nbsp;&lt;code&gt;DataStructure&lt;/code&gt;&amp;nbsp;object.&lt;br /&gt;
Inner classes can be used to implement helper classes like the one shown in the example above. If you plan on handling user-interface events, you will need to know how to use inner classes because the event-handling mechanism makes extensive use of them.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Local and Anonymous Inner Classes&lt;/h2&gt;
There are two additional types of inner classes. You can declare an inner class within the body of a method. Such a class is known as a&amp;nbsp;&lt;i&gt;local inner class&lt;/i&gt;. You can also declare an inner class within the body of a method without naming it. These classes are known as&amp;nbsp;&lt;i&gt;anonymous inner classes&lt;/i&gt;. You will encounter such classes in advanced Java programming.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Modifiers&lt;/h2&gt;
You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers —&amp;nbsp;&lt;code&gt;private&lt;/code&gt;,&amp;nbsp;&lt;code&gt;public&lt;/code&gt;, and&amp;nbsp;&lt;code&gt;protected&lt;/code&gt;— to restrict access to inner classes, just as you do to other class members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
Summary of Nested Classes&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class&#39;s members even if they are declared private.&lt;br /&gt;
&lt;span id=&quot;table:summary-nested&quot;&gt;The following table&lt;/span&gt;&amp;nbsp;shows the types of nested classes:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNNTQY_hrdZGnhAVdHf0pDeKp2sYxNLEuEJdmsXUWvkOW8mWZtjZelVw4z6QNarYZX8bRBWnV_PFNVFJISu4c0VlW6PJxRL6Jpz1XWVnJivEo_yMf3RD9iSntvflk8OTNqnUtYVaxksgsM/s1600/1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;129&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNNTQY_hrdZGnhAVdHf0pDeKp2sYxNLEuEJdmsXUWvkOW8mWZtjZelVw4z6QNarYZX8bRBWnV_PFNVFJISu4c0VlW6PJxRL6Jpz1XWVnJivEo_yMf3RD9iSntvflk8OTNqnUtYVaxksgsM/s320/1.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Source: Oracle&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/6411778586410794766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-25-nested-classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6411778586410794766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6411778586410794766'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-25-nested-classes.html' title='Java Tutorial 25: Nested Classes'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm0UaJZ-Aiadi4nuKxqTzC3U8O5HvrK3UNnx7S47t-JrLMp6iNwLFSXHjp2FEtdO6PR9eBwOe_WvrW5frtA81WXV5Ceeia-btZLbdmOKEY1vvQTmaBKgS3ipXF2cekBgMefcNKZDSzcEBp/s72-c/2.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-6487588208438307093</id><published>2013-05-17T17:27:00.002+01:00</published><updated>2013-05-17T17:42:46.265+01:00</updated><title type='text'>Java Encapsulation</title><content type='html'>&lt;br /&gt;
&lt;div style=&quot;background-color: white; font-family: Helvetica, Arial, sans-serif; font-size: 12px; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: Helvetica, Arial, sans-serif; font-size: 12px; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: Helvetica, Arial, sans-serif; font-size: 12px; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: Helvetica, Arial, sans-serif; font-size: 12px; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
To run the example below, you need to set-up a new project in Eclipse and put both classes under this project.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: Helvetica, Arial, sans-serif; font-size: 12px; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: Helvetica, Arial, sans-serif; font-size: 12px; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
&lt;b&gt;account class:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; margin-bottom: 1em; margin-top: 0.8em; padding: 0px; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;public class account {&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;font-size: 12px; white-space: pre;&quot;&gt;&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;i&gt; &lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;private String name;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;private String address;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;private double balance;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;font-size: 12px; white-space: pre;&quot;&gt;&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;i&gt; &lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public void setName (String n){&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;name=n;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public String getName(){&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return name;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;font-size: 12px; white-space: pre;&quot;&gt;&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;i&gt; &lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public void setAddress (String a){&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;address=a;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public String getAddress(){&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return address;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;font-size: 12px; white-space: pre;&quot;&gt;&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;i&gt; &lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public void setBalance(double g){&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;balance=g;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public double getBalance (){&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return balance;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;i&gt;}&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;b&gt;myaccount class&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;public class myAccount {&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;i&gt; &lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public static void main (String [] args){&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;i&gt;  &lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;account myAccount=new account();&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;myAccount.setName (&quot;Tom&quot;);&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;myAccount.setAddress (&quot;bugaosuni&quot;);&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;myAccount.setBalance (10000000);&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;i&gt;    &lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(myAccount.getName()+&quot;\t&quot;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;       &lt;/span&gt;+myAccount.getAddress()+&quot;\t&quot;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;        &lt;/span&gt;+myAccount.getBalance());&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;i&gt;    &lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;i&gt;}&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
The addition of the word private in front of each of the account&lt;br /&gt;
class’s field declarations. The word private is a Java keyword. When a&lt;br /&gt;
field is declared private, no code outside of the class can make direct reference&lt;br /&gt;
to that field. &lt;br /&gt;
&lt;br /&gt;
Instead of referencing myAccount.name, the useaccount program must&lt;br /&gt;
call method myAccount.setName or method myAccount.getName. These&lt;br /&gt;
methods, setName and getName, are called accessor methods, because they&lt;br /&gt;
provide access to the Account class’s name field.&lt;br /&gt;
&lt;br /&gt;
Eclipse has a very nice function that can generate these for you so you don&#39;t have to type in all these tedious code. See screen capture below:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkzoTv9T_B94iL9cw5OtFVirXrY7eyGDdfQU1eJHro6cEUg7ePDrqPUIf8fkBSt6fOrf8buN4DzZbAC5HgWn_SnC1cfAmaixZBiU4aQ3XnzTJB__el5RA9FHeqStUr-gAN3fpfhFFYkkh2/s1600/New+Picture.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;171&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkzoTv9T_B94iL9cw5OtFVirXrY7eyGDdfQU1eJHro6cEUg7ePDrqPUIf8fkBSt6fOrf8buN4DzZbAC5HgWn_SnC1cfAmaixZBiU4aQ3XnzTJB__el5RA9FHeqStUr-gAN3fpfhFFYkkh2/s320/New+Picture.bmp&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 1em; margin-top: 0.8em; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/6487588208438307093/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-encapsulation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6487588208438307093'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6487588208438307093'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-encapsulation.html' title='Java Encapsulation'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkzoTv9T_B94iL9cw5OtFVirXrY7eyGDdfQU1eJHro6cEUg7ePDrqPUIf8fkBSt6fOrf8buN4DzZbAC5HgWn_SnC1cfAmaixZBiU4aQ3XnzTJB__el5RA9FHeqStUr-gAN3fpfhFFYkkh2/s72-c/New+Picture.bmp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-9203517974312674427</id><published>2013-05-16T14:47:00.001+01:00</published><updated>2013-05-17T09:08:05.198+01:00</updated><title type='text'>Java Tutorial 24: More on Classes-Initializing Fields</title><content type='html'>Before you start reading:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;u&gt;A variable declared inside a class but not inside any
particular method is a field. &lt;o:p&gt;&lt;/o:p&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;u&gt;Another name for a field is an instance variable.&lt;/u&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
As you have seen, you can often provide an initial value for a field in its declaration:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class BedAndBreakfast {

    // initialize to 10
    public static int capacity = 10;

    // initialize to false
    private boolean full = false;
}
&lt;/pre&gt;
&lt;/div&gt;
This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or a&amp;nbsp;&lt;code&gt;for&lt;/code&gt;&amp;nbsp;loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes&amp;nbsp;&lt;i&gt;static initialization blocks&lt;/i&gt;.&lt;br /&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp;It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Static Initialization Blocks&lt;/h2&gt;
A&amp;nbsp;&lt;i&gt;static initialization block&lt;/i&gt;&amp;nbsp;is a normal block of code enclosed in braces,&amp;nbsp;&lt;code&gt;{ }&lt;/code&gt;, and preceded by the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;keyword. Here is an example:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;static {
    // whatever code is needed for initialization goes here
}
&lt;/pre&gt;
&lt;/div&gt;
A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.&lt;br /&gt;
There is an alternative to static blocks — you can write a private static method:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;class Whatever {
    public static varType myVar = initializeClassVariable();
        
    private static varType initializeClassVariable() {

        // initialization code goes here
    }
}
&lt;/pre&gt;
&lt;/div&gt;
The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Initializing Instance Members&lt;/h2&gt;
Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods.&lt;br /&gt;
Initializer blocks for instance variables look just like static initializer blocks, but without the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;keyword:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;{
    // whatever code is needed for initialization goes here
}
&lt;/pre&gt;
&lt;/div&gt;
The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.&lt;br /&gt;
A&amp;nbsp;&lt;i&gt;final method&lt;/i&gt;&amp;nbsp;cannot be overridden in a subclass. This is discussed in the lesson on interfaces and inheritance. Here is an example of using a final method for initializing an instance variable:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;class Whatever {
    private varType myVar = initializeInstanceVariable();
        
    protected final varType initializeInstanceVariable() {

        // initialization code goes here
    }
}

&lt;/pre&gt;
&lt;/div&gt;
This is especially useful if subclasses might want to reuse the initialization method. The method is final because calling non-final methods during instance initialization can cause problems.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot; style=&quot;color: #f90000; margin: 0px 5px 0.5em 0px;&quot;&gt;
&lt;h1 style=&quot;font-family: sans-serif; font-size: 20px;&quot;&gt;
Summary of Creating and Using Classes and Objects&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot; style=&quot;margin: 0px 5px 0px 20px;&quot;&gt;
A class declaration names the class and encloses the class body between braces. The class name can be preceded by modifiers. The class body contains fields, methods, and constructors for the class. A class uses fields to contain state information and uses methods to implement behavior. Constructors that initialize a new instance of a class use the name of the class and look like methods without a return type.&lt;br /&gt;
You control access to classes and members in the same way: by using an access modifier such as&amp;nbsp;&lt;code&gt;public&lt;/code&gt;&amp;nbsp;in their declaration.&lt;br /&gt;
You specify a class variable or a class method by using the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;keyword in the member&#39;s declaration. A member that is not declared as&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;is implicitly an instance member. Class variables are shared by all instances of a class and can be accessed through the class name as well as an instance reference. Instances of a class get their own copy of each instance variable, which must be accessed through an instance reference.&lt;br /&gt;
You create an object from a class by using the&amp;nbsp;&lt;code&gt;new&lt;/code&gt;&amp;nbsp;operator and a constructor. The new operator returns a reference to the object that was created. You can assign the reference to a variable or use it directly.&lt;br /&gt;
Instance variables and methods that are accessible to code outside of the class that they are declared in can be referred to by using a qualified name. The qualified name of an instance variable looks like this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;em&gt;objectReference.variableName&lt;/em&gt;
&lt;/pre&gt;
&lt;/div&gt;
The qualified name of a method looks like this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;em&gt;objectReference.methodName(argumentList)&lt;/em&gt;
&lt;/pre&gt;
&lt;/div&gt;
or:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;&lt;em&gt;objectReference.methodName()&lt;/em&gt;
&lt;/pre&gt;
&lt;/div&gt;
The garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it. You can explicitly drop a reference by setting the variable holding the reference to&amp;nbsp;&lt;code&gt;null&lt;/code&gt;.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Source:oracle</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/9203517974312674427/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-24-more-on-classes.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/9203517974312674427'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/9203517974312674427'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-24-more-on-classes.html' title='Java Tutorial 24: More on Classes-Initializing Fields'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-4301810836705822131</id><published>2013-05-16T14:45:00.002+01:00</published><updated>2013-05-16T14:46:02.354+01:00</updated><title type='text'>Java Tutorial 23: More on Classes-Understanding Instance and Class Members</title><content type='html'>&lt;br /&gt;
In this section, we discuss the use of the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;keyword to create fields and methods that belong to the class, rather than to an instance of the class.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Class Variables&lt;/h2&gt;
When a number of objects are created from the same class blueprint, they each have their own distinct copies of&amp;nbsp;&lt;i&gt;instance variables&lt;/i&gt;. In the case of the&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;class, the instance variables are&amp;nbsp;&lt;code&gt;cadence&lt;/code&gt;,&amp;nbsp;&lt;code&gt;gear&lt;/code&gt;, and&amp;nbsp;&lt;code&gt;speed&lt;/code&gt;. Each&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;object has its own values for these variables, stored in different memory locations.&lt;br /&gt;
Sometimes, you want to have variables that are common to all objects. This is accomplished with the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;modifier. Fields that have the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;modifier in their declaration are called&lt;i&gt;static fields&lt;/i&gt;&amp;nbsp;or&amp;nbsp;&lt;i&gt;class variables&lt;/i&gt;. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.&lt;br /&gt;
For example, suppose you want to create a number of&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. For this you need a class variable,&amp;nbsp;&lt;code&gt;numberOfBicycles&lt;/code&gt;, as follows:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Bicycle{
        
    private int cadence;
    private int gear;
    private int speed;
        
    // &lt;b&gt;add an instance variable for&lt;/b&gt;
    // &lt;b&gt;the object ID&lt;/b&gt;
    private int id;
    
    // &lt;b&gt;add a class variable for the&lt;/b&gt;
    // &lt;b&gt;number of Bicycle objects instantiated&lt;/b&gt;
    private &lt;b&gt;static&lt;/b&gt; int numberOfBicycles = 0;
        ...
}
&lt;/pre&gt;
&lt;/div&gt;
Class variables are referenced by the class name itself, as in&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;Bicycle.numberOfBicycles
&lt;/pre&gt;
&lt;/div&gt;
This makes it clear that they are class variables.&lt;br /&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp;You can also refer to static fields with an object reference like&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;myBike.numberOfBicycles
&lt;/pre&gt;
&lt;/div&gt;
but this is discouraged because it does not make it clear that they are class variables.&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
You can use the&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;constructor to set the&amp;nbsp;&lt;code&gt;id&lt;/code&gt;&amp;nbsp;instance variable and increment the&amp;nbsp;&lt;code&gt;numberOfBicycles&lt;/code&gt;&amp;nbsp;class variable:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Bicycle{
        
    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;
        
    public Bicycle(int startCadence, int startSpeed, int startGear){
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

        // &lt;b&gt;increment number of Bicycles&lt;/b&gt;
        // &lt;b&gt;and assign ID number&lt;/b&gt;
        &lt;b&gt;id = ++numberOfBicycles;&lt;/b&gt;
    }

    // &lt;b&gt;new method to return the ID instance variable&lt;/b&gt;
    public int getID() {
        return id;
    }
        ...
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Class Methods&lt;/h2&gt;
The Java programming language supports static methods as well as static variables. Static methods, which have the&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;ClassName.methodName(args)
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp;You can also refer to static methods with an object reference like&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;instanceName.methodName(args)
&lt;/pre&gt;
&lt;/div&gt;
but this is discouraged because it does not make it clear that they are class methods.&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
A common use for static methods is to access static fields. For example, we could add a static method to the&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;class to access the&amp;nbsp;&lt;code&gt;numberOfBicycles&lt;/code&gt;&amp;nbsp;static field:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public &lt;b&gt;static&lt;/b&gt; int getNumberOfBicycles() {
    return numberOfBicycles;
}
&lt;/pre&gt;
&lt;/div&gt;
Not all combinations of instance and class variables and methods are allowed:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Instance methods can access instance variables and instance methods directly.&lt;/li&gt;
&lt;li&gt;Instance methods can access class variables and class methods directly.&lt;/li&gt;
&lt;li&gt;Class methods can access class variables and class methods directly.&lt;/li&gt;
&lt;li&gt;Class methods&amp;nbsp;&lt;b style=&quot;font-style: italic;&quot;&gt;cannot&lt;/b&gt;&amp;nbsp;access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;keyword as there is no instance for&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;to refer to.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Constants&lt;/h2&gt;
The&amp;nbsp;&lt;code&gt;static&lt;/code&gt;&amp;nbsp;modifier, in combination with the&amp;nbsp;&lt;code&gt;final&lt;/code&gt;&amp;nbsp;modifier, is also used to define constants. The&amp;nbsp;&lt;code&gt;final&lt;/code&gt;&amp;nbsp;modifier indicates that the value of this field cannot change.&lt;br /&gt;
For example, the following variable declaration defines a constant named&amp;nbsp;&lt;code&gt;PI&lt;/code&gt;, whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter):&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;static final double PI = 3.141592653589793;
&lt;/pre&gt;
&lt;/div&gt;
Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so. By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated by an underscore (_).&lt;br /&gt;
&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp;If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a&amp;nbsp;&lt;i&gt;compile-time constant&lt;/i&gt;. If the value of the constant in the outside world changes (for example, if it is legislated that pi actually should be 3.975), you will need to recompile any classes that use this constant to get the current value.&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
The&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;Class&lt;/h2&gt;
After all the modifications made in this section, the&amp;nbsp;&lt;code&gt;Bicycle&lt;/code&gt;&amp;nbsp;class is now:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Bicycle{
        
    private int cadence;
    private int gear;
    private int speed;
        
    private int id;
    
    private &lt;b&gt;static&lt;/b&gt; int numberOfBicycles = 0;

        
    public Bicycle(int startCadence,
                   int startSpeed,
                   int startGear){
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

        id = ++numberOfBicycles;
    }

    public int getID() {
        return id;
    }

    public static int getNumberOfBicycles() {
        return numberOfBicycles;
    }

    public int getCadence(){
        return cadence;
    }
        
    public void setCadence(int newValue){
        cadence = newValue;
    }
        
    public int getGear(){
    return gear;
    }
        
    public void setGear(int newValue){
        gear = newValue;
    }
        
    public int getSpeed(){
        return speed;
    }
        
    public void applyBrake(int decrement){
        speed -= decrement;
    }
        
    public void speedUp(int increment){
        speed += increment;
    }
}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Source: oracle&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/4301810836705822131/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-22-more-on-classes_16.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4301810836705822131'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4301810836705822131'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-22-more-on-classes_16.html' title='Java Tutorial 23: More on Classes-Understanding Instance and Class Members'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-8417989430030784881</id><published>2013-05-16T13:39:00.002+01:00</published><updated>2013-05-16T13:39:24.755+01:00</updated><title type='text'>Java Tutorial 22: More on Classes-Controlling Access to Members of a Class</title><content type='html'>&lt;br /&gt;
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;At the top level—&lt;code&gt;public&lt;/code&gt;, or&amp;nbsp;&lt;i&gt;package-private&lt;/i&gt;&amp;nbsp;(no explicit modifier).&lt;/li&gt;
&lt;li&gt;At the member level—&lt;code&gt;public&lt;/code&gt;,&amp;nbsp;&lt;code&gt;private&lt;/code&gt;,&amp;nbsp;&lt;code&gt;protected&lt;/code&gt;, or&amp;nbsp;&lt;i&gt;package-private&lt;/i&gt;&amp;nbsp;(no explicit modifier).&lt;/li&gt;
&lt;/ul&gt;
A class may be declared with the modifier&amp;nbsp;&lt;code&gt;public&lt;/code&gt;, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as&amp;nbsp;&lt;i&gt;package-private&lt;/i&gt;), it is visible only within its own package (packages are named groups of related classes)&lt;br /&gt;
&lt;br /&gt;
At the member level, you can also use the&amp;nbsp;&lt;code&gt;public&lt;/code&gt;&amp;nbsp;modifier or no modifier (&lt;i&gt;package-private&lt;/i&gt;) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers:&amp;nbsp;&lt;code&gt;private&lt;/code&gt;&amp;nbsp;and&amp;nbsp;&lt;code&gt;protected&lt;/code&gt;. The&amp;nbsp;&lt;code&gt;private&lt;/code&gt;&amp;nbsp;modifier specifies that the member can only be accessed in its own class. The&amp;nbsp;&lt;code&gt;protected&lt;/code&gt;&amp;nbsp;modifier specifies that the member can only be accessed within its own package (as with&amp;nbsp;&lt;i&gt;package-private&lt;/i&gt;) and, in addition, by a subclass of its class in another package.&lt;br /&gt;
&lt;br /&gt;
The following table shows the access to members permitted by each modifier.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9bXYHsN_wWKF7sNMioHpnHfc-AnGbKBQDBtNuOk7wPYP4GaoSQctyzvsIV7JRZgXgohCb1Znp4D8olG7EkAzWxfMOnUhA4dvtqD-I2XJux_dqPAPd2I0FNJqqyMfLqlwDO0lXzRRWAkqz/s1600/Capture.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9bXYHsN_wWKF7sNMioHpnHfc-AnGbKBQDBtNuOk7wPYP4GaoSQctyzvsIV7JRZgXgohCb1Znp4D8olG7EkAzWxfMOnUhA4dvtqD-I2XJux_dqPAPd2I0FNJqqyMfLqlwDO0lXzRRWAkqz/s1600/Capture.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.&lt;br /&gt;
&lt;br /&gt;
Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s look at a collection of classes and see how access levels affect visibility.&amp;nbsp;&lt;span id=&quot;figure:classes-access.gif&quot;&gt;The following figure&lt;/span&gt;&amp;nbsp;shows the four classes in this example and how they are related.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7UmAy8Xy4AfPYqaQ-ZwqBDpjSVOY-T_-jzeoF-vuiJ4fCF2ZGNF9mlS_V6Ls7tXpDwtr3s_prXnPR2859DPTbmGvruoOAis9BH7D0S6D0sBgeCfYGxndmlOzHL4WEnr-bp8-fnlzoRjG7/s1600/Capture.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7UmAy8Xy4AfPYqaQ-ZwqBDpjSVOY-T_-jzeoF-vuiJ4fCF2ZGNF9mlS_V6Ls7tXpDwtr3s_prXnPR2859DPTbmGvruoOAis9BH7D0S6D0sBgeCfYGxndmlOzHL4WEnr-bp8-fnlzoRjG7/s1600/Capture.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;center&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;FigureCaption&quot; style=&quot;font-family: sans-serif; font-size: smaller; margin-left: 1in; margin-right: 1in; text-align: justify;&quot;&gt;
Classes and Packages of the Example Used to Illustrate Access Levels&lt;/div&gt;
&lt;div class=&quot;FigureCaption&quot; style=&quot;font-family: sans-serif; font-size: smaller; margin-left: 1in; margin-right: 1in; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/center&gt;
&lt;span id=&quot;table:accesscontrol-levels&quot;&gt;The following table&lt;/span&gt;&amp;nbsp;shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijX3_JGFbHWCupOaycJ2G1pV_-2zaD5T9LAC-Hxf_bl6l3dERlSX2z8kH-EtI99wrSVkti88Hmr2U_TAeyiVT-8258zLjE-rhYDXe5ZVXkKhL-Pcy1dzm91xmjW14i3NmDyoq4nfe_l_KE/s1600/Capture.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;165&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijX3_JGFbHWCupOaycJ2G1pV_-2zaD5T9LAC-Hxf_bl6l3dERlSX2z8kH-EtI99wrSVkti88Hmr2U_TAeyiVT-8258zLjE-rhYDXe5ZVXkKhL-Pcy1dzm91xmjW14i3NmDyoq4nfe_l_KE/s320/Capture.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;div class=&quot;note&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;hr /&gt;
&lt;strong&gt;Tips on Choosing an Access Level:&lt;/strong&gt;&amp;nbsp;If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Use the most restrictive access level that makes sense for a particular member. Use&amp;nbsp;&lt;code&gt;private&lt;/code&gt;&amp;nbsp;unless you have a good reason not to.&lt;/li&gt;
&lt;li&gt;Avoid&amp;nbsp;&lt;code&gt;public&lt;/code&gt;&amp;nbsp;fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Source: oracle&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/8417989430030784881/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-22-more-on-classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8417989430030784881'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8417989430030784881'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-22-more-on-classes.html' title='Java Tutorial 22: More on Classes-Controlling Access to Members of a Class'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9bXYHsN_wWKF7sNMioHpnHfc-AnGbKBQDBtNuOk7wPYP4GaoSQctyzvsIV7JRZgXgohCb1Znp4D8olG7EkAzWxfMOnUhA4dvtqD-I2XJux_dqPAPd2I0FNJqqyMfLqlwDO0lXzRRWAkqz/s72-c/Capture.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-317828237915248770</id><published>2013-05-16T13:22:00.002+01:00</published><updated>2013-05-16T13:22:36.323+01:00</updated><title type='text'>Java Tutorial 21: More on Classes-Using the this Keyword</title><content type='html'>&lt;br /&gt;
Within an instance method or a constructor,&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;is a reference to the&amp;nbsp;&lt;i&gt;current object&lt;/i&gt;&amp;nbsp;— the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using&amp;nbsp;&lt;code&gt;this&lt;/code&gt;.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Using&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;with a Field&lt;/h2&gt;
The most common reason for using the&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;keyword is because a field is shadowed by a method or constructor parameter.&lt;br /&gt;
For example, the&amp;nbsp;&lt;code&gt;Point&lt;/code&gt;&amp;nbsp;class was written like this&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Point {
    public int x = 0;
    public int y = 0;
        &lt;b&gt;
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }&lt;/b&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
but it could have been written like this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Point {
    public int x = 0;
    public int y = 0;
        &lt;b&gt;
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }&lt;/b&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
Each argument to the constructor shadows one of the object&#39;s fields — inside the constructor&amp;nbsp;&lt;b&gt;&lt;code&gt;x&lt;/code&gt;&lt;/b&gt;&amp;nbsp;is a local copy of the constructor&#39;s first argument. To refer to the&amp;nbsp;&lt;code&gt;Point&lt;/code&gt;&amp;nbsp;field&amp;nbsp;&lt;b&gt;&lt;code&gt;x&lt;/code&gt;&lt;/b&gt;, the constructor must use&amp;nbsp;&lt;code style=&quot;font-weight: bold;&quot;&gt;this.x&lt;/code&gt;.&lt;br /&gt;
&lt;h2 style=&quot;color: #f90000; font-family: sans-serif; font-size: 17px;&quot;&gt;
Using&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;with a Constructor&lt;/h2&gt;
From within a constructor, you can also use the&amp;nbsp;&lt;code&gt;this&lt;/code&gt;&amp;nbsp;keyword to call another constructor in the same class. Doing so is called an&amp;nbsp;&lt;i&gt;explicit constructor invocation&lt;/i&gt;. Here&#39;s another&lt;code&gt;Rectangle&lt;/code&gt;&amp;nbsp;class, with a different implementation from the one in the&amp;nbsp;Objects&amp;nbsp;section.&lt;br /&gt;
&lt;div class=&quot;codeblock&quot; style=&quot;margin: 0px 30px;&quot;&gt;
&lt;pre&gt;public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        &lt;b&gt;this(0, 0, 0, 0);&lt;/b&gt;
    }
    public Rectangle(int width, int height) {
        &lt;b&gt;this(0, 0, width, height);&lt;/b&gt;
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}
&lt;/pre&gt;
&lt;/div&gt;
This class contains a set of constructors. Each constructor initializes some or all of the rectangle&#39;s member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.&lt;br /&gt;
If present, the invocation of another constructor must be the first line in the constructor.&lt;br /&gt;
&lt;br /&gt;
Source: ORACLE</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/317828237915248770/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-20-more-on-classes-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/317828237915248770'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/317828237915248770'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2013/05/java-tutorial-20-more-on-classes-using.html' title='Java Tutorial 21: More on Classes-Using the this Keyword'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-2824049363161847049</id><published>2012-04-30T14:19:00.001+01:00</published><updated>2013-05-17T09:25:07.957+01:00</updated><title type='text'>Java Tutorial 20: More on Classes-Returning a Value from a Method</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9VxZCu3NboYQ5i250DAjc4nssLXg0RpzFrtusg5Qh8JDg8i8XQ-Stx3jpWgXAPVeOk2Y9-iUBJSokPGhQyFOCSnREJlfvzke9E_plhfT7CKSz0xsYqGQ_Nj6VuEUVO9JDMnAZql5aExGG/s1600/classes-hierarchy.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


Returning a Value from a Method&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
A method returns to the code that invoked it when it&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; completes all the
statements in the method,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; reaches a return
statement, or&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; throws an
exception (covered later),&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
whichever occurs first.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You declare a method&#39;s return type in its method
declaration. Within the body of the method, you use the return statement to
return the value.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Any method declared &lt;u&gt;void&lt;/u&gt; doesn&#39;t return a value. It
does not need to contain a return statement, but it may do so. In such a case,
a return statement can be used to branch out of a control flow block and exit
the method and is simply used like this:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
return;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
If you try to return a value from a method that is declared
void, you will get a compiler error.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Any method that is not declared void must contain a return
statement with a corresponding return value, like this:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
return returnValue;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The data type of the return value must match the method&#39;s
declared return type; you can&#39;t return an integer value from a method declared
to return a boolean.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The getArea() method in the Rectangle class discussed earlier
returns an integer:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // a method for
computing the area of the rectangle&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int
getArea() {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return width *
height;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
This method returns the integer that the expression
width*height evaluates to.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The getArea method returns a primitive type. A method can
also return a reference type. For example, in a program to manipulate Bicycle
objects, we might have a method like this:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public Bicycle seeWhosFastest(Bicycle myBike, Bicycle
yourBike,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Environment env) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Bicycle fastest;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // code to
calculate which bike is faster, given each bike&#39;s gear &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // and cadence and
given the environment (terrain and wind)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return fastest;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


Returning a Class or Interface&lt;/h3&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


&amp;nbsp;&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;i style=&quot;mso-bidi-font-style: normal;&quot;&gt;If this section
confuses you, skip it and return to it after you have finished the lesson on
interfaces and inheritance.&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
When a method uses a class name as its return type, such as
whosFastest does, the class of the type of the returned object must be either a
subclass of, or the exact class of, the return type. Suppose that you have a
class hierarchy in which ImaginaryNumber is a subclass of java.lang.Number,
which is in turn a subclass of Object, as illustrated in the following figure.&lt;/div&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9VxZCu3NboYQ5i250DAjc4nssLXg0RpzFrtusg5Qh8JDg8i8XQ-Stx3jpWgXAPVeOk2Y9-iUBJSokPGhQyFOCSnREJlfvzke9E_plhfT7CKSz0xsYqGQ_Nj6VuEUVO9JDMnAZql5aExGG/s1600/classes-hierarchy.gif&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9VxZCu3NboYQ5i250DAjc4nssLXg0RpzFrtusg5Qh8JDg8i8XQ-Stx3jpWgXAPVeOk2Y9-iUBJSokPGhQyFOCSnREJlfvzke9E_plhfT7CKSz0xsYqGQ_Nj6VuEUVO9JDMnAZql5aExGG/s1600/classes-hierarchy.gif&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Now suppose that you have a method declared to return a
Number:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public Number returnANumber() {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The returnANumber method can return an ImaginaryNumber but
not an Object. ImaginaryNumber is a Number because it&#39;s a subclass of Number.
However, an Object is not necessarily a Number — it could be a String or
another type.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can override a method and define it to return a subclass
of the original method, like this:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public ImaginaryNumber returnANumber() {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
This technique, called covariant return type, means that the
return type is allowed to vary in the same direction as the subclass.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Note: You also can use interface names as return types. In
this case, the object returned must implement the specified interface.&lt;br /&gt;
&lt;br /&gt;
Source: ORACLE&lt;br /&gt;
&lt;br /&gt;
&lt;!--[if gte mso 9]&gt;&lt;xml&gt;
 &lt;o:OfficeDocumentSettings&gt;
  &lt;o:AllowPNG/&gt;
 &lt;/o:OfficeDocumentSettings&gt;
&lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;
 &lt;w:WordDocument&gt;
  &lt;w:View&gt;Normal&lt;/w:View&gt;
  &lt;w:Zoom&gt;0&lt;/w:Zoom&gt;
  &lt;w:TrackMoves/&gt;
  &lt;w:TrackFormatting/&gt;
  &lt;w:PunctuationKerning/&gt;
  &lt;w:ValidateAgainstSchemas/&gt;
  &lt;w:SaveIfXMLInvalid&gt;false&lt;/w:SaveIfXMLInvalid&gt;
  &lt;w:IgnoreMixedContent&gt;false&lt;/w:IgnoreMixedContent&gt;
  &lt;w:AlwaysShowPlaceholderText&gt;false&lt;/w:AlwaysShowPlaceholderText&gt;
  &lt;w:DoNotPromoteQF/&gt;
  &lt;w:LidThemeOther&gt;EN-GB&lt;/w:LidThemeOther&gt;
  &lt;w:LidThemeAsian&gt;ZH-CN&lt;/w:LidThemeAsian&gt;
  &lt;w:LidThemeComplexScript&gt;X-NONE&lt;/w:LidThemeComplexScript&gt;
  &lt;w:Compatibility&gt;
   &lt;w:BreakWrappedTables/&gt;
   &lt;w:SnapToGridInCell/&gt;
   &lt;w:WrapTextWithPunct/&gt;
   &lt;w:UseAsianBreakRules/&gt;
   &lt;w:DontGrowAutofit/&gt;
   &lt;w:SplitPgBreakAndParaMark/&gt;
   &lt;w:EnableOpenTypeKerning/&gt;
   &lt;w:DontFlipMirrorIndents/&gt;
   &lt;w:OverrideTableStyleHps/&gt;
   &lt;w:UseFELayout/&gt;
  &lt;/w:Compatibility&gt;
  &lt;m:mathPr&gt;
   &lt;m:mathFont m:val=&quot;Cambria Math&quot;/&gt;
   &lt;m:brkBin m:val=&quot;before&quot;/&gt;
   &lt;m:brkBinSub m:val=&quot;&amp;#45;-&quot;/&gt;
   &lt;m:smallFrac m:val=&quot;off&quot;/&gt;
   &lt;m:dispDef/&gt;
   &lt;m:lMargin m:val=&quot;0&quot;/&gt;
   &lt;m:rMargin m:val=&quot;0&quot;/&gt;
   &lt;m:defJc m:val=&quot;centerGroup&quot;/&gt;
   &lt;m:wrapIndent m:val=&quot;1440&quot;/&gt;
   &lt;m:intLim m:val=&quot;subSup&quot;/&gt;
   &lt;m:naryLim m:val=&quot;undOvr&quot;/&gt;
  &lt;/m:mathPr&gt;&lt;/w:WordDocument&gt;
&lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;
 &lt;w:LatentStyles DefLockedState=&quot;false&quot; DefUnhideWhenUsed=&quot;true&quot;
  DefSemiHidden=&quot;true&quot; DefQFormat=&quot;false&quot; DefPriority=&quot;99&quot;
  LatentStyleCount=&quot;267&quot;&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;0&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Normal&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;heading 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 7&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 8&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;9&quot; QFormat=&quot;true&quot; Name=&quot;heading 9&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 7&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 8&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; Name=&quot;toc 9&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;35&quot; QFormat=&quot;true&quot; Name=&quot;caption&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;10&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Title&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;1&quot; Name=&quot;Default Paragraph Font&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;11&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Subtitle&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;22&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Strong&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;20&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Emphasis&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;59&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Table Grid&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; UnhideWhenUsed=&quot;false&quot; Name=&quot;Placeholder Text&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;1&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;No Spacing&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; UnhideWhenUsed=&quot;false&quot; Name=&quot;Revision&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;34&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;List Paragraph&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;29&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Quote&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;30&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Intense Quote&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3 Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid Accent 1&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3 Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid Accent 2&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3 Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid Accent 3&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3 Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid Accent 4&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3 Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid Accent 5&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;60&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Shading Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;61&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light List Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;62&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Light Grid Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;63&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 1 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;64&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Shading 2 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;65&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 1 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;66&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium List 2 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;67&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 1 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;68&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 2 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;69&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Medium Grid 3 Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;70&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Dark List Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;71&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Shading Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;72&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful List Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;73&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; Name=&quot;Colorful Grid Accent 6&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;19&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Subtle Emphasis&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;21&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Intense Emphasis&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;31&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Subtle Reference&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;32&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Intense Reference&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;33&quot; SemiHidden=&quot;false&quot;
   UnhideWhenUsed=&quot;false&quot; QFormat=&quot;true&quot; Name=&quot;Book Title&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;37&quot; Name=&quot;Bibliography&quot;/&gt;
  &lt;w:LsdException Locked=&quot;false&quot; Priority=&quot;39&quot; QFormat=&quot;true&quot; Name=&quot;TOC Heading&quot;/&gt;
 &lt;/w:LatentStyles&gt;
&lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if gte mso 10]&gt;
&lt;style&gt;
 /* Style Definitions */
 table.MsoNormalTable
 {mso-style-name:&quot;Table Normal&quot;;
 mso-tstyle-rowband-size:0;
 mso-tstyle-colband-size:0;
 mso-style-noshow:yes;
 mso-style-priority:99;
 mso-style-parent:&quot;&quot;;
 mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
 mso-para-margin-top:0cm;
 mso-para-margin-right:0cm;
 mso-para-margin-bottom:10.0pt;
 mso-para-margin-left:0cm;
 line-height:115%;
 mso-pagination:widow-orphan;
 font-size:11.0pt;
 font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;
 mso-ascii-font-family:Calibri;
 mso-ascii-theme-font:minor-latin;
 mso-hansi-font-family:Calibri;
 mso-hansi-theme-font:minor-latin;
 mso-bidi-font-family:&quot;Times New Roman&quot;;
 mso-bidi-theme-font:minor-bidi;}
&lt;/style&gt;
&lt;![endif]--&gt;

&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/2824049363161847049/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-20-more-on-classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/2824049363161847049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/2824049363161847049'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-20-more-on-classes.html' title='Java Tutorial 20: More on Classes-Returning a Value from a Method'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9VxZCu3NboYQ5i250DAjc4nssLXg0RpzFrtusg5Qh8JDg8i8XQ-Stx3jpWgXAPVeOk2Y9-iUBJSokPGhQyFOCSnREJlfvzke9E_plhfT7CKSz0xsYqGQ_Nj6VuEUVO9JDMnAZql5aExGG/s72-c/classes-hierarchy.gif" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-2116476668470144898</id><published>2012-04-26T16:25:00.001+01:00</published><updated>2013-05-17T09:15:01.253+01:00</updated><title type='text'>Java Tutorial 19: Object-Creating Objects</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
A typical Java program creates many objects, which as you
know, interact by invoking methods. Through these object interactions, a
program can carry out various tasks, such as implementing a GUI, running an
animation, or sending and receiving information over a network. Once an object
has completed the work for which it was created, its resources are recycled for
use by other objects.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Here&#39;s a small program, called CreateObjectDemo, that
creates three objects: one Point object and two Rectangle objects. You will
need all three source files to compile this program. Don’t worry if you cannot
understand everything, we will come back later. Now you just need to copy the
code and run the program.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;u&gt;To do this, you will need to create a new project in Eclipse
then create 3 classes under the new Java project and name them Point, Rectangle
and &amp;nbsp;CreateObjectDemo. &lt;/u&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In class Point, type the code below:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
public
class Point {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int x = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int y = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a constructor!&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Point(int a, int b) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = a;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y = b;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In class Rectangle, type the code below:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;public class Rectangle {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int width = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int height = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Point origin;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // four constructors&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = new Point(0, 0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle(Point p) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = p;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle(int w, int h) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = new Point(0, 0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; width = w;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; height = h;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle(Point p, int w, int h) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = p;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; width = w;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; height = h;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a method for moving the rectangle&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void move(int x, int y) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin.x = x;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin.y = y;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a method for computing the area of the rectangle&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int getArea() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return width * height;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: Calibri, sans-serif; font-size: 11pt;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In class CreateObjectDemo, type the code below:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public class CreateObjectDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //
Declare and create a point object and two rectangle objects.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Point
originOne = new Point(23, 94);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rectangle
rectOne = new &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Rectangle(originOne, 100, 200);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rectangle
rectTwo =&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new
Rectangle(50, 100);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // display rectOne&#39;s width, height, and
area&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Width of rectOne: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
+ rectOne.width);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Height of rectOne: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;+ rectOne.height);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Area of rectOne: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
+ rectOne.getArea());&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // set rectTwo&#39;s position&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rectTwo.origin
= originOne;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // display rectTwo&#39;s position&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;X Position of rectTwo: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
+ rectTwo.origin.x);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Y Position of rectTwo: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
+ rectTwo.origin.y);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // move rectTwo and display &amp;nbsp;its new position&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
rectTwo.move(40, 72);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;X Position of rectTwo: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
+ rectTwo.origin.x);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Y Position of rectTwo: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
+ rectTwo.origin.y);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
This program creates, manipulates, and displays information
about various objects. Here&#39;s the output:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Width of rectOne: 100&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Height of rectOne: 200&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Area of rectOne: 20000&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
X Position of rectTwo: 23&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Y Position of rectTwo: 94&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
X Position of rectTwo: 40&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Y Position of rectTwo: 72&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The following three sections use the above example to
describe the life cycle of an object within a program. From them, you will
learn how to write code that creates and uses objects in your own programs. You
will also learn how the system cleans up after an object when its life has
ended.&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;


Creating Objects&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
As you
know, a class provides the blueprint for objects; you create an object from a
class. Each of the following statements taken from the&lt;a href=&quot;http://docs.oracle.com/javase/tutorial/java/javaOO/examples/CreateObjectDemo.java&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: windowtext; text-decoration: none;&quot;&gt;CreateObjectDemo&lt;/span&gt;&lt;/a&gt;&amp;nbsp;program creates an object and assigns it
to a variable:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;u&gt;Point originOne&lt;/u&gt; = new Point(23, 94);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;u&gt;Rectangle rectOne&lt;/u&gt; = new Rectangle(originOne, 100, 200);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;u&gt;Rectangle rectTwo&lt;/u&gt; = new Rectangle(50, 100);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The first line creates an object of the&amp;nbsp;&lt;a href=&quot;http://docs.oracle.com/javase/tutorial/java/javaOO/examples/Point.java&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: windowtext; text-decoration: none;&quot;&gt;Point&lt;/span&gt;&lt;/a&gt;&amp;nbsp;class, and the second and third lines each create
an object of the&amp;nbsp;&lt;a href=&quot;http://docs.oracle.com/javase/tutorial/java/javaOO/examples/Rectangle.java&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: windowtext; text-decoration: none;&quot;&gt;Rectangle&lt;/span&gt;&lt;/a&gt;&amp;nbsp;class.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Each of
these statements has three parts:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 51pt; text-align: justify; text-indent: -18pt;&quot;&gt;
1.&lt;span style=&quot;font-size: 7pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;Declaration: The code underlined&amp;nbsp;are all
variable declarations that associate a variable name with an object type.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 51pt; text-align: justify; text-indent: -18pt;&quot;&gt;
2.&lt;span style=&quot;font-size: 7pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;Instantiation: The&amp;nbsp;new&amp;nbsp;keyword is a
Java operator that creates the object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 51pt; text-align: justify; text-indent: -18pt;&quot;&gt;
3.&lt;span style=&quot;font-size: 7pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;Initialization: The&amp;nbsp;new&amp;nbsp;operator is
followed by a call to a constructor, which initializes the new object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 51pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;


Declaring a Variable to Refer to an Object&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=2118932319280195164&quot; name=&quot;declaring&quot;&gt;&lt;/a&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Previously,
you learned that to declare a variable, you write:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
type name;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
This
notifies the compiler that you will use&amp;nbsp;name&amp;nbsp;to refer to data whose
type is&amp;nbsp;type. With a primitive variable, this declaration also reserves
the proper amount of memory for the variable.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
You can
also declare a reference variable on its own line. For example:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Point
originOne;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
If you
declare&amp;nbsp;originOne&amp;nbsp;like this, its value will be undetermined until an
object is actually created and assigned to it. Simply declaring a reference
variable does not create an object. For that, you need to use the&amp;nbsp;new&amp;nbsp;operator,
as described in the next section. You must assign an object to&amp;nbsp;originOne&amp;nbsp;before
you use it in your code. Otherwise, you will get a compiler error.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
A variable
in this state, which currently references no object, can be illustrated as
follows (the variable name,&amp;nbsp;originOne, plus a reference pointing to
nothing):&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUpdoOdcjW8cq7HFJ-BflITcU30rjmxJzomDg9cGZGRUKcQat2BP0hXEoqdZFfCFCw5w11ULoaUSst9w51exSzZug4x-8NTSaLl0a76PtApP61LUMtqMeD1Fnv8t8IWWYp9eoVkUYuVwg5/s1600/1.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUpdoOdcjW8cq7HFJ-BflITcU30rjmxJzomDg9cGZGRUKcQat2BP0hXEoqdZFfCFCw5w11ULoaUSst9w51exSzZug4x-8NTSaLl0a76PtApP61LUMtqMeD1Fnv8t8IWWYp9eoVkUYuVwg5/s1600/1.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;


Instantiating a Class&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=2118932319280195164&quot; name=&quot;instantiating&quot;&gt;&lt;/a&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The&amp;nbsp;new&amp;nbsp;operator instantiates a class by allocating memory for a
new object and returning a reference to that memory. The&amp;nbsp;new&amp;nbsp;operator
also invokes the object constructor.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;hr align=&quot;center&quot; size=&quot;2&quot; width=&quot;100%&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
Note:&amp;nbsp;The phrase
&quot;instantiating a class&quot; means the same thing as &quot;creating an
object.&quot; When you create an object, you are creating an
&quot;instance&quot; of a class, therefore &quot;instantiating&quot; a class.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;hr align=&quot;center&quot; size=&quot;2&quot; width=&quot;100%&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The&amp;nbsp;new&amp;nbsp;operator
requires a single, postfix argument: a call to a constructor. The name of the
constructor provides the name of the class to instantiate.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The&amp;nbsp;new&amp;nbsp;operator
returns a reference to the object it created. This reference is usually
assigned to a variable of the appropriate type, like:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
Point
originOne = new Point(23, 94);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The
reference returned by the&amp;nbsp;new&amp;nbsp;operator does not have to be assigned
to a variable. It can also be used directly in an expression. For example:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
int
height = new Rectangle().height;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
This
statement will be discussed in the next section.&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=2118932319280195164&quot; name=&quot;initializing&quot;&gt;&lt;/a&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;


Initializing an Object&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Here&#39;s the
code for the&amp;nbsp;Point&amp;nbsp;class:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
public
class Point {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int x = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int y = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; //constructor&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Point(int a, int b) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = a;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y = b;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
This class
contains a single constructor. You can recognize a constructor because its
declaration uses the same name as the class and it has no return type. The
constructor in the&amp;nbsp;Point&amp;nbsp;class takes two integer arguments, as
declared by the code&amp;nbsp;(int a, int b). The following statement provides 23
and 94 as values for those arguments:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
Point
originOne = new Point(23, 94);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The result
of executing this statement can be illustrated in the next figure:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM_oF-5mIl0woOaUGyP9g2SLaJT0zj07Dx8fwy0Vm9BFUzoixnyL0DNZcWxRgiam7IPrKt5BMmOYQqHt-mRX1RWVvW68GWz1tORgq-cRz-jAU7o75GTLzT-3aqY7OTwuXTF38YmJmdfYPy/s1600/2.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;194&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM_oF-5mIl0woOaUGyP9g2SLaJT0zj07Dx8fwy0Vm9BFUzoixnyL0DNZcWxRgiam7IPrKt5BMmOYQqHt-mRX1RWVvW68GWz1tORgq-cRz-jAU7o75GTLzT-3aqY7OTwuXTF38YmJmdfYPy/s320/2.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Here&#39;s the
code for the&amp;nbsp;Rectangle&amp;nbsp;class, which contains four constructors:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
public
class Rectangle {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int width = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int height = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Point origin;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // four constructors&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = new Point(0, 0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle(Point p) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = p;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle(int w, int h) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;origin = new Point(0, 0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; width = w;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; height = h;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Rectangle(Point p, int w, int h) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin = p;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; width = w;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; height = h;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // a method for moving the rectangle&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void move(int x, int y) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin.x = x;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; origin.y = y;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // a method for computing the area &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // of the rectangle&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int getArea() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return width * height;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Each
constructor lets you provide initial values for the rectangle&#39;s size and width,
using both primitive and reference types. If a class has multiple constructors,
they must have different signatures. The Java compiler differentiates the
constructors based on the number and the type of the arguments. When the Java
compiler encounters the following code, it knows to call the constructor in the&amp;nbsp;Rectangle&amp;nbsp;class
that requires a&amp;nbsp;Point&amp;nbsp;argument followed by two integer arguments:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Rectangle
rectOne = new Rectangle(originOne, 100, 200);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
This calls
one of&amp;nbsp;Rectangle&#39;s constructors that initializes&amp;nbsp;origin&amp;nbsp;to&amp;nbsp;originOne.
Also, the constructor sets&amp;nbsp;width&amp;nbsp;to 100 and&amp;nbsp;height&amp;nbsp;to 200.
Now there are two references to the same&amp;nbsp;Point object—an object can have
multiple references to it, as shown in the next figure:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNjF91-LxZcQQhWfxTVxLSQZLJECZswyAgZzxoigeZ6GS_JYaLhiCtyaeJ0lp7HMnZ8NKb8UjumRoya2qeA718vsc4Ntl5ETLhDOGx8Jrij_-7yJcU4xRKUxllgdGDIshjA9CSG9QiSgyP/s1600/3.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;282&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNjF91-LxZcQQhWfxTVxLSQZLJECZswyAgZzxoigeZ6GS_JYaLhiCtyaeJ0lp7HMnZ8NKb8UjumRoya2qeA718vsc4Ntl5ETLhDOGx8Jrij_-7yJcU4xRKUxllgdGDIshjA9CSG9QiSgyP/s320/3.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The
following line of code calls the&amp;nbsp;Rectangle&amp;nbsp;constructor that requires
two integer arguments, which provide the initial values for&amp;nbsp;widthand&amp;nbsp;height.
If you inspect the code within the constructor, you will see that it creates a
new&amp;nbsp;Point&amp;nbsp;object whose&amp;nbsp;x&amp;nbsp;and&amp;nbsp;y&amp;nbsp;values are
initialized to 0:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Rectangle
rectTwo = new Rectangle(50, 100);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The&amp;nbsp;Rectangle&amp;nbsp;constructor
used in the following statement doesn&#39;t take any arguments, so it&#39;s called a&amp;nbsp;no-argument
constructor:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Rectangle
rect = new Rectangle();&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
All classes
have at least one constructor. If a class does not explicitly declare any, the
Java compiler automatically provides a no-argument constructor, called the&amp;nbsp;default
constructor. This default constructor calls the class parent&#39;s no-argument
constructor, or the&amp;nbsp;Objectconstructor if the class has no other parent. If
the parent has no constructor (Object&amp;nbsp;does have one), the compiler will
reject the program.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/2116476668470144898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/typical-java-program-creates-many.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/2116476668470144898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/2116476668470144898'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/typical-java-program-creates-many.html' title='Java Tutorial 19: Object-Creating Objects'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUpdoOdcjW8cq7HFJ-BflITcU30rjmxJzomDg9cGZGRUKcQat2BP0hXEoqdZFfCFCw5w11ULoaUSst9w51exSzZug4x-8NTSaLl0a76PtApP61LUMtqMeD1Fnv8t8IWWYp9eoVkUYuVwg5/s72-c/1.gif" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-1414932362568165226</id><published>2012-04-22T15:23:00.000+01:00</published><updated>2012-04-22T15:30:31.129+01:00</updated><title type='text'>Understanding constructors-How constructors differ from methods</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
Saying that a constructor is a method is like saying that the Australian
 platypus is just another mammal. To understand the
                           platypus, it is important to know how it is 
different from other mammals. To understand the constructor, it is 
similarly important
                           to understand how it differs from a method. 
Any student of Java, especially one studying for certification, needs to
 know
                           those differences; in this article, I will 
concretely spell them out. Table 1, at the end of this article, 
summarizes the
                           key constructor/method distinctions. &lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

Purpose and function&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Constructors have one purpose in life: to create an instance of a class. This can also be called creating an object, as in:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap4&quot;&gt;
&lt;pre&gt;Platypus p1 = new Platypus();
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The purpose of methods, by contrast, is much more general. A method&#39;s basic function is to execute Java code.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

Signature differences&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Constructors and methods differ in three aspects of the &lt;u&gt;signature, modifiers, return type, and name&lt;/u&gt;. Like methods, constructors
                              can have any of the access modifiers: public, protected, private, or none (often called &lt;i&gt;package&lt;/i&gt; or &lt;i&gt;friendly&lt;/i&gt;). Unlike methods, constructors can take only access modifiers. Therefore, constructors cannot be &lt;code&gt;abstract&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt;, &lt;code&gt;native&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt;, or &lt;code&gt;synchronized&lt;/code&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The return types are very different too. Methods can have any valid return type, or no return type, in which case the return
                              type is given as &lt;code&gt;void&lt;/code&gt;. Constructors have no return type, not even &lt;code&gt;void&lt;/code&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Finally, in terms of the signature, 
methods and constructors have different names. Constructors have the 
same name as their
                              class; by convention, methods use names 
other than the class name. If the Java program follows normal &lt;u&gt;
conventions, methods
                              will start with a lowercase letter, 
constructors with an uppercase letter. &lt;/u&gt;Also, &lt;u&gt;constructor names are 
usually nouns because
                              class names are usually nouns; method 
names usually indicate actions.&amp;nbsp;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

The use of &quot;this&quot;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Constructors and methods use the keyword &lt;code&gt;this&lt;/code&gt; quite differently. A method uses &lt;code&gt;this&lt;/code&gt; to refer to the instance of the class that is executing the method. Static methods do not use &lt;code&gt;this&lt;/code&gt;; they do not belong to a class instance, so &lt;code&gt;this&lt;/code&gt; would have nothing to reference. Static methods belong to the class as a whole, rather than to an instance. Constructors
                              use &lt;code&gt;this&lt;/code&gt; to refer to another constructor in the same class with a different parameter list. Study the following code:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap28&quot;&gt;
&lt;pre&gt;public class Platypus {
       String name;
       Platypus(String input) {
               name = input;
       }
       Platypus() {
               this(&quot;John/Mary Doe&quot;);
       }
       public static void main(String args[]) {
               Platypus p1 = new Platypus(&quot;digger&quot;);
               Platypus p2 = new Platypus();
       }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the code, there are two constructors. The first takes a &lt;code&gt;String&lt;/code&gt; input to name the instance. The second, taking no parameters, calls the first constructor by the default name &lt;code&gt;&quot;John/Mary Doe&quot;&lt;/code&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If a constructor uses &lt;code&gt;this&lt;/code&gt;, it must be in the constructor&#39;s first line; ignoring this rule will cause the compiler to object.
                           &lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

The use of &quot;super&quot;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Methods and constructors both use &lt;code&gt;super&lt;/code&gt; to refer to a superclass, but in different ways. Methods use &lt;code&gt;super&lt;/code&gt; to execute an overridden method in the superclass, as the following example illustrates:
                              
                           age 2 of 2&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap40&quot;&gt;
&lt;pre&gt;class Mammal {
       void getBirthInfo() {
               System.out.println(&quot;born alive.&quot;);
       }
}
class Platypus extends Mammal {
       void getBirthInfo() {
               System.out.println(&quot;hatch from eggs&quot;);
               System.out.print(&quot;a mammal normally is &quot;);
               super.getBirthInfo();
       }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the above program, the call to &lt;code&gt;super.getBirthInfo()&lt;/code&gt; calls the overridden method of the &lt;code&gt;Mammal&lt;/code&gt; superclass.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Constructors use &lt;code&gt;super&lt;/code&gt; to invoke the superclass&#39;s constructor. If a constructor uses &lt;code&gt;super&lt;/code&gt;, it must use it in the first line; otherwise, the compiler will complain. An example follows:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap49&quot;&gt;
&lt;pre&gt;public class SuperClassDemo {
        SuperClassDemo() {}
}
class Child extends SuperClassDemo {
       Child() {
               super();
       }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the above (and trivial!) example, the constructor &lt;code&gt;Child()&lt;/code&gt; includes a call to &lt;code&gt;super&lt;/code&gt;, which causes the class &lt;code&gt;SuperClassDemo&lt;/code&gt; to be instantiated, in addition to the &lt;code&gt;Child&lt;/code&gt; class.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

Compiler-supplied code&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The new Java programmer may stumble when 
the compiler automatically supplies code for constructors. This happens 
if you write
                              a class with no constructors; the compiler
 will automatically supply a no-argument constructor for you. Thus, if 
you write:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap59&quot;&gt;
&lt;pre&gt;public class Example {}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
it is functionally equivalent to writing:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap63&quot;&gt;
&lt;pre&gt;public class Example {
       Example() {}
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The compiler also automatically supplies code when you do not use &lt;code&gt;super&lt;/code&gt; (using zero or more parameters) as the first line of a constructor. In this case, the computer automatically inserts &lt;code&gt;super&lt;/code&gt;. Thus, if you write:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap69&quot;&gt;
&lt;pre&gt;public class TestConstructors {
       TestConstructors() {}
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
it is functionally equivalent to writing:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap73&quot;&gt;
&lt;pre&gt;public class TestConstructors {
       TestConstructors() {
               &lt;code&gt;super&lt;/code&gt;;
       }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The sharp-eyed beginner may wonder how the above program can call the parent class&#39;s constructor when &lt;code&gt;TestConstructor&lt;/code&gt; is not extending any class. The answer is that Java extends the &lt;code&gt;Object&lt;/code&gt;
 class when you do not explicitly extend a class. The compiler 
automatically supplies a no-argument constructor if no constructor
                              is explicitly declared, and automatically 
supplies a no-argument &lt;code&gt;super&lt;/code&gt; call when a constructor has no explicit call to &lt;code&gt;super&lt;/code&gt;. So the following two code snippets are functionally equivalent:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap82&quot;&gt;
&lt;pre&gt;public class Example {} 
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
and&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap86&quot;&gt;
&lt;pre&gt;public class Example {
       Example() {
             &lt;code&gt;super&lt;/code&gt;;
       }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

Inheritance&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
What is wrong with the following scenario? A lawyer is reading the will of &lt;code&gt;A. Class&lt;/code&gt;. Members of the &lt;code&gt;Class&lt;/code&gt; family are gathered around a large conference table, some sobbing gently. The lawyer reads, &quot;I, &lt;code&gt;A. Class&lt;/code&gt;, being of sound mind and body, leave all my constructors to my children.&quot;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The problem is that constructors cannot be inherited. Fortunately for the &lt;code&gt;Class&lt;/code&gt; children, they will automatically inherit any of their parents&#39; methods, so the &lt;code&gt;Class&lt;/code&gt; children will not become totally destitute.
                           &lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Remember, Java methods are inherited, constructors are not. Consider the following class:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;codewrap&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;div id=&quot;codewrap99&quot;&gt;
&lt;pre&gt;public class Example {
        public void sayHi {
                system.out.println(&quot;Hi&quot;);
        }
        Example() {}
}
public class SubClass extends Example {
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The &lt;code&gt;SubClass&lt;/code&gt; class automatically inherits the &lt;code&gt;sayHi&lt;/code&gt; method found in the parent class. However, the constructor &lt;code&gt;Example()&lt;/code&gt; is not inherited by the &lt;code&gt;SubClass&lt;/code&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;

Summarizing the differences&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Just as the platypus differs from the typical mammal, so too do constructors differ from methods; specifically in their purpose,
                              signature, and use of &lt;code&gt;this&lt;/code&gt; and &lt;code&gt;super&lt;/code&gt;.
 Additionally, constructors differ with respect to inheritance and 
compiler-supplied code. Keeping all these details straight
                              can be a chore; the following table 
provides a convenient summary of the salient points.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Differences Between Constructors and Methods&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWZYpdxaNBOrAy1g5s0bMkY96qqCdFnB28KSLJr1LDI3xNJM1xpotKN7OT0a19DN542JXQCQqEft6n-6SG-hM7_ErfqCX6CewL0OBc4ILDl6VCGRG41800iXRsyAsNhr3y_fPY9rSbJBm1/s1600/Capture.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;536&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWZYpdxaNBOrAy1g5s0bMkY96qqCdFnB28KSLJr1LDI3xNJM1xpotKN7OT0a19DN542JXQCQqEft6n-6SG-hM7_ErfqCX6CewL0OBc4ILDl6VCGRG41800iXRsyAsNhr3y_fPY9rSbJBm1/s640/Capture.PNG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Source: Javaworld&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/1414932362568165226/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/understanding-constructors-how.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/1414932362568165226'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/1414932362568165226'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/understanding-constructors-how.html' title='Understanding constructors-How constructors differ from methods'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWZYpdxaNBOrAy1g5s0bMkY96qqCdFnB28KSLJr1LDI3xNJM1xpotKN7OT0a19DN542JXQCQqEft6n-6SG-hM7_ErfqCX6CewL0OBc4ILDl6VCGRG41800iXRsyAsNhr3y_fPY9rSbJBm1/s72-c/Capture.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-4617391480860373930</id><published>2012-04-22T15:15:00.003+01:00</published><updated>2012-04-30T14:18:45.395+01:00</updated><title type='text'>Java Tutorial 18: Classes: Passing Information to a Method or a Constructor</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The declaration for a method or a constructor declares the
number and the type of the arguments for that method or constructor. For
example, the following is a method that computes the monthly payments for a
home loan, based on the amount of the loan, the interest rate, the length of
the loan (the number of periods), and the future value of the loan:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public double computePayment(&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
double loanAmt,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
double rate,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
double futureValue,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int
numPeriods) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; double interest =
rate / 100.0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; double partial1 =
Math.pow((1 + interest), &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -
numPeriods);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; double denominator
= (1 - partial1) / interest;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; double answer =
(-loanAmt / denominator)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -
((futureValue * partial1) / denominator);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return answer;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
This method has four parameters: the loan amount, the
interest rate, the future value and the number of periods. The first three are
double-precision floating point numbers, and the fourth is an integer. The
parameters are used in the method body and at runtime will take on the values
of the arguments that are passed in.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Note: Parameters refers to the list of variables in a method
declaration. Arguments are the actual values that are passed in when the method
is invoked. When you invoke a method, the arguments used must match the
declaration&#39;s parameters in type and order.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


Parameter Types&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can use any data type for a parameter of a method or a
constructor. This includes primitive data types, such as doubles, floats, and
integers, as you saw in the computePayment method, and reference data types,
such as objects and arrays.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Here&#39;s an example of a method that accepts an array as an
argument. In this example, the method creates a new Polygon object and
initializes it from an array of Point objects (assume that Point is a class
that represents an x, y coordinate):&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public Polygon polygonFrom(Point[] corners) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // method body
goes here&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Note: The Java programming language doesn&#39;t let you pass
methods into methods. But you can pass an object into a method and then invoke
the object&#39;s methods.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


Arbitrary Number of Arguments&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can use a construct called varargs to pass an arbitrary
number of values to a method. You use varargs when you don&#39;t know how many of a
particular type of argument will be passed to the method. It&#39;s a shortcut to
creating an array manually (the previous method could have used varargs rather
than an array).&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
To use varargs, you follow the type of the last parameter by
an ellipsis (three dots, ...), then a space, and the parameter name. The method
can then be called with any number of that parameter, including none.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public Polygon polygonFrom(Point... corners) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int numberOfSides
= corners.length;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; double
squareOfSide1, lengthOfSide1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; squareOfSide1 =
(corners[1].x - corners[0].x)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *
(corners[1].x - corners[0].x) &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +
(corners[1].y - corners[0].y)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *
(corners[1].y - corners[0].y);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; lengthOfSide1 =
Math.sqrt(squareOfSide1);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // more method
body code follows that creates and returns a &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // polygon
connecting the Points&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can see that, inside the method, corners is treated like
an array. The method can be called either with an array or with a sequence of
arguments. The code in the method body will treat the parameter as an array in
either case.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You will most commonly see varargs with the printing
methods; for example, this printf method:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public PrintStream printf(String format, Object... args)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
allows you to print an arbitrary number of objects. It can
be called like this:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
System.out.printf(&quot;%s: %d, %s%n&quot;, name, idnum,
address);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
or like this&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
System.out.printf(&quot;%s: %d, %s, %s, %s%n&quot;, name,
idnum, address, phone, email);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
or with yet a different number of arguments.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


Parameter Names&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
When you declare a parameter to a method or a constructor,
you provide a name for that parameter. This name is used within the method body
to refer to the passed-in argument.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The name of a parameter must be unique in its scope. It
cannot be the same as the name of another parameter for the same method or
constructor, and it cannot be the name of a local variable within the method or
constructor.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
A parameter can have the same name as one of the class&#39;s
fields. If this is the case, the parameter is said to shadow the field.
Shadowing fields can make your code difficult to read and is conventionally
used only within constructors and methods that set a particular field. For
example, consider the following Circle class and its setOrigin method:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public class Circle {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private int x, y,
radius;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void
setOrigin(int x, int y) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The Circle class has three fields: x, y, and radius. The
setOrigin method has two parameters, each of which has the same name as one of
the fields. Each method parameter shadows the field that shares its name. So
using the simple names x or y within the body of the method refers to the
parameter, not to the field. To access the field, you must use a qualified
name. This will be discussed later in this lesson in the section titled
&quot;Using the this Keyword.&quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;


Passing Primitive Data Type Arguments&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Primitive arguments, such as an int or a double, are passed
into methods by value. This means that any changes to the values of the
parameters exist only within the scope of the method. When the method returns,
the parameters are gone and any changes to them are lost. Here is an example:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public class PassPrimitiveByValue {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int x = 3;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // invoke
passMethod() with &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // x as
argument&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; passMethod(x);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // print x to
see if its &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // value has
changed&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;After invoking passMethod, x = &quot; + x);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // change
parameter in passMethod()&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
passMethod(int p) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p = 10;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
When you run this program, the output is:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
After invoking passMethod, x = 3&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Passing Reference Data Type Arguments&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Reference data type parameters, such as objects, are also
passed into methods by value. This means that when the method returns, the
passed-in reference still references the same object as before. However, the
values of the object&#39;s fields can be changed in the method, if they have the
proper access level.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
For example, consider a method in an arbitrary class that
moves Circle objects:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public void moveCircle(Circle circle, int deltaX, int
deltaY) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // code to move
origin of &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // circle to
x+deltaX, y+deltaY&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
circle.setX(circle.getX() + deltaX);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
circle.setY(circle.getY() + deltaY);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // code to assign
a new &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // reference to
circle&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; circle = new
Circle(0, 0);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Let the method be invoked with these arguments:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
moveCircle(myCircle, 23, 56)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Inside the method, circle initially refers to myCircle. The
method changes the x and y coordinates of the object that circle references
(i.e., myCircle) by 23 and 56, respectively. These changes will persist when
the method returns. Then circle is assigned a reference to a new Circle object
with x = y = 0. This reassignment has no permanence, however, because the
reference was passed in by value and cannot change. Within the method, the
object pointed to by circle has changed, but, when the method returns, myCircle
still references the same Circle object as before the method was called.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/4617391480860373930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/passing-information-to-method-or.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4617391480860373930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4617391480860373930'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/passing-information-to-method-or.html' title='Java Tutorial 18: Classes: Passing Information to a Method or a Constructor'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-8628498581017993323</id><published>2012-04-22T14:45:00.002+01:00</published><updated>2012-04-22T14:48:23.069+01:00</updated><title type='text'>Java Tutorial 17: Classes-Defining Methods and Providing Constructors for Your Classes</title><content type='html'>&lt;br /&gt;
&lt;h2 class=&quot;MsoNormal&quot;&gt;



Defining Methods&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Here is an example of a typical method declaration:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public double calculateAnswer(double wingSpan, int
numberOfEngines,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
double length, double grossTons) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; //do the
calculation here&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The only required elements of a method declaration are the
method&#39;s return type, name, a pair of parentheses, (), and a body between
braces, {}.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
More generally, method declarations have six components, in
order:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Modifiers—such as
public, private.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The return
type—the data type of the value returned by the method, or void if the method
does not return a value.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The method
name—the rules for field names apply to method names as well, but the
convention is a little different.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The parameter list
in parenthesis—a comma-delimited list of input parameters, preceded by their
data types, enclosed by parentheses, (). If there are no parameters, you must
use empty parentheses.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; An exception
list—to be discussed later.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The method body,
enclosed between braces—the method&#39;s code, including the declaration of local
variables, goes here.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Modifiers, return types, and parameters will be discussed
later in this lesson. Exceptions are discussed in a later lesson.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Definition: Two of the components of a method declaration
comprise the method signature—the method&#39;s name and the parameter types.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The signature of the method declared above is:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
calculateAnswer(double, int, double, double)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;



Naming a Method&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Although a method name can be any legal identifier, code
conventions restrict method names. By convention, method names should be a verb
in lowercase or a multi-word name that begins with a verb in lowercase,
followed by adjectives, nouns, etc. In multi-word names, the first letter of
each of the second and following words should be capitalized. Here are some
examples:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
run&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
runFast&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
getBackground&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
getFinalData&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
compareTo&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
setX&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
isEmpty&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Typically, a method has a unique name within its class.
However, a method might have the same name as other methods due to method
overloading which is not encouraged.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot;&gt;



Providing Constructors for Your Classes&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
A class contains constructors that are invoked to create
objects from the class blueprint. Constructor declarations look like method
declarations—except that they use the name of the class and have no return
type. For example, Bicycle has one constructor:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public Bicycle(int startCadence, int startSpeed, int
startGear) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; gear = startGear;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cadence =
startCadence;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; speed =
startSpeed;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
To create a new Bicycle object called myBike, a constructor
is called by the new operator:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Bicycle myBike = new Bicycle(30, 0, 8);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
new Bicycle(30, 0, 8) creates space in memory for the object
and initializes its fields.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Although Bicycle only has one constructor, it could have
others, including a no-argument constructor:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public Bicycle() {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; gear = 1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cadence = 10;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; speed = 0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Bicycle yourBike = new Bicycle(); invokes the no-argument
constructor to create a new Bicycle object called yourBike.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Both constructors could have been declared in Bicycle
because they have different argument lists. As with methods, the Java platform
differentiates constructors on the basis of the number of arguments in the list
and their types. You cannot write two constructors that have the same number
and type of arguments for the same class, because the platform would not be
able to tell them apart. Doing so causes a compile-time error.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You don&#39;t have to provide any constructors for your class,
but you must be careful when doing this. The compiler automatically provides a
no-argument, default constructor for any class without constructors. This
default constructor will call the no-argument constructor of the superclass. In
this situation, the compiler will complain if the superclass doesn&#39;t have a
no-argument constructor so you must verify that it does. If your class has no
explicit superclass, then it has an implicit superclass of Object, which does
have a no-argument constructor.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can use a superclass constructor yourself. The
MountainBike class at the beginning of this lesson did just that. This will be
discussed later, in the lesson on interfaces and inheritance.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can use access modifiers in a constructor&#39;s declaration
to control which other classes can call the constructor.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Note: If another class cannot call a MyClass constructor, it
cannot directly create MyClass objects.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/8628498581017993323/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/defining-methods-and-providing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8628498581017993323'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8628498581017993323'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/defining-methods-and-providing.html' title='Java Tutorial 17: Classes-Defining Methods and Providing Constructors for Your Classes'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-5435835446485866535</id><published>2012-04-22T14:08:00.000+01:00</published><updated>2012-04-22T14:50:20.438+01:00</updated><title type='text'>Java Tutorial 16: Classes-Declaring Classes and Declaring Member Variables</title><content type='html'>&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The&lt;a href=&quot;http://mmp669-android.blogspot.co.uk/2012/04/java-tutorial-3-object-oriented.html&quot; target=&quot;_blank&quot;&gt; introduction to object-oriented Programming Concepts&lt;/a&gt; used a bicycle class as an example, with racing bikes, mountain bikes,
and tandem bikes as subclasses. Here is sample code for a possible
implementation of a Bicycle class, to give you an overview of a class
declaration. Subsequent sections of this lesson will back up and explain class
declarations step by step. For the moment, don&#39;t concern yourself with the
details.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public class Bicycle {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
// the Bicycle class has three
fields&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int
cadence;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int gear;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int speed;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
// the Bicycle class has one
constructor&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Bicycle(int
startCadence, int startSpeed, int startGear) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gear =
startGear;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cadence =
startCadence;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; speed =
startSpeed;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // the Bicycle
class has four methods&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void
setCadence(int newValue) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cadence =
newValue;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void
setGear(int newValue) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gear =
newValue;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void
applyBrake(int decrement) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; speed -=
decrement;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void
speedUp(int increment) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; speed +=
increment;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
A class declaration for a MountainBike class that is a
subclass of Bicycle might look like this:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public class MountainBike extends Bicycle {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
// the MountainBike subclass has
one field&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int
seatHeight;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
// the MountainBike subclass has
one constructor&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 9.75pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public
MountainBike(int startHeight, int startCadence,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
int startSpeed, int startGear) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
super(startCadence, startSpeed, startGear);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; seatHeight =
startHeight;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // the
MountainBike subclass has one method&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void
setHeight(int newValue) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; seatHeight =
newValue;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
MountainBike inherits all the fields and methods of Bicycle
and adds the field seatHeight and a method to set it (mountain bikes have seats
that can be moved up and down as the terrain demands).&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;


&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;


&amp;nbsp;Declaring Classes &lt;/h2&gt;
&lt;br /&gt;
You&#39;ve seen classes defined in the following way:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class MyClass {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // field,
constructor, and &amp;nbsp;method declarations&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
This is a class declaration. The class body (the area
between the braces) contains all the code that provides for the life cycle of
the objects created from the class: &lt;u&gt;constructors for initializing new objects,
declarations for the fields that provide the state of the class and its
objects, and methods to implement the behavior of the class and its objects.&lt;/u&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The preceding class declaration is a minimal one. It
contains only those components of a class declaration that are required. You
can provide more information about the class, such as the name of its
superclass, whether it implements any interfaces, and so on, at the start of the
class declaration. For example,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class MyClass extends MySuperClass implements YourInterface
{&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // field,
constructor, and&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // method
declarations&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
means that MyClass is a subclass of MySuperClass and that it
implements the YourInterface interface.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can also add modifiers like public or private at the
very beginning—so you can see that the opening line of a class declaration can
become quite complicated. The modifiers &lt;u&gt;public and private&lt;/u&gt;, which determine
what other classes can access MyClass, are discussed later . The
lesson on interfaces and inheritance will explain how and why you would use the
extends and implements keywords in a class declaration. For the moment you do
not need to worry about these extra complications.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In general, class declarations can include these components,
in order:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Modifiers such as
public, private, and a number of others that you will encounter later.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The class name,
with the initial letter capitalized by convention.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The name of the
class&#39;s parent (superclass), if any, preceded by the keyword extends. A class
can only extend (subclass) one parent.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; A comma-separated
list of interfaces implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The class body,
surrounded by braces, {}.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h2 class=&quot;MsoNormal&quot;&gt;



Declaring Member Variables&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
There are several kinds of variables:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;u&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Member variables
in a class—these are called fields.&lt;/u&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;u&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Variables in a method
or block of code—these are called local variables.&lt;/u&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;u&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Variables in
method declarations—these are called parameters.&lt;/u&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The Bicycle class uses the following lines of code to define
its fields:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public int cadence;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public int gear;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
public int speed;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Field declarations are composed of three components, in
order:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Zero or more
modifiers, such as public or private.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; The field&#39;s type.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; The field&#39;s name.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The fields of Bicycle are named cadence, gear, and speed and
are all of data type integer (int). The public keyword identifies these fields
as public members, accessible by any object that can access the class.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Access Modifiers&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The first (left-most) modifier used lets you control what
other classes have access to a member field. For the moment, consider only
public and private. &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public
modifier—the field is accessible from all classes.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private
modifier—the field is accessible only within its own class.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In the spirit of encapsulation, it is common to make fields
private. This means that they can only be directly accessed from the Bicycle
class. &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;



Types&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
All variables must have a type. You can use primitive types
such as int, float, boolean, etc. Or you can use reference types, such as
strings, arrays, or objects.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot;&gt;



Variable Names&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
All variables, whether they are fields, local variables, or
parameters, follow the same naming rules and conventions that were covered in
the Language Basics lesson, Variables—Naming.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In this lesson, be aware that the same naming rules and
conventions are used for method and class names, except that&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&lt;u&gt;&amp;nbsp; &lt;/u&gt;&lt;u&gt;the first letter
of a class name should be capitalized, and&lt;/u&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;u&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; the first (or
only) word in a method name should be a verb.&lt;/u&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/5435835446485866535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-17-classes-declaring.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/5435835446485866535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/5435835446485866535'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-17-classes-declaring.html' title='Java Tutorial 16: Classes-Declaring Classes and Declaring Member Variables'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-6647760764282444815</id><published>2012-04-22T10:56:00.000+01:00</published><updated>2012-04-22T11:04:24.266+01:00</updated><title type='text'>Java Tutorial 15: Control Flow Statements- Branching Statements</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;



The
break Statement&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
break statement has two forms: labelled and unlabelled. You saw the unlabelled
form in the previous discussion of the switch statement. You can also use an unlabelled
break to terminate a for, while, or do-while loop, as shown in the following Break
Demo program:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
BreakDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int[] arrayOfInts = &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 32, 87, 3, 589,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12, 1076, 2000,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8, 622, 127 };&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int searchfor = 12;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; boolean foundIt = false;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (i = 0; i &amp;lt; arrayOfInts.length;
i++) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (arrayOfInts[i] == searchfor) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foundIt = true;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (foundIt) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Found
&quot; + searchfor + &quot; at index &quot; + i);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(searchfor +
&quot; not in the array&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
This
program searches for the number 12 in an array. The break statement, shown in
boldface, terminates the for loop when that value is found. Control flow then
transfers to the statement after the for loop. This program&#39;s output is:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Found
12 at index 4&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
An unlabelled
break statement terminates the inner most switch, for, while, or do-while
statement, but a labelled break terminates an outer statement. The following
program, BreakWithLabelDemo, is similar to the previous program, but uses
nested for loops to search for a value in a two-dimensional array. When the
value is found, a labelled break terminates the outer for loop (labelled
&quot;search&quot;):&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
BreakWithLabelDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int[][] arrayOfInts = { &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{ 32, 87, 3, 589 },&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 12, 1076, 2000, 8 },&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 622, 127, 77, 955 }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int searchfor = 12;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int j = 0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; boolean foundIt = false;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; search:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (i = 0; i &amp;lt; arrayOfInts.length;
i++) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (j = 0; j &amp;lt;
arrayOfInts[i].length;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; j++) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (arrayOfInts[i][j] ==
searchfor) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foundIt = true;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break search;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (foundIt) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Found
&quot; + searchfor +&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot; at &quot;
+ i + &quot;, &quot; + j);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(searchfor +&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot; not in
the array&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
This
is the output of the program.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Found
12 at 1, 0&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
break statement terminates the labelled statement; it does not transfer the
flow of control to the label. Control flow is transferred to the statement
immediately following the labelled (terminated) statement.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;



The
continue Statement&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
continue statement skips the current iteration of a for, while , or do-while
loop. The unlabelled form skips to the end of the innermost loop&#39;s body and
evaluates the boolean expression that controls the loop. The following program,
ContinueDemo , steps through a String, counting the occurrences of the letter
&quot;p&quot;. If the current character is not a p, the continue statement
skips the rest of the loop and proceeds to the next character. If it is a
&quot;p&quot;, the program increments the letter count.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ContinueDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String searchMe &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = &quot;peter piper picked a &quot;
+&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;peck of pickled
peppers&quot;;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int numPs = 0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; searchMe.length(); i++) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // interested only in p&#39;s&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (searchMe.charAt(i) != &#39;p&#39;)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // process p&#39;s&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numPs++;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Found &quot; +&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numPs + &quot; p&#39;s in the
string.&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Here
is the output of this program:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Found
9 p&#39;s in the string.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
To
see this effect more clearly, try removing the continue statement and
recompiling. When you run the program again, the count will be wrong, saying
that it found 35 p&#39;s instead of 9.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
A labelled
continue statement skips the current iteration of an outer loop marked with the
given label. The following example program, ContinueWithLabelDemo, uses nested
loops to search for a substring within another string. Two nested loops are
required: one to iterate over the substring and one to iterate over the string
being searched. The following program, ContinueWithLabelDemo, uses the labelled
form of continue to skip an iteration in the outer loop.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ContinueWithLabelDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String searchMe &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = &quot;Look for a substring in
me&quot;;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String substring = &quot;sub&quot;;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; boolean foundIt = false;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int max = searchMe.length() - &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; substring.length();&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; test:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt;= max; i++) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int n = substring.length();&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int j = i;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int k = 0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while (n-- != 0) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (searchMe.charAt(j++)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; != substring.charAt(k++)) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue test;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foundIt = true;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break test;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(foundIt ?&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;Found it&quot; : &quot;Didn&#39;t find
it&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Here
is the output from this program.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Found
it&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
return Statement&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
last of the branching statements is the return statement. The return statement
exits from the current method, and control flow returns to where the method was
invoked. The return statement has two forms: one that returns a value, and one
that doesn&#39;t. To return a value, simply put the value (or an expression that
calculates the value) after the return keyword.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
return
++count;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
data type of the returned value must match the type of the method&#39;s declared
return value. When a method is declared void, use the form of return that
doesn&#39;t return a value.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
return;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
Classes and Objects lesson will cover everything you need to know about writing
methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot;&gt;
&lt;h1&gt;

Summary of Control Flow Statements&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot;&gt;
The &lt;code&gt;if-then&lt;/code&gt; statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code &lt;i&gt;only if&lt;/i&gt; a particular test evaluates to &lt;code&gt;true&lt;/code&gt;. The &lt;code&gt;if-then-else&lt;/code&gt; statement provides a secondary path of execution when an &quot;if&quot; clause evaluates to &lt;code&gt;false&lt;/code&gt;. Unlike &lt;code&gt;if-then&lt;/code&gt; and &lt;code&gt;if-then-else&lt;/code&gt;, the &lt;code&gt;switch&lt;/code&gt; statement allows for any number of possible execution paths. The &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;do-while&lt;/code&gt; statements continually execute a block of statements while a particular condition is &lt;code&gt;true&lt;/code&gt;. The difference between &lt;code&gt;do-while&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; is that &lt;code&gt;do-while&lt;/code&gt; evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the &lt;code&gt;do&lt;/code&gt; block are always executed at least once. The &lt;code&gt;for&lt;/code&gt;
 statement provides a compact way to iterate over a range of values. It 
has two forms, one of which was designed for looping through collections
 and arrays.&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/6647760764282444815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-14-control-flow.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6647760764282444815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6647760764282444815'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-14-control-flow.html' title='Java Tutorial 15: Control Flow Statements- Branching Statements'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-8377673057138683359</id><published>2012-04-19T17:02:00.003+01:00</published><updated>2012-04-19T17:03:41.976+01:00</updated><title type='text'>Java Tutorial 14: Control Flow Statements- The for Statement</title><content type='html'>&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The for statement provides a compact way to iterate over a
range of values. Programmers often refer to it as the &quot;for loop&quot;
because of the way in which it repeatedly loops until a particular condition is
satisfied. The general form of the for statement can be expressed as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
for (initialization; termination;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; increment) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; statement(s)&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
When using this version of the for statement, keep in mind
that:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The initialization expression initializes the loop; it&#39;s
executed once, as the loop begins.&amp;nbsp;When the termination expression evaluates to false, the loop
terminates.&amp;nbsp;The increment expression is invoked after each iteration
through the loop; it is perfectly acceptable for this expression to increment
or decrement a value.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The following program, ForDemo, uses the general form of the
for statement to print the numbers 1 through 10 to standard output:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class ForDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args){&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=1;
i&amp;lt;11; i++){&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Count is: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + i);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The output of this program is:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 4&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 5&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 6&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 7&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 8&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 9&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 10&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Notice how the code declares a variable within the
initialization expression. The scope of this variable extends from its
declaration to the end of the block governed by the for statement, so it can be
used in the termination and increment expressions as well. &lt;b&gt;If the variable that controls a for statement is not needed outside of
the loop, it&#39;s best to declare the variable in the initialization expression.&lt;/b&gt;
The &lt;b&gt;&lt;u&gt;names i, j, and k&lt;/u&gt;&lt;/b&gt; are
often used to control for loops; declaring them within the initialization
expression limits their life span and reduces errors.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The three expressions of the for loop are optional; an
infinite loop can be created as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
// infinite loop&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
for ( ; ; ) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// your code goes here&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The for statement also has another form designed for
iteration through Collections and arrays This form is sometimes referred to as
the enhanced for statement, and can be used to make your loops more compact and
easy to read. To demonstrate, consider the following array, which holds the
numbers 1 through 10:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
int[] numbers = {1,2,3,4,5,6,7,8,9,10};&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The following program, EnhancedForDemo, uses the enhanced
for to loop through the array:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class EnhancedForDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args){&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int[] numbers
= &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{1,2,3,4,5,6,7,8,9,10};&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int item
: numbers) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Count is: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + item);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In this example, the variable item holds the current value
from the numbers array. The output from this program is the same as before:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 4&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 5&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 6&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 7&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 8&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 9&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Count is: 10&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
We recommend using this form of the for statement instead of
the general form whenever possible.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/8377673057138683359/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/for-statement.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8377673057138683359'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8377673057138683359'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/for-statement.html' title='Java Tutorial 14: Control Flow Statements- The for Statement'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-8641411063169401105</id><published>2012-04-19T16:53:00.002+01:00</published><updated>2012-04-19T17:03:17.031+01:00</updated><title type='text'>Java Tutorial 13: Control Flow Statements- The while and do-while Statements</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The while statement continually executes a block of
statements while a particular condition is true. Its syntax can be expressed
as:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
while (expression) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statement(s)&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The while statement evaluates expression, which must return
a boolean value. If the expression evaluates to true, the while statement
executes the statement(s) in the while block. The while statement continues
testing the expression and executing its block until the expression evaluates
to false. Using the while statement to print the values from 1 through 10 can
be accomplished as in the following WhileDemo program:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class WhileDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args){&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int count = 1;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while (count
&amp;lt; 11) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Count is: &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;+ count);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count++;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can implement an infinite loop using the while statement
as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
while (true){&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // your code goes
here&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The Java programming language also provides a do-while
statement, which can be expressed as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
do {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statement(s)&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
} while (expression);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The difference between do-while and while is that do-while
evaluates its expression at the bottom of the loop instead of the top.
Therefore, the statements within the do block are always executed at least
once, as shown in the following DoWhileDemo program:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class DoWhileDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args){&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int count = 1;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println(&quot;Count is:
&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + count);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count++;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } while (count
&amp;lt; 11);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/8641411063169401105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-12-control-flow_19.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8641411063169401105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/8641411063169401105'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-12-control-flow_19.html' title='Java Tutorial 13: Control Flow Statements- The while and do-while Statements'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-2785743840152789558</id><published>2012-04-19T14:21:00.003+01:00</published><updated>2013-05-14T15:20:08.386+01:00</updated><title type='text'>How to use these tutorials</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Just to answer some peoples email and give you a few tips on
how to use these tutorials. Most of these tutorials that I have posted are from
ORACLE website. By the end of the day, these are the most official material. &lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
I have deleted a few bits here and there to make it easier
to follow. These things either are not very important for beginners or not
important at all. You can always come back to them when you are “fluent” with
the language. I have also changed some of the codes to make it easier, again,
to make it easy to follow.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
To use these tutorial, I recommend to follow the below steps&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;text-indent: -18pt;&quot;&gt;Read the tutorials, including the code, and try
to understand everything.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: 7pt; text-indent: -18pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;text-indent: -18pt;&quot;&gt;Read the tutorials a few more times and try to
memorize all the important concept, definition, etc.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;text-indent: -18pt;&quot;&gt;TYPR (do not copy) the code into Eclipse and run
the application, change some variables and see what will happen. Don’t worry;
you cannot break your computer.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;text-indent: -18pt;&quot;&gt;Now close the tutorial, see if you can remember
the code.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: 7pt; text-indent: -18pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;text-indent: -18pt;&quot;&gt;In later tutorial, if you forget something, don’t
just ignore them. Go back and read them again.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Hope this helps!&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/2785743840152789558/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/how-to-use-these-tutorials.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/2785743840152789558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/2785743840152789558'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/how-to-use-these-tutorials.html' title='How to use these tutorials'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-4495118989942337280</id><published>2012-04-18T16:28:00.001+01:00</published><updated>2012-04-19T17:07:51.866+01:00</updated><title type='text'>Java Tutorial 12: Control Flow Statements- The switch Statement</title><content type='html'>&lt;br /&gt;
&lt;h1 style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;

&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Unlike
if-then and if-then-else statements, the switch statement can have a number of
possible execution paths. A switch works with the &lt;b&gt;byte, short, char, and int&lt;/b&gt; primitive data types. It also works with
enumerated types (discussed later), the &lt;b&gt;String
class&lt;/b&gt;, and a few special classes that wrap certain primitive types:
Character, Byte, Short, and Integer (discussed in Numbers and Strings).&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following code example, SwitchDemo, declares an int named month whose value
represents a month. The code displays the name of the month, based on the value
of month, using the switch statement.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
public
class SwitchDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int month = 8;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String monthString;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; switch (month) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 1:&amp;nbsp; monthString = &quot;January&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 2:&amp;nbsp; monthString = &quot;February&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 3:&amp;nbsp; monthString = &quot;March&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 4:&amp;nbsp; monthString = &quot;April&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 5:&amp;nbsp; monthString = &quot;May&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 6:&amp;nbsp; monthString = &quot;June&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 7:&amp;nbsp; monthString = &quot;July&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 8:&amp;nbsp; monthString = &quot;August&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 9:&amp;nbsp; monthString = &quot;September&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 10: monthString = &quot;October&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 11: monthString =
&quot;November&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 12: monthString =
&quot;December&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default: monthString = &quot;Invalid
month&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(monthString);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
In
this case, August is printed to standard output.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
body of a switch statement is known as a switch block. A statement in the
switch block can be labelled with one or more case or default labels. The
switch statement evaluates its expression, then executes all statements that
follow the matching case label.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
You
could also display the name of the month with if-then-else statements:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
int
month = 8;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
if
(month == 1) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;January&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}
else if (month == 2) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;February&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
...&amp;nbsp; // and so on&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Deciding
whether to use if-then-else statements or a switch statement is based on
readability and the expression that the statement is testing. An if-then-else
statement can &lt;u&gt;test expressions based on ranges of values or conditions,&lt;/u&gt;
whereas a switch statement tests expressions based only &lt;u&gt;on a single integer,
enumerated value, or String object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Another
point of interest is the break statement. Each break statement terminates the
enclosing switch statement. Control flow continues with the first statement
following the switch block. The break statements are necessary because without
them, statements in switch blocks fall through: All statements after the
matching case label are executed in sequence, regardless of the expression of
subsequent case labels, until a break statement is encountered. The program witchDemoFallThrough
shows statements in a switch block that fall through. The program displays the
month corresponding to the integer month and the months that follow in the
year: (This demo include ArrayList and for loop that we will come back to that
later.)&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
public
class SwitchDemoFallThrough {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String args[]) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; java.util.ArrayList&amp;lt;String&amp;gt;
futureMonths =&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new
java.util.ArrayList&amp;lt;String&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int month = 8;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; switch (month) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 1:&amp;nbsp; futureMonths.add(&quot;January&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 2:&amp;nbsp; futureMonths.add(&quot;February&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 3:&amp;nbsp; futureMonths.add(&quot;March&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 4:&amp;nbsp; futureMonths.add(&quot;April&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 5:&amp;nbsp; futureMonths.add(&quot;May&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 6:&amp;nbsp; futureMonths.add(&quot;June&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 7:&amp;nbsp; futureMonths.add(&quot;July&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 8:&amp;nbsp; futureMonths.add(&quot;August&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 9:&amp;nbsp; futureMonths.add(&quot;September&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 10:
futureMonths.add(&quot;October&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 11:
futureMonths.add(&quot;November&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 12:
futureMonths.add(&quot;December&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default: break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (futureMonths.isEmpty()) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Invalid
month number&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
// the code below utilize the enhanced for&amp;nbsp;statement, which will be&amp;nbsp;&lt;a href=&quot;http://mmp669-android.blogspot.co.uk/2012/04/for-statement.html&quot; target=&quot;_blank&quot;&gt;discussed in tutorial 14.&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (String monthName :
futureMonths) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(monthName);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
This
is the output from the code:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
August&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
September&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
October&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
November&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
December&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Technically,
the final break is not required because flow falls out of the switch statement.
Using a break is recommended so that modifying the code is easier and less
error prone. The default section handles all values that are not explicitly
handled by one of the case sections.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following code example, SwitchDemo2, shows how a statement can have multiple
case labels. The code example calculates the number of days in a particular
month:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
SwitchDemo2 {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int month = 2;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int year = 2000;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int numDays = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; switch (month) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 1: case 3: case 5:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 7: case 8: case 10:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 12:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numDays = 31;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 4: case 6:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 9: case 11:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numDays = 30;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case 2:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (year % 4 == 0) &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {numDays = 29;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {numDays = 28;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Invalid month.&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Number of Days
= &quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + numDays);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
This
is the output from the code:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Number
of Days = 29&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;

Using
Strings in switch Statements&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
In
Java SE 7 and later, you can use a String object in the switch statement&#39;s
expression. The following code example, StringSwitchDemo, displays the number
of the month based on the value of the String named month:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
public
class StringSwitchDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static int getMonthNumber(String
month) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int monthNumber = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (month == null) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return monthNumber;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; switch (month.toLowerCase()) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;january&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 1;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;february&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 2;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;march&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 3;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;april&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 4;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;may&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 5;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;june&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 6;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;july&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 7;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;august&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 8;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;september&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 9;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;october&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 10;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;november&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 11;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case &quot;december&quot;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 12;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default: &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monthNumber = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return monthNumber;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String month = &quot;August&quot;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int returnedMonthNumber =&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
StringSwitchDemo.getMonthNumber(month);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (returnedMonthNumber == 0) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Invalid
month&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(returnedMonthNumber);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
output from this code is 8.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
String in the switch expression is compared with the expressions associated
with each case label as if the String.equals method were being used. In order
for the StringSwitchDemo example to accept any month regardless of case, month
is converted to lowercase (with the toLowerCase method), and all the strings
associated with the case labels are in lowercase.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Note:
This example checks if the expression in the switch statement is null. Ensure
that the expression in any switch statement is not null to prevent a
NullPointerException from being thrown.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/4495118989942337280/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-12-control-flow.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4495118989942337280'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4495118989942337280'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-12-control-flow.html' title='Java Tutorial 12: Control Flow Statements- The switch Statement'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-3935123487612947020</id><published>2012-04-18T15:01:00.006+01:00</published><updated>2012-04-18T15:02:08.646+01:00</updated><title type='text'>Java Tutorial 11: Control Flow Statements-The if-then and if-then-else Statements</title><content type='html'>&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The statements inside your source files are generally
executed from top to bottom, in the order that they appear. Control flow
statements, however, break up the flow of execution by employing decision
making, looping, and branching, enabling your program to conditionally execute
particular blocks of code. This section describes the decision-making
statements (if-then, if-then-else, switch), the looping statements (for, while,
do-while), and the branching statements (break, continue, return) supported by
the Java programming language.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;

The if-then Statement&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The if-then statement is the most basic of all the control
flow statements. It tells your program to execute a certain section of code
only if a particular test evaluates to true. For example, the Bicycle class
could allow the brakes to decrease the bicycle&#39;s speed only if the bicycle is
already in motion. One possible implementation of the applyBrakes method could
be as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
void applyBrakes() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // the
&quot;if&quot; clause: bicycle must be moving&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (isMoving){ &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // the
&quot;then&quot; clause: decrease current speed&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
currentSpeed--;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
If this test evaluates to false (meaning that the bicycle is
not in motion), control jumps to the end of the if-then statement.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;

The if-then-else Statement&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The if-then-else statement provides a secondary path of
execution when an &quot;if&quot; clause evaluates to false. You could use an
if-then-else statement in the applyBrakes method to take some action if the
brakes are applied when the bicycle is not in motion. In this case, the action
is to simply print an error message stating that the bicycle has already
stopped.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
void applyBrakes() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (isMoving) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
currentSpeed--;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.err.println(&quot;The bicycle has &quot; + &quot;already
stopped!&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The following program, IfElseDemo, assigns a grade based on
the value of a test score: an A for a score of 90% or above, a B for a score of
80% or above, and so on.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class IfElseDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void
main(String[] args) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int testscore
= 76;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char grade;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (testscore
&amp;gt;= 90) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grade =
&#39;A&#39;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else if
(testscore &amp;gt;= 80) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grade =
&#39;B&#39;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else if
(testscore &amp;gt;= 70) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grade =
&#39;C&#39;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else if
(testscore &amp;gt;= 60) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;grade = &#39;D&#39;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grade =
&#39;F&#39;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Grade = &quot; + grade);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The output from the program is:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Grade = C&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You may have noticed that the value of testscore can satisfy
more than one expression in the compound statement: 76 &amp;gt;= 70 and 76 &amp;gt;=
60. However, once a condition is satisfied, the appropriate statements are
executed (grade = &#39;C&#39;;) and the remaining conditions are not evaluated.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/3935123487612947020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/control-flow-statements-if-then-and-if.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/3935123487612947020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/3935123487612947020'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/control-flow-statements-if-then-and-if.html' title='Java Tutorial 11: Control Flow Statements-The if-then and if-then-else Statements'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-6715244372059447721</id><published>2012-04-18T14:52:00.005+01:00</published><updated>2012-04-18T14:53:24.521+01:00</updated><title type='text'>Java Tutorial 10: Expressions, Statements, and Blocks</title><content type='html'>&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Operators are used in building expressions, which compute
values; expressions are the core components of statements; statements may be
grouped into blocks.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;

Expressions&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
An expression is a construct made up of variables,
operators, and method invocations, which are constructed according to the
syntax of the language, which evaluates to a single value. You&#39;ve already seen
examples of expressions, illustrated in bold below:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 10pt;&quot;&gt;int &lt;b&gt;cadence = 0&lt;/b&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;anArray[0] = 100&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 10pt;&quot;&gt;System.out.println(&lt;b&gt;&quot;Element
1 at index 0: &quot; + anArray[0]&lt;/b&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 10pt;&quot;&gt;int &lt;b&gt;result = 1 + 2&lt;/b&gt;;
// result is now 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 10pt;&quot;&gt;if (&lt;b&gt;value1 == value2&lt;/b&gt;)
&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-indent: 24pt;&quot;&gt;
&lt;span style=&quot;font-size: 10pt;&quot;&gt;System.out.println(&lt;b&gt;&quot;value1
== value2&quot;&lt;/b&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-indent: 24pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-indent: 24pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The data type of the value returned by an expression depends
on the elements used in the expression. The expression cadence = 0 returns an
int because the assignment operator returns a value of the same data type as
its left-hand operand; in this case, cadence is an int. As you can see from the
other expressions, an expression can return other types of values as well, such
as boolean or String.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The Java programming language allows you to construct
compound expressions from various smaller expressions as long as the data type
required by one part of the expression matches the data type of the other.
Here&#39;s an example of a compound expression:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
1 * 2 * 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In this particular example, the order in which the
expression is evaluated is unimportant because the result of multiplication is
independent of order; the outcome is always the same, no matter in which order
you apply the multiplications. However, this is not true of all expressions.
For example, the following expression gives different results, depending on
whether you perform the addition or the division operation first:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
x + y / 100&amp;nbsp;&amp;nbsp;&amp;nbsp; //
ambiguous&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
You can specify exactly how an expression will be evaluated
using balanced parenthesis: ( and ). For example, to make the previous
expression unambiguous, you could write the following:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
(x + y) / 100&amp;nbsp; //
unambiguous, recommended&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
If you don&#39;t explicitly indicate the order for the
operations to be performed, the order is determined by the precedence assigned
to the operators in use within the expression. Operators that have a higher
precedence get evaluated first. For example, the division operator has a higher
precedence than does the addition operator. Therefore, the following two
statements are equivalent:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
x + y / 100 &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
x + (y / 100) // unambiguous, recommended&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
When writing compound expressions, be explicit and indicate
with parentheses which operators should be evaluated first. This practice makes
code easier to read and to maintain.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;

Statements&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Statements are roughly equivalent to sentences in natural
languages. A statement forms a complete unit of execution. The following types
of expressions can be made into a statement by terminating the expression with
a semicolon (;).&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;i&gt;Assignment expressions&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;i&gt;Any use of ++ or --&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;i&gt;Method invocations&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;i&gt;Object creation
expressions&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Such statements are called &lt;u&gt;expression statements&lt;/u&gt;.
Here are some examples of expression statements.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
// assignment statement&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
aValue = 8933.234;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
// increment statement&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
aValue++;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
// method invocation statement&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
System.out.println(&quot;Hello World!&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
// object creation statement&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Bicycle myBike = &lt;b&gt;new&lt;/b&gt;
Bicycle();&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In addition to expression statements, there are two other
kinds of statements: &lt;u&gt;declaration statements&lt;/u&gt; and &lt;u&gt;control flow
statements&lt;/u&gt;. A declaration statement declares a variable. You&#39;ve seen many
examples of declaration statements already:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
// declaration statement&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
double aValue = 8933.234;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Finally, control flow statements regulate the order in which
statements get executed. You&#39;ll learn about control flow statements in the next
section.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;

Blocks&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
A block is a group of zero or more statements between
balanced braces and can be used anywhere a single statement is allowed. The
following example, BlockDemo, illustrates the use of blocks:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
class BlockDemo {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static
void main(String[] args) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; boolean
condition = true;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if
(condition) { // begin block 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Condition is true.&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } // end
block one&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else { //
begin block 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
System.out.println(&quot;Condition is false.&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } // end
block 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
}&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/6715244372059447721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/expressions-statements-and-blocks.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6715244372059447721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6715244372059447721'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/expressions-statements-and-blocks.html' title='Java Tutorial 10: Expressions, Statements, and Blocks'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-165141000882812279</id><published>2012-04-16T20:45:00.001+01:00</published><updated>2012-04-18T12:00:15.715+01:00</updated><title type='text'>Java Tutorial 9: Operators-Equality, Relational, and Conditional Operators</title><content type='html'>&lt;br /&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;




The
Equality and Relational Operators&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
equality and relational operators determine if one operand is greater than,
less than, equal to, or not equal to another operand. The majority of these
operators will probably look familiar to you as well. Keep in mind that you
must use &quot;==&quot;, not &quot;=&quot;, when testing if two primitive
values are equal.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
==&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; equal to&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
!=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; not equal to&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; greater than&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;gt;=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; greater than or equal to&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;lt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; less than&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;lt;=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; less than or equal to&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, ComparisonDemo, tests the comparison operators:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ComparisonDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args){&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value1 = 1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value2 = 2;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(value1 == value2)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1 ==
value2&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(value1 != value2)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1 !=
value2&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(value1 &amp;gt; value2)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1
&amp;gt; value2&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(value1 &amp;lt; value2)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1
&amp;lt; value2&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(value1 &amp;lt;= value2)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1
&amp;lt;= value2&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Output:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
value1
!= value2&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
value1
&amp;lt;&amp;nbsp; value2&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
value1
&amp;lt;= value2&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;




The
Conditional Operators&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
&amp;amp;&amp;amp; and || operators perform Conditional-AND and Conditional-OR
operations on two boolean expressions. These operators exhibit
&quot;short-circuiting&quot; behavior, which means that the second operand is
evaluated only if needed.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;amp;&amp;amp;
Conditional-AND&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
||
Conditional-OR&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, ConditionalDemo1, tests these operators:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ConditionalDemo1 {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args){&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value1 = 1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value2 = 2;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if((value1 == 1) &amp;amp;&amp;amp; (value2 ==
2))&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1 is
1 AND value2 is 2&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if((value1 == 1) || (value2 == 1))&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;value1 is
1 OR value2 is 1&quot;);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;




Another
conditional operator is ?:,&amp;nbsp;&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
which can be thought of as shorthand for an&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt; if-then-else statement &lt;/b&gt;(discussed in
the Control Flow Statements section of this lesson). This operator is also
known as the ternary operator because it uses three operands. In the following
example, this operator should be read as: &quot;If someCondition is true,
assign the value of value1 to result. Otherwise, assign the value of value2 to
result.&quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, ConditionalDemo2, tests the ?: operator:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ConditionalDemo2 {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args){&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value1 = 1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value2 = 2;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int result;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; boolean someCondition = true;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = someCondition ? value1 :
value2;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(result);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Because
someCondition is true, this program prints &quot;1&quot; to the screen. Use the
?: operator instead of an if-then-else statement if it makes your code more
readable; for example, when the expressions are compact and without
side-effects (such as assignments).&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;




The
Type Comparison Operator instanceof&lt;/h3&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
instanceof operator compares an object to a specified type. You can use it to
test if an object is an instance of a class, an instance of a subclass, or an
instance of a class that implements a particular interface.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, InstanceofDemo, defines a parent class (named Parent), a
simple interface (named MyInterface), and a child class (named Child) that
inherits from the parent and implements the interface. (Don&#39;t worry if you can&#39;t understand everything, we&#39;ll comeback to this later).&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
InstanceofDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Parent obj1 = new Parent();&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Parent obj2 = new Child();&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;obj1
instanceof Parent: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + (obj1 instanceof Parent));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;obj1
instanceof Child: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + (obj1 instanceof Child));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;obj1
instanceof MyInterface: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + (obj1 instanceof MyInterface));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;obj2
instanceof Parent: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + (obj2 instanceof Parent));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;obj2
instanceof Child: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + (obj2 instanceof Child));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;obj2
instanceof MyInterface: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + (obj2 instanceof MyInterface));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
Parent {}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
Child extends Parent implements MyInterface {}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
interface
MyInterface {}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Output:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
obj1
instanceof Parent: true&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
obj1
instanceof Child: false&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
obj1
instanceof MyInterface: false&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
obj2
instanceof Parent: true&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
obj2
instanceof Child: true&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
obj2
instanceof MyInterface: true&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
When
using the instanceof operator, keep in mind that null is not an instance of
anything.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;

&lt;/h2&gt;
&lt;h2&gt;
Summary of Operators&lt;/h2&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The following quick reference summarizes the operators supported
by the Java programming language.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;

Simple Assignment Operator&lt;/h4&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Simple
assignment operator&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;

Arithmetic Operators&lt;/h4&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Additive
operator (also used&amp;nbsp;for String
concatenation)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
-&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Subtraction
operator&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Multiplication
operator&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Division
operator&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Remainder
operator&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;

Unary Operators&lt;/h4&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Unary plus
operator; indicates&amp;nbsp;positive value
(numbers are&amp;nbsp;positive
without this, however)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
-&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Unary minus
operator; negates&amp;nbsp;an expression&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
++&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Increment
operator; increments&amp;nbsp;a value by 1&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
--&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Decrement
operator; decrements&amp;nbsp;a value by 1&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
!&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Logical
complement operator;&amp;nbsp;inverts the
value of a boolean&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;

Equality and Relational Operators&lt;/h4&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
==&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Equal to&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
!=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Not equal to&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Greater
than&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;gt;=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Greater
than or equal to&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;lt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Less than&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;lt;=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Less than
or equal to&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;

Conditional Operators&lt;/h4&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;amp;&amp;amp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Conditional-AND&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
||&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Conditional-OR&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
?:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ternary
(shorthand for&amp;nbsp;if-then-else
statement)&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;h4&gt;
Type Comparison Operator&lt;/h4&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
instanceof&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Compares an object to&amp;nbsp;a
specified type&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/165141000882812279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-8-operators-equality.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/165141000882812279'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/165141000882812279'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-8-operators-equality.html' title='Java Tutorial 9: Operators-Equality, Relational, and Conditional Operators'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-1940690707487296140</id><published>2012-04-16T20:25:00.004+01:00</published><updated>2012-04-16T20:26:26.838+01:00</updated><title type='text'>Java Tutorial 8: Operators-Assignment, Arithmetic, and Unary Operators</title><content type='html'>&lt;br /&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;

The
Simple Assignment Operator&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
One
of the most common operators that you&#39;ll encounter is the simple assignment
operator &quot;=&quot;. You saw this operator in the Bicycle class; it assigns
the value on its right to the operand on its left:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;int cadence = 0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;int speed = 0;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;int gear = 1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
This
operator can also be used on objects to assign object references, as discussed
in Creating Objects.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;

The
Arithmetic Operators&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
Java programming language provides operators that perform addition,
subtraction, multiplication, and division. &quot;%&quot; divides one operand by
another and returns the remainder as its result.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; additive operator (also used for &amp;nbsp;String concatenation)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
-&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; subtraction operator&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; multiplication operator&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; division operator&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; remainder operator, &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
You
can combine the arithmetic operators with the simple assignment operator to
create compound assignments. For example, &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;x+=1;
and x=x+1; both increment the value of x by 1&lt;/b&gt;. The + operator can also be
used for concatenating (joining) two strings together, as shown in the
following ConcatDemo program:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;

The
Unary Operators&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
unary operators require only one operand; they perform various operations such
as incrementing/decrementing a value by one, negating an expression, or
inverting the value of a boolean.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Unary plus operator; indicates positive
value (numbers are positive without this)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
-&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Unary minus operator; negates&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
++&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Increment operator; increments &amp;nbsp;&amp;nbsp;a value by 1&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
--&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Decrement operator; decrements a value by
1&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
!&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Logical complement operator; &amp;nbsp;inverts the value of a boolean&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
increment/decrement operators can be applied before (prefix) or after (postfix)
the operand. The code result++; and ++result; will both end in result being
incremented by one. T&lt;u&gt;he only difference is that the prefix version (++result)
evaluates to the incremented value, whereas the postfix version (result++)
evaluates to the original value. &lt;/u&gt;If you are just performing a simple
increment/decrement, it doesn&#39;t really matter which version you choose. But if
you use this operator in part of a larger expression, the one that you choose
may make a significant difference.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, PrePostDemo, illustrates the prefix/postfix unary increment
operator:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
PrePostDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args){&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i = 3;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i++;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints 4&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(i);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ++i;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints 5&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(i);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints 6&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(++i);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints 6&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(i++);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints 7&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(i);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/1940690707487296140/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/assignment-arithmetic-and-unary.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/1940690707487296140'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/1940690707487296140'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/assignment-arithmetic-and-unary.html' title='Java Tutorial 8: Operators-Assignment, Arithmetic, and Unary Operators'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-6070265723122802334</id><published>2012-04-16T17:13:00.001+01:00</published><updated>2012-04-16T17:13:53.269+01:00</updated><title type='text'>Java Tutorial 8: Operators-Introduction</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Operators are special symbols that perform specific operations on one, two, or
three operands, and then return a result.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The operators in the following table are listed
according to precedence order. The closer to the top of the table an operator
appears, the higher its precedence. Operators with higher precedence are
evaluated before operators with relatively lower precedence. Operators on the
same line have equal precedence. When operators of equal precedence appear in
the same expression, a rule must govern which is evaluated first. All binary
operators except for the assignment operators are evaluated from left to right;
assignment operators are evaluated right to left.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw8EADgDttzheHzkCA9JX9xfcb6BxTdgo5Kfvm73jeblCn1Be52NUeVG82hhBcZdiH8redgrte1sOFqdNyigUoRYbvJ83uYsahA4dIl6JjRRxYZlyFb5tHfFYZE8SbEOk0ime37tqV5aK2/s1600/Capture.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw8EADgDttzheHzkCA9JX9xfcb6BxTdgo5Kfvm73jeblCn1Be52NUeVG82hhBcZdiH8redgrte1sOFqdNyigUoRYbvJ83uYsahA4dIl6JjRRxYZlyFb5tHfFYZE8SbEOk0ime37tqV5aK2/s320/Capture.PNG&quot; width=&quot;305&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In general-purpose programming, certain operators tend to
appear more frequently than others; for example, the assignment operator
&quot;=&quot; is far more common than the unsigned right shift operator
&quot;&amp;gt;&amp;gt;&amp;gt;&quot;. With that in mind, the following discussion focuses
first on the operators that you&#39;re most likely to use on a regular basis, and
ends focusing on those that are less common. Each discussion is accompanied by
sample code that you can compile and run. Studying its output will help
reinforce what you&#39;ve just learned.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/6070265723122802334/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/operators-are-special-symbols-that.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6070265723122802334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/6070265723122802334'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/operators-are-special-symbols-that.html' title='Java Tutorial 8: Operators-Introduction'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw8EADgDttzheHzkCA9JX9xfcb6BxTdgo5Kfvm73jeblCn1Be52NUeVG82hhBcZdiH8redgrte1sOFqdNyigUoRYbvJ83uYsahA4dIl6JjRRxYZlyFb5tHfFYZE8SbEOk0ime37tqV5aK2/s72-c/Capture.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-4305023069076457459</id><published>2012-04-15T17:25:00.000+01:00</published><updated>2012-04-16T17:08:16.510+01:00</updated><title type='text'>Java Tutorial 7: Variables-array</title><content type='html'>&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
An
array is a container object that holds a fixed number of values of a single
type. The length of an array is established when the array is created. After
creation, its length is fixed. &lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj035FSK-tMF2wMNli5ba6vp53GT1vBkHCWGtt3-Mw4chy-gnTzu5Jr5mZS498ejvdFXEzjDwE41O8taduVtU8z1RLUwYtOy9ReNmV4pMvBbnLIJOounJZB2U4ydhyc6WZPlO0XKP5_IWS8/s1600/objects-tenElementArray.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;116&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj035FSK-tMF2wMNli5ba6vp53GT1vBkHCWGtt3-Mw4chy-gnTzu5Jr5mZS498ejvdFXEzjDwE41O8taduVtU8z1RLUwYtOy9ReNmV4pMvBbnLIJOounJZB2U4ydhyc6WZPlO0XKP5_IWS8/s320/objects-tenElementArray.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Each
item in an array is called an element, and each element is accessed by its
numerical index. As shown in the above illustration, numbering begins with 0.
The 9th element, for example, would therefore be accessed at index 8.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, ArrayDemo, creates an array of integers, puts some values in
it, and prints each value to standard output.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ArrayDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // declares an array of integers&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int[] anArray;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // allocates memory for 10 integers&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray = new int[10];&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initialize first element&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[0] = 100;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initialize second element&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[1] = 200;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // etc.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[2] = 300;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[3] = 400;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[4] = 500;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[5] = 600;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[6] = 700;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[7] = 800;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[8] = 900;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; anArray[9] = 1000;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 0: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[0]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 1: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[1]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 2: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;+ anArray[2]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 3: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[3]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 4: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[4]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 5: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;+ anArray[5]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 6: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[6]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 7: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[7]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 8: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[8]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Element at
index 9: &quot;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + anArray[9]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
} &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
output from this program is:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 0: 100&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 1: 200&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 2: 300&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 3: 400&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 4: 500&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 5: 600&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 6: 700&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 7: 800&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 8: 900&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Element
at index 9: 1000&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
In a
real-world programming situation, you&#39;d probably use one of the supported &lt;i&gt;looping
constructs&lt;/i&gt; to iterate through each element of the array, rather than write
each line individually as shown above. However, this example clearly
illustrates the array syntax.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;


Declaring
a Variable to Refer to an Array&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
above program declares anArray with the following line of code:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
//
declares an array of integers&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
int[]
anArray;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Like
declarations for variables of other types, an array declaration has two
components: the array&#39;s type and the array&#39;s name. An array&#39;s type is written
as type[], where type is the data type of the contained elements; the square
brackets are special symbols indicating that this variable holds an array. The
size of the array is not part of its type (which is why the brackets are
empty). An array&#39;s name can be anything you want, provided that it follows the
rules and conventions as previously discussed in the naming section. As with
variables of other types, the declaration does not actually create an array —
it simply tells the compiler that this variable will hold an array of the
specified type.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Similarly,
you can declare arrays of other types:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
double[]
anArrayOfDoubles;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
boolean[]
anArrayOfBooleans;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
String[]
anArrayOfStrings;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;


Creating,
Initializing, and Accessing an Array&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
One
way to create an array is with the new operator. The next statement in the
ArrayDemo program allocates an array with enough memory for ten integer
elements and assigns the array to the anArray variable.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
//
create an array of integers&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
anArray
= new int[10];&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
If
this statement were missing, the compiler would print an error like the following,
and compilation would fail:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
ArrayDemo.java:4:
Variable anArray may not have been initialized.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
next few lines assign values to each element of the array:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
anArray[0]
= 100; // initialize first element&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
anArray[1]
= 200; // initialize second element&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
anArray[2]
= 300; // etc.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Each
array element is accessed by its numerical index:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
System.out.println(&quot;Element
1 at index 0: &quot; + anArray[0]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
System.out.println(&quot;Element
2 at index 1: &quot; + anArray[1]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
System.out.println(&quot;Element
3 at index 2: &quot; + anArray[2]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Alternatively,
you can use the shortcut syntax to create and initialize an array:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
int[]
anArray = { &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 100, 200, 300,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 400, 500, 600, &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 700, 800, 900, 1000&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
};&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Here
the length of the array is determined by the number of values provided between
{ and }.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
You
can also declare an array of arrays (also known as a multidimensional array) by
using two or more sets of square brackets, such as String[][] names. Each element,
therefore, must be accessed by a corresponding number of index values.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
In
the Java programming language, a multidimensional array is simply an array
whose components are themselves arrays. A consequence of this is that the rows
are allowed to vary in length, as shown in the following
MultiDimArrayDemoprogram:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
MultiDimArrayDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String[][] names = {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&quot;Mr. &quot;, &quot;Mrs.
&quot;, &quot;Ms. &quot;},&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&quot;Smith&quot;,
&quot;Jones&quot;}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Mr. Smith&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(names[0][0] +
names[1][0]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Ms. Jones&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(names[0][2] +
names[1][1]);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
output from this program is:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Mr.
Smith&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Ms.
Jones&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
Finally,
you can use the built-in length property to determine the size of any array.
The code&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;System.out.println(anArray.length);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
will
print the array&#39;s size to standard output.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;


Copying
Arrays&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
System class has an arraycopy method that you can use to efficiently copy data
from one array into another:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
public
static void arraycopy(Object src, int srcPos,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Object dest, int
destPos, int length)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
two Object arguments specify the array to copy from and the array to copy to.
The three int arguments specify the starting position in the source array, the
starting position in the destination array, and the number of array elements to
copy.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
following program, ArrayCopyDemo, declares an array of char elements, spelling
the word &quot;decaffeinated&quot;. It uses arraycopy to copy a subsequence of
array components into a second array:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
class
ArrayCopyDemo {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char[] copyFrom = { &#39;d&#39;, &#39;e&#39;, &#39;c&#39;, &#39;a&#39;,
&#39;f&#39;, &#39;f&#39;, &#39;e&#39;,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &#39;i&#39;, &#39;n&#39;, &#39;a&#39;, &#39;t&#39;, &#39;e&#39;, &#39;d&#39; };&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char[] copyTo = new char[7];&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.arraycopy(copyFrom, 2, copyTo,
0, 7);&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(new String(copyTo));&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
The
output from this program is:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify; text-justify: inter-ideograph;&quot;&gt;
caffein&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;br /&gt;Summary of Variables&lt;/h2&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The Java programming language uses both &quot;fields&quot;
and &quot;variables&quot; as part of its terminology. Instance variables
(non-static fields) are unique to each instance of a class. Class variables
(static fields) are fields declared with the static modifier; there is exactly
one copy of a class variable, regardless of how many times the class has been
instantiated. Local variables store temporary state inside a method. Parameters
are variables that provide extra information to a method; both local variables
and parameters are always classified as &quot;variables&quot; (not
&quot;fields&quot;). When naming your fields or variables, there are rules and
conventions that you &amp;nbsp;must follow.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The eight primitive data types are:&amp;nbsp;&lt;b&gt;int,&lt;/b&gt;&amp;nbsp;&lt;b&gt;double, boolean,&amp;nbsp;&lt;/b&gt;byte, short, long,
float,&amp;nbsp;and char. The java.lang.String class represents
character strings. A literal is the source code representation of a fixed value. An
array is a container object that holds a fixed number of values of a single
type. The length of an array is established when the array is created. After
creation, its length is fixed.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Sorce: ORACLE &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/4305023069076457459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/v-behaviorurldefaultvmlo.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4305023069076457459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/4305023069076457459'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/v-behaviorurldefaultvmlo.html' title='Java Tutorial 7: Variables-array'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj035FSK-tMF2wMNli5ba6vp53GT1vBkHCWGtt3-Mw4chy-gnTzu5Jr5mZS498ejvdFXEzjDwE41O8taduVtU8z1RLUwYtOy9ReNmV4pMvBbnLIJOounJZB2U4ydhyc6WZPlO0XKP5_IWS8/s72-c/objects-tenElementArray.gif" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2118932319280195164.post-7873710219646398695</id><published>2012-04-15T17:08:00.003+01:00</published><updated>2012-04-15T17:26:17.748+01:00</updated><title type='text'>Java Tutorial 6: Variables-Primitive Data Types</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;


Primitive Data Types&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The Java programming language is statically-typed, which
means that all variables must first be declared before they can be used. This
involves stating the variable&#39;s type and name, as you&#39;ve already seen:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
int gear = 1;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Doing so tells your program that a field named
&quot;gear&quot; exists, holds numerical data, and has an initial value of
&quot;1&quot;. A variable&#39;s data type determines the values it may contain,
plus the operations that may be performed on it. In addition to int, the Java programming
language supports seven other primitive data types. A primitive type is
predefined by the language and is named by a reserved keyword. Primitive values
do not share state with other primitive values. &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The eight primitive data types supported by the Java
programming language are listed below, the first 3 is the most common ones and
they are probably the ones that you’ll ever need. &lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;u&gt;int&lt;/u&gt;: The int
data type is a 32-bit signed two&#39;s complement integer. It has a minimum value
of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For
integral values, this data type is generally &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;the default choice &lt;/b&gt;unless there is a reason (like the above) to
choose something else. This data type will most likely be large enough for the
numbers your program will use, but if you need a wider range of values, use
long instead.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;u&gt;double&lt;/u&gt;: The double data type is a double-precision
64-bit IEEE 754 floating point. Its range of values is beyond the scope of this
discussion, but is specified in section 4.2.3 of the Java Language
Specification. &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;For decimal values, this
data type is generally the default choice&lt;/b&gt;. As mentioned above, this data
type should never be used for precise values, such as currency.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;u&gt;boolean&lt;/u&gt;: The boolean data type has only two possible
values: true and false. Use this data type for simple flags that track
true/false conditions. This data type represents one bit of information, but
its &quot;size&quot; isn&#39;t something that&#39;s precisely defined.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
byte: The byte data type is an 8-bit signed two&#39;s complement
integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
The byte data type can be useful for saving memory in large arrays, where the
memory savings actually matters. They can also be used in place of int where
their limits help to clarify your code; the fact that a variable&#39;s range is
limited can serve as a form of documentation.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
short: The short data type is a 16-bit signed two&#39;s
complement integer. It has a minimum value of -32,768 and a maximum value of
32,767 (inclusive). As with byte, the same guidelines apply: you can use a
short to save memory in large arrays, in situations where the memory savings
actually matters.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
long: The long data type is a 64-bit signed two&#39;s complement
integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum
value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you
need a range of values wider than those provided by int.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
float: The float data type is a single-precision 32-bit IEEE
754 floating point. Its range of values is beyond the scope of this discussion,
but is specified in section 4.2.3 of the Java Language Specification. As with
the recommendations for byte and short, use a float (instead of double) if you
need to save memory in large arrays of floating point numbers. This data type
should never be used for precise values, such as currency. For that, you will
need to use the java.math.BigDecimal class instead. Numbers and Strings covers
BigDecimal and other useful classes provided by the Java platform.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
char: The char data type is a single 16-bit Unicode
character. It has a minimum value of &#39;\u0000&#39; (or 0) and a maximum value of
&#39;\uffff&#39; (or 65,535 inclusive).&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
In addition to the eight primitive data types listed above,
the Java programming language also provides special support for &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;character strings via the java.lang.String
class&lt;/b&gt;. Enclosing your character string within double quotes will
automatically create a new String object; for example, String s = &quot;this is
a string&quot;;. String objects are immutable, which means that once created,
their values cannot be changed. The String class is not technically a primitive
data type, but considering the special support given to it by the language,
you&#39;ll probably tend to think of it as such. You&#39;ll learn more about the String
class in Simple Data Objects&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;


Default Values&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
It&#39;s not always necessary to assign a value when a field is
declared. Fields that are declared but not initialized will be set to a
reasonable default by the compiler. Generally speaking, this default will be
zero or null, depending on the data type. Relying on such default values, however,
is generally considered bad programming style, so I am not going to go into
them.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;


Literals&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
You may have noticed that the “new” keyword isn&#39;t used when
initializing a variable of a primitive type. Primitive types are special data
types built into the language; they are not objects created from a class. A
literal is the source code representation of a fixed value; literals are
represented directly in your code without requiring computation. As shown
below, it&#39;s possible to assign a literal to a variable of a primitive type:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
boolean result = true;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
int i = 100000;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;


Character and String Literals&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
Literals of types char and String may contain any Unicode
(UTF-16) characters. If your editor and file system allow it, you can use such
characters directly in your code. If not, you can use a &quot;Unicode
escape&quot; such as &#39;\u0108&#39; (capital C with circumflex), or &quot;S\u00ED
Se\u00F1or&quot; (Sí Señor in Spanish). &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Always
use &#39;single quotes&#39; for char literals and &quot;double quotes&quot; for String
literals. &lt;/b&gt;Unicode escape sequences may be used elsewhere in a program (such
as in field names, for example), not just in char or String literals.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
The Java programming language also supports a few special
escape sequences for char and String literals: &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;\b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage
return), \&quot; (double quote), \&#39; (single quote), and \\ (backslash).&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;


Using Underscore Characters in Numeric Literals&lt;/h2&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
In Java SE 7 and later, any number of underscore characters
(_) can appear anywhere between digits in a numerical literal. This feature
enables you, for example. to separate groups of digits in numeric literals,
which can improve the readability of your code.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
For instance, if your code contains numbers with many
digits, you can use an underscore character to separate digits in groups of
three, similar to how you would use a punctuation mark like a comma, or a
space, as a separator.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
long creditCardNumber = 1234_5678_9012_3456L;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
long socialSecurityNumber = 999_99_9999L;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
You can place underscores only between digits; you cannot
place underscores in the following places:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; At the beginning
or end of a number&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Adjacent to a
decimal point in a floating point literal&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Prior to an F or L
suffix&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; In positions where
a string of digits is expected&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmp669-android.blogspot.com/feeds/7873710219646398695/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-5-variables-primitive.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/7873710219646398695'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2118932319280195164/posts/default/7873710219646398695'/><link rel='alternate' type='text/html' href='http://mmp669-android.blogspot.com/2012/04/java-tutorial-5-variables-primitive.html' title='Java Tutorial 6: Variables-Primitive Data Types'/><author><name>mmp669</name><uri>http://www.blogger.com/profile/01075449556424134505</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>