CSCI 161 - Lecture 2 - String class, Output, Static Methods, Pitfalls, Procedural Decomposition

Instructor's Class Notes

  1. String class
    1. Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects.
    2. The most direct way to create a string is to write
          String greeting = "Hello world!";
                          

      Note: Strings are literals within double quotes and cannot span multiple lines.
    3. Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object.
    4. You can concatenate strings with the + operator, or the .concat() method:
          String string1 = "Hey";
          String string2 = " now";
          string1.concat(string2);
          System.out.println(string1);  // Maybe not what is expected
          string1 = string1.concat(string2);
          System.out.println(string1);  // Interesting!
                          
    5. Strings are immutable, meaning they don't actually change. The only way to change them is to re-assign them which causes a new object and new String in memory.
    6. Compare and contrast that to StringBuffer which is mutable and allows for in-place updating of a string
          StringBuffer b = new StringBuffer("Hello");
          System.out.println(b);
          b.append(", world!");
          System.out.println(b);
                          
    7. One of the really cool methods of the String class is format():
          String fs;
          fs = String.format("The value of the float variable is " +
                             "%f, while the value of the integer " +
                             "variable is %d, and the string " +
                             "is %s", floatVar, intVar, stringVar);
          System.out.println(fs);
                          
    8. toUpperCase() - a method that uppercases the string.
    9. indexOf() - a method that determines if a specific character is contained withing the string.
    10. A great overview of the String class along with its various methods is here: https://www.tutorialspoint.com/java/java_strings.htm
  2. Output
    1. Output is done through the System class.
    2. The System class contains several useful class fields and methods. It cannot be instantiated.
      Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
    3. System.out has println and print methods for output to standard output (aka "the console").
    4. println - Used to output a single line of text.
    5. print - Used to output data to the current line without including a new line. Repeated calls will place output on the same line.
    6. Escape sequences:
  3. Identifiers and Keywords
    1. identifier - a name given to an entity in a program such as a class, methor or variable.
    2. Can start with a letter followed by any letter or number.
    3. Can also include an underscore _ or dollar sign $.
    4. Cannot include plus sign +, minu sign/hyphen -, space or start with a number.
    5. Conventions:
    6. Java Keywords (aka Reserved Words):
          abstract    default	    goto           package	    this
          assert      do          if             private	    throw
          boolean     double	    implements     protected        throws
          break       else	    import         public	    transient
          byte        enum	    instanceof     return	    true
          case        extends	    int	           short	    try
          catch       false	    interface	   static	    void
          char        final	    long           strictfp	    volatile
          class       finally	    native         super	    while
          const       float	    new	           switch	
          continue    for         null           synchronized	
                          
  4. Static Methods
    1. A static method of a class can be called without an instance of the class existing.
    2. For example:
          System.out.println("Hello");   // println is a static method
          System.out.println("Length of the word car is: " + "car".length()); 
            // length method of String class not static (is an instance method)
                              
    3. For our first few labs you will deal exclusively with static methods.
    4. More on static methods vs instance methods in future lectures.
  5. Pitfalls - Program Errors
    1. Three general types of programming errors:
    2. Syntax Errors - a list of common syntax errors are:
  6. Procedural Decomposition
    1. Procedural Decomposition - A separation into discernible parts, each of which is simpler than the whole.
    2. Cake example:
    3. However, making the batter may require sub-tasks:
    4. In Java, the easiest way to separate a problem into parts is through the use of methods.
    5. Iterative Enhancement - The process of producing a program in stages, adding new functionality at each stage. A key feature is that you test as you go.

* Some materials from www.tutorialspoint.com.