Home Previous Next

CSC110AB::Lecture Note::Week 07
Assignments | Handouts | Resources | Email Thurman {Twitter::@compufoo Facebook::CSzero}
GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates



Code: Xor.java | SquareNumbers.java | Elevator.java | Alfred.java | PowersOfX.java | LabeledBreak.java
FutureDay2019.java | Stringer.java | MoreStringExamples.java

The break Statement

The break statement is a jump control statement that is used inside of repetition control statements and the switch selection control statement.

The break statement causes an immediate exit from the innermost enclosing switch or loop statement.

The break statement allows a loop to be exited from some point other than at the top or bottom of the loop.

Structured programming purist are not overly fond of the break statement.

   int foo = 100;
   while (true) {
      if (0 == foo)
         break;  // jump to statement A
   /*A*/ foo = 100;

You must be careful when you use the break.

   while (...) {
      if (...) {
         if (...) 
            break;  // the programmer wanted to break from the
                    // body of the enclosing  if  statement,
                    // but instead the  break  will cause the
                    // flow control of the program to jump to
                    // the 1st executable statement after the
                    // body of the  while  statement, which in
                    // this example is  statement2

Java does support labeled break statements. Labeled break statements are useful for breaking from nested loops.

   FOO: while (true) {
      while (true) {
         break FOO;  // jumps to statement /*A*/

   /*A*/ some_statement;   // statement label is FOO

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

The continue Statement

The continue statement is a jump control statement that is used inside of repetition control statements.

The continue statement causes the next iteration of the enclosing loop statement to begin. In the while and do, this means that the test part is executed immediately; in the for control passes to the step-EXPR.

The continue statement is often used when the part of the loop that follows is complicated, so that reversing a test and indenting another level would nest the program too deeply.

Structured programming purist are not overly fond of the continue statement.

   while (boolean_control_EXPR) {
      if (...)
         continue;   // jump to the boolean_control_EXPR
                     // the remaining part of the loop body
                     // section  A  is skipped
      /*A*/ ...

Java does have labeled continue statements.

   FOO: while (boolean_control_EXPR) {
      while (another_boolean_control_EXPR) {
         if (...)
            continue FOO;  // jump to the boolean_control_EXPR for
                           // the while statement labeled FOO
                           // sections B and C are skipped
         /*B*/ ...
      /*C*/ ...

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

The goto Statement

Java does have a goto statement. goto is not a keyword of the language, but it is a reserved word. The word is reserved because it may be added to Java in the future.

The goto statement is frequently found in code that is generated by other programs.

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

class String

A string is a sequence of zero or more characters. String objects, once created, cannot be modified (i.e. they are immutable).

Frequently used String methods are:

It is important to remember that when you are testing two String objects for equality, then you must use the equals() method.

   String s1 = new String("hello");
   String s2 = new String();
   String s3 = new String(s1);
   String s4 = "bye";
   s4 = s1;

   if (s1 == s2)   // evaluates to false in this example
      // tests if  s1  and  s2  reference the same String object

   if (s1.equals(s2))   // evaluates to false in this example
      // tests if the content of the object referenced by  s1
      // equals the content of the object referenced by  s2

   s1.equals(s3)   // evaluates to true in this example
   s1 == s3        // evaluates to false in this example
   s1.equals(s4)   // evaluates to true in this example
   s4.equals(s1)   // evaluates to true in this example
   s1 == s4        // evaluates to true in this example

Docs.Oracle.com::String API

Additional String Related Notes

Here are some more notes concerning some of the String instance methods.

The substring() method is used to get a sub-string of an instance of a String object.
   public String substring(begin_i, end_i);

   "Hello".substring(0, 2)

      returns a String object with  He  as its content.
startsWith() and endsWith()
The startsWith() and endsWith() return true or false if the this object starts or ends with a particular String value.
   public boolean startsWith(String prefix);
   public boolean endsWith(String suffix);

   "WebAccount.java".startsWith("Web")   returns  true
   "WebAccount.java".startsWith("web")   returns  false
   "WebAccount.java".endsWith("Web")     returns  false
   "WebAccount.java".endsWith(".java")   returns  true
The compareTo() method is used to test String objects for equality. You use it over the equals() method when you need to determine if one string is less than or greater than another string (e.g. you are sorting strings). If compareTo() returns a value less than 0, then this string is lexicographically less than the string received as a parameter; otherwise, a 0 return from compareTo() implies the strings are equal; else, a positive return value indicates that this string is lexicographically greater than the parameter string.
   public int compareTo(String that);

   "hello".compareTo("bye")     returns positive value
   "hello".compareTo("jello")   returns negative value
   "hello".compareTo("hello")   returns zero
[review Stringer.java]

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

Introduction to Methods

Lowest level ideas become expressions (EXPRs), expressions are grouped into statements, and statements are grouped together into blocks and methods.

A method is a set of statements that have been collected together and given a name.

Methods break large computing tasks into smaller ones, and enable people to build on what others have done instead of starting over from scratch. Methods support reuse.

Methods hide details of operation from parts of the program that don't need to know about them. Methods support information hiding.

Methods usually consist of zero or more statements, and a collection of local data. Methods support encapsulation.

Programs generally consist of many small methods rather than a few big ones. Methods support modularity.

Repetitive expressions should be grouped into a method. Methods eliminate duplicate code.

Additional benefits derived from using methods:

Methods that perform generic tasks can be added to a class library. Library methods can be used in multiple programs. Library methods are static methods.

Java doesn't allow for global methods; i.e. every method must be defined in a class.

Static methods are class methods; whereas non-static methods are instance methods.

Instance methods typically contain code that understands and manipulate an object's state.

Static methods can be invoked in one of two ways:

Instance methods can only be invoked using an object variable.

Every instance method has access to an object variable named this. The this object variable points-to (or is a handle-to, or is a reference-to) the recipient object.

A recipient object is the object for which a method has been called.

Statically defined methods do not have access to the this object variable.

Calling an instance method is equivalent to sending a message to an object. The values of the arguments, if any, make up the content of the message. If no arguments are used, then you have an empty message.

Method arguments are passed by-value. All argument EXPRs are evaluated prior to the method being called.

[review Underline.java]

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

Introduction to Classes

A class is a blueprint or template that describes an object.

Class Syntax
   classDeclaration {
Syntax of a class declaration:
   [ modifiers ] class ClassName [ extends SuperClassName ]
                                 [ implements InterfaceNames ]

   Some example modifiers are:  public, abstract, final 
   If no modifier is specified, then it defaults to "package."

The class body contains static data members, instance data members, static methods and instance methods.

By convention, class names begin with an uppercase letter.

About Constructor Methods

A constructor is a special method that is used to help guarantee that objects are initialized to a "valid" or "sane" state when they are created (instantiated).

[review UnderlineOO.java]

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

Home Previous Next