Strings and String Concatenation
A String is an object that represents a sequence of characters.
Strings, Class Methods,
and Applications
Java strings can be written as characters delimited by double quotes:
“cat”
“Computer Science”
“CS111 rocks!”
A string constant creates a new object without the use of new.
In Java, strings are concatenated with + :
Fri. Feb 15, 2013
“CS” + “111” + “ ” + “rocks!” ⇒ “CS111 rocks!”
Strings can be displayed in the console using System.out.println():
CS111 Computer Programming
System.out.println(“cat”);
System.out.println(“CS” + “111” + “ ” + “rocks!” );
Department of Computer Science
Wellesley College
System.out.printlb(“one\ntwo\nthree”); // \n is newline char
Applications
Strings of Digits vs. Numbers
2
System.out.println is used for debugging
Strings of digits are different from numbers;
addition is different from concatenation:
class WallHuggerDebugger extends TickBuggle {
public void followWall() {
System.out.println("I'm at " + getPosition());
if (isOverBagel()) {
System.out.println("Picking up bagel.");
pickUpBagel();
} else if (isFacingWall()) {
System.out.println("Turning left.");
left();
} else {
System.out.println("Going forward.");
forward();
}
}
111 + 234 ⇒ 345
“111” + “234” ⇒ “111234”
“111” + 234 ⇒ “111234” // coerces 234 to “234”
111 + “234” ⇒ “111234” // coerces 111 to “111”
When Java expects a String, it will often coerce a non-String value to a String:
Dr. Java Interaction Pane
> System.out.println(3); // coerces 3 to “3”
3
> System.out.println(4 == 5); // coerces false to “false”
false
> Buggle bob = new Buggle();
> System.out.println(bob);
[position = (1, 1); heading = EAST; color = java.awt.Color[r=255,g=0,b=0];
brushDown? true]
> System.out.println(bob.getPosition());
Location(x=1,y=1)
Applications
3
public void tick() {
followWall();
}
}
Applications
4
What’s a Class Method?
Classes can have both Instance and Class Methods
Example: The Color class
A class method is a method that, when invoked, does not have a receiver
instance. But it may have arguments and/or a return value.
Some instance methods
In other programming languages, class methods are called functions or
procedures.
public int getBlue()
In a contract method header, class methods indicated by the keyword static.
E.g., the java.lang.Math class has no instances, but is a repository including
class methods for many standard math functions. E.g.:
public String toString()
public Color darker()
Some class methods
public static int abs(int a);
public static Color decode (String nm)
public static int max(int a, int b);
public static Color getHSBColor(float h, float s, float b)
public static int floor (double a);
Class methods are invoked via className.methodName( argExp1,…,argExpn ):
Dr. Java
Interaction Pane
> Math.abs(-17)
17
> Math.max(3,8)
8
> Math.max(8,3)
8
> Math.PI
3.141592653589793
> Math.floor(4.625)
4.0
> Math.floor(Math.PI)
3.0
Applications
Dr. Java
Interaction Pane
5
Defining a Class Method
Dr. Java Interaction Pane
public class Temperature {
> Temperature.CToF(100.0)
212.0
// Test Celsius to Fahrenheit conversion
public static void testCToF (double tempC) {
System.out.println(tempC + " degrees Celsius is "
+ CToF(tempC)
+ " degrees Fahrenheit");
}
6
JEM for Class Method Invocations
Java class file
// Convert Celsius to Fahrenheit
public static double CToF (double tempC) {
return (tempC * 9.0 / 5.0) + 32.0;
}
> Color.magenta.toString()
"java.awt.Color[r=255,g=0,b=255]"
> Color.magenta.darker().getBlue()
178
> Color.decode("#FF0080").toString()
"java.awt.Color[r=255,g=0,b=128]"
> Color.decode("#FF0080").darker().getBlue()
89
Applications
Temperature.testCToF(37.0)
Execution
Land
tempC
37.0
System.out.println(tempC + " degrees Celsius is "
+ CToF(tempC)
+ " degrees Fahrenheit");
> Temperature.CToF(0)
32.0
>Temperature.testCToF(37.0);
37.0 degrees Celsius is 98.6
degrees Fahrenheit
Within the Temperature class,
Temperature.CToF(…) can be
abbreviated CToF(…).
Notes:
Execution frames for class method invocations have no this variable
because they have no receiver object.
}
In general, execution frames for class methods can refer to objects
in ObjectLand, but this particular example does not.
Applications
7
Applications
8
Evaluate first tempC
Evaluate first concatenation
Temperature.testCToF(37.0)
Execution
Land
tempC
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println(tempC + " degrees Celsius is "
+ CToF(tempC)
+ " degrees Fahrenheit");
9
Applications
10
Applications
12
What do we do now?
Evaluate second tempC
Temperature.testCToF(37.0)
tempC
37.0
System.out.println(37.0 + " degrees Celsius is "
+ CToF(tempC)
+ " degrees Fahrenheit");
Applications
Execution
Land
tempC
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println(”37.0 degrees Celsius is "
+ CToF(tempC)
+ " degrees Fahrenheit");
tempC
37.0
System.out.println(”37.0 degrees Celsius is "
+ CToF(37.0 )
+ " degrees Fahrenheit");
Applications
11
Create Execution Frame for CToF Invocation
Create Execution Frame for CToF Invocation
Temperature.testCToF(37.0)
Execution
Land
tempC
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println(”37.0 degrees Celsius is "
+ CToF(37.0 )
+ " degrees Fahrenheit");
37.0
System.out.println(”37.0 degrees Celsius is "
+ CToF(37.0 )
+ " degrees Fahrenheit");
Temperature.CToF(37.0)
tempC
tempC
Temperature.CToF(37.0)
tempC
37.0
return (tempC * 9.0 / 5.0) + 32.0;
37.0
return (tempC * 9.0 / 5.0) + 32.0;
No this variable in
frame for class
method invocation!
Applications
13
Evaluate tempC
tempC
Applications
16
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println(”37.0 degrees Celsius is "
+ CToF(37.0 )
+ " degrees Fahrenheit");
tempC
37.0
System.out.println(”37.0 degrees Celsius is "
+ CToF(37.0 )
+ " degrees Fahrenheit");
Temperature.CToF(37.0)
tempC
14
Evaluate rest of expression
Temperature.testCToF(37.0)
Execution
Land
Applications
Temperature.CToF(37.0)
tempC
37.0
return (tempC * 9.0 / 5.0) + 32.0;
37.0
return (37.0 * 9.0 / 5.0) + 32.0;
Applications
15
Return result
Replace invocation by result
Temperature.testCToF(37.0)
Execution
Land
tempC
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println(“37.0 degrees Celsius is "
+ CToF(37.0 )
+ " degrees Fahrenheit");
tempC
37.0
System.out.println(”37.0 degrees Celsius is "
+ 98.6
+ " degrees Fahrenheit");
Temperature.CToF(37.0)
tempC
37.0
return 98.6 ;
Applications
17
Evaluate second concatenation
tempC
18
Applications
20
Evaluate third concatenation
Temperature.testCToF(37.0)
Execution
Land
Applications
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println(”37.0 degrees Celsius is "
+ 98.6
+ " degrees Fahrenheit");
tempC
37.0
System.out.println(”37.0 degrees Celsius is 98.6"
+ " degrees Fahrenheit");
Applications
19
Invoke System.out.println
The string is displayed in the console
Temperature.testCToF(37.0)
Execution
Land
tempC
Temperature.testCToF(37.0)
Execution
Land
37.0
System.out.println("37.0 degrees Celsius is 98.6 degrees Fahrenheit");
37.0
System.out.println("37.0 degrees Celsius is 98.6 degrees Fahrenheit");
Java
Console
Applications
tempC
37.0 degrees Celsius is 98.6 degrees Fahrenheit
21
Another Class with Class Methods
Applications
22
What’s an Application?
public class StringOps {
// Return a string consisting of two copies of a given string
public static String repeat (String s) {
return s + s;
}
Can invoke Java instance and class methods (1) from
within Java classes or (2) from special interactive
environments like the Dr. Java Interactions pane.
// Pluralize a string
public static String plural (String str) {
if (str.equals("mouse")) {
return "mice";
} else if (str.equals("goose")) {
return "geese";
} else { // Can have many other special cases before general case
return str + "s";
}
}
There are two ways to package Java code into “programs”
that can be run independently:
application is a Java program that can run on its
own. You can run an application directly from a
command line or from the DrJava Interactions pane.
1. An
2. An
applet is a Java program that runs in a browser
(or a similar graphical container).
}
What is the value of StringOps.repeat(StringOps.plural(StringOps.repeat("ma")))?
Applications
23
Applications
24
Consider the following song
A Simple Java Application
I know an old lady who swallowed a fly
I don't know why she swallowed the fly
I guess she'll die
Can turn any class into an application by defining a special void class method
named main that is the entry point to the program:
I know an old lady who swallowed a spider
That wriggled and jiggled and tickled inside her
She swallowed the spider to catch the fly
But I don't know why she swallowed the fly
I guess she'll die
public class Temperature {
public static void main (String [] args) {
testCToF(100.0);
testCToF(37.0);
}
I know an old lady who swallowed a bird
How absurd to swallow a bird
She swallowed the bird to catch the spider
That wriggled and jiggled and tickled inside her
She swallowed the spider to catch the fly
But I don't know why she swallowed the fly
I guess she'll die
Ignore main’s argument for now.
It will be explained later when we
learn about arrays.
… CToF and testCToF the same as before …
I know an old lady who swallowed a cat
Imagine that she swallowed a cat
She swallowed the cat to catch the bird
She swallowed the bird to catch the spider
That wriggled and jiggled and tickled inside her
She swallowed the spider to catch the fly
But I don't know why she swallowed the fly
I guess she'll die
}
Run a Java application from command line or Dr. Java Interaction pane via
java className
Dr. Java
Interaction Pane
(or press Run button in Dr. Java)
I know an old lady who swallowed a dog
What a hog to swallow a dog
She swallowed the dog to catch the cat
She swallowed the cat to catch the bird
She swallowed the bird to catch the spider
That wriggled and jiggled and tickled inside her
She swallowed the spider to catch the fly
But I don't know why she swallowed the fly
I guess she'll die
> java Temperature
100.0 degrees Celsius is 212.0 degrees Fahrenheit
37.0 degrees Celsius is 98.6 degrees Fahrenheit
Applications
25
I know an old lady who swallowed a goat
Just opened her throat and swallowed a goat
She swallowed the goat to catch the dog
She swallowed the dog to catch the cat
She swallowed the cat to catch the bird
She swallowed the bird to catch the spider
That wriggled and jiggled and tickled inside her
She swallowed the spider to catch the fly
But I don't know why she swallowed the fly
I guess she'll die
I know an old lady who swallowed a cow
I don't know how but she swallowed a cow
She swallowed the cow to catch the goat
She swallowed the goat to catch the dog
She swallowed the dog to catch the cat
She swallowed the cat to catch the bird
She swallowed the bird to catch the spider
That wriggled and jiggled and tickled inside her
She swallowed the spider to catch the fly
But I don't know why she swallowed the fly
I guess she'll die
I know an old lady who swallowed a horse
She's dead of course
Write an application that displays these lyrics.
Use class methods to capture the patterns
in the lyrics!
Applications
26
© Copyright 2026 Paperzz