Java provides a String Class - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Java provides a String Class

Description:

Java provides a String Class The String class can be used to create String objects. String str1; //declare an object variable str1 = new String ( mary had a little ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 21
Provided by: facul350
Category:

less

Transcript and Presenter's Notes

Title: Java provides a String Class


1
Java provides a String Class
  • The String class can be used to create String
    objects.
  • String str1 //declare an object
    variable
  • str1 new String (mary had a little lamb)
  • Strings are created SO OFTEN that Java provides a
    short cut for creating them (without using
    constructor) ?
  • str1 mary had a little lamb
  • Object declaration and construction (creation)
    can be done in one statement
  • String str1 new String(mary had a little
    lamb)

2
Using a String Object
  • String objects have many behaviors. One of these
    behaviors is called length .
  • The length behavior of a String object
    provides the number of characters in the String.
  • So
  • str1.length() provides the number of
    characters
  • in the String
    object str1
  • And
  • System.out.println(the length is
    str1.length() )
  • Prints the length is 22

3
Design a Greeter Class
Greeter

message
sayGreeting( ) Greeter (themessage)
String
The message attribute of the Greeter class will
have a String for a value ..
4
Using a Greeter Class
  • If we had a Greeter class, we could create use
    Greeter objects like this
  • public class Application
  • public static void main (String args )
  • Greeter gre1
  • Greeter gre2 // declare two Greeter
    object variables
  • gre1 new Greeter(Hello, how are you!)
  • gre2 new Greeter(HOWDY!!)
  • System.out.println( gre1.sayGreeting()
    )
  • System.out.println( gre2.sayGreeting() )

5
Code the Greeter Class
  • public class Greeter //name of
    the class we

  • are defining
  • private String message //each object of
    type

  • Greeter will need a variable
  • to
    store its message
  • public String sayGreeting( )
  • // this behavior is available to all
    classes (public)
  • // this behavior gives back a String
  • // this behavior requires no
    information to be provided
  • return message //statement that
    executes when this behavior
  • // is
    executed
  • We still need to add the constructor
    method.

6
the constructor method ..
  • public Greeter(String themessage)
  • //accessible to other classes
  • //requires a String,well call it themessage
  • message themessage
  • //store the String which was provided
  • // in the variable message

7
The entire Greeter class ..
  • public class Greeter
  • private String message
  • public Greeter(String themessage)
  • message themessage
  • public String sayGreeting() return
    message

8
Updated UML
Greeter

- message String
Greeter (String) String sayGreeting( )
the constructor needs to be provided with a
String so that the message can be given an
initial value the sayGreeting method returns a
String
9
Instance Fields (Variables)
  • public class Greeter
  • ...private String message // called
    instance field

  • // or instance variable
  • access specifier (such as private)
  • type of variable (such as String)
  • name of variable (such as message)

10
Method Definition
  • access specifier (such as public or private)
  • public
  • return type (such as String or void)
  • public String
  • method name (such as sayHello)
  • public String sayHello
  • list of parameters (empty for sayHello)
  • public String sayHello ( )
  • method body in

11
Parameters
  • public Greeter(String themessage)
  • message themessage
  • themessage is the name we are giving to the
    String value which will be provided
  • we will store the value which is provided in
    our variable message when an Greeter object is
    created
  • themessage is called a parameter

12
Syntax Method Implementation
  • public class ClassName
  • ...
  • accessSpecifier returnType methodName(parameter
    Type

  • parameterName,...) method body
  • ...

13
Syntax The return Statement
  • return expression
  • or
  • return
  • Example
  • return message
  • Purpose
  • To specify the value that a method returns, and
  • exit the method immediately. The return value
  • becomes the value of the method call
  • expression.

14
The Constructor
  • The constructor is a method used to create and
    object
  • The constructor has the same name as the class
    (and no return type)
  • The constructor is a method which provides
    initial values to the instance variables.
  • public Greeter(String themessage)
  • message themessage

15
  • The Greeter class cannot execute (it is not an
    application class, there is no main method)
  • The Greeter class is will be used for creating
    objects, and then using them.
  • we already saw a class which USES the Greeter
    class.

16
this class can be used to test if Greeter works
correctly!!
  • public class Application
  • public static void main (String args )
  • Greeter gre1
  • Greeter gre2 // declare two Greeter
    object variables
  • gre1 new Greeter(Hello, how are you!)
  • gre2 new Greeter(HOWDY!!)
  • System.out.println( gre1.sayGreeting()
    )
  • System.out.println( gre2.sayGreeting() )

17
Another Class for the Greeter Class
  • public class GreeterTest
  • public static void main(String args))
    Greeter worldGreeter new Greeter(World)
  • Greeter daveGreeter new
    Greeter(Dave)
  • System.out.println(worldGreeter.sayHello()
    )
  • System.out.println(daveGreeter.sayHello())

18
Each object of type Greeter is an instance of
class Greeter.
Each instance gets its own variable message,
so message is called an instance field or
variable.
19
Accessing Instance Fields
  • The instance field message is PRIVATE.
  • That means it can only be used by statements
    inside the Greeter class definition.
  • The sayGreeting method of the Greeter class can
    access the private instance field
  • public String sayGreeting() return
    message

20
  • The application class cannot access message
  • public class GreeterTest public static void
    main(String args)
  • System.out.println(daveGreeter.message)
  • // ERROR!!!
  • Encapsulation Hiding data and providing access
    through methods
Write a Comment
User Comments (0)
About PowerShow.com