Activity 2.2.1.
Run this code to see the output below it. How would you change it to print the ! on the same line as Hi there keeping all 3 print statements?
println
and print
System.out.println(value)
prints the the textual representation of value followed by a new line (println
is an abbreviation for “print line”.)
System.out.print(value)
prints the textual representation of value without advancing to the next line
42
is a literal expression representing the int
value 42. When we print a value with print
or println
, Java converts the value to its textual representation and then emits that to the screen. So we could write System.out.print(42)
to print the text 42
. Another trivial kind of expression is a variable, whose value is whatever value was last assigned to it.
String
. Why are they called "strings"? Who knows? Java didn’t make it up; computer scientists have been using “string” to refer to sequences of items since at least the 1940s. Think “string of pearls” or “string of ponies” but in this case a “string of characters”, where “character” is what computer scientists call letters, numbers, punctuation, spaces, etc.
String
. As another data type, like int
and double
, we can declare String
variables. We’ll learn a lot more about the String
data type in Section 7.1 Manipulating strings but for now we just need to know we can write string literals by enclosing the text we want in quotation marks: ""
.
"Hi there!"
is a string literal whose value is the text: Hi there!
. The quotation marks are not part of the value; they are the syntax for writing the string literal. Thus the code System.out.println("Hi there!");
prints the text Hi there!
on the screen, without the quotation marks, and then advances to the next line.
String
feature for printing output is the string concatenation operator, written with +
, the same as arithmetic addition. When we write a +
expression where one of the operands is a String
, it produces a new String
by smooshing together the String
with the String
representation of the other value. For instance if name
and age
were variables containing someone’s name and age we could write:
System.out.println("Name: " + name + "; Age: " + age);
Name: Sally Sue; Age: 17
""
. The latter is just a String
whose text happens to be the same as the name of the variable while the former will produce the String
representation of the variable’s value. A common mistake is to write something like System.out.println("x")
when we meant to write System.out.println(x)
On the other hand, a really good way to write that would be: System.out.println("x: " + x)
which would emit the value of x
labeled with the name of the variable.
String
literal. Without spaces, we will get output like HiJose
instead of Hi Jose
.
"
character? Since "
has a special meaning within Java’s string literals, it might seem there’d be no way to include it within a string. However, Java provides a way to write string literals that contain double quotes using a mechanism called escaping. To “escape” the normal syntax of a string literal, where a quotation mark would indicate the end of the string, we put a backslash (\
) in front of the quotation mark which tells Java to include the following quotation mark in the string value rather than treating it as the end of the literal.
\n
which will be translated to a newline character, which will break the text across lines.
println
actually uses whatever is appropriate on the computer the program is running on. On MacOS and Linux that will be the same character as we can write in a string literal with \n
. On Windows, however, it will be the two character sequence that would be written \r\n
. To make our code run the same everywhere, we’d have to use only println
when we want a new line. The College Board does want you to know about the \n
escape sequence, however.
Scanner
class that comes with Java as one way. And knowing how to use Scanner
will let us write programs whose behavior can change depending on their input.
Scanner
class to read a name the user types in and then says hello. It will have different results depending on what the user types. Try it by typing in your name in the box below the code before you click “Run”. Try again with a friend’s name. The code works for any name.
Scanner
class, go to https://www.w3schools.com/java/java_user_input.asp, and for the newer Console
class, https://howtodoinjava.com/java-examples/console-input- output/. We are limited with the one way communication with the Java server in this Runestone ebook, but in most IDEs, the input/output would be more interactive. You can try this Scanner input example in JuiceMind (click on Create Starter Code after login with a Google account) or Scanner input example in Replit using the Scanner
class and Console input example using the Console
class. We will also learn how to use Scanner
with input files in a later unit.
System.out.print
and System.out.println
are Java output statements that display information on the computer screen. System.out.println
moves the cursor to a new line after the information has been displayed, while System.out.print
does not.
\
and have a special meaning in Java. Escape sequences used in this course include double quote "
, backslash \
, and newline n
.
Scanner
class is one way to obtain text input from the keyboard, although input from the keyboard will not be on the AP exam.
System.out.print("Java is ");
System.out.println("fun ");
System.out.print("and cool!");
Java is fun and cool!
Java isfun and cool!
Java is fun and cool!
Java is fun and cool!