Session 1 |
Strings
A string is any sequence of characters – letters, digits, spaces, punctuation, and other “special characters” placed between the quotes.
A few examples on strings: "abc", "12345", "<}{>", "!", "".
To print something out, use one of the following statements:
//if a statement has to be printed and the cursor must be placed on a new line, use println System.out.println("Hello, world!") // if several print statements have to be printed on the same line, use print System.out.print("Hello, world!")
These statements are used to print out the string "Hello, world!".
println
orprint
- these methods print a line of text;System
class and itsout
stream are used to access the print method;- a string must be surrounded by a pair of double quotes.
Non-printable and control characters can be represented by an escape sequence, which begins with a back-slash \
followed by a pattern. The commonly-used escape sequences are:
Escape Sequence | Description |
\n | new line |
\r | carriage-return |
\t | tab |
\" | double-quote |
\' | single-quote |
\\ | back-slash |
Self-assessment
What is the output of the following program?
public class HelloWorld { public static void main(String[] args){ String word = "Hello"; System.out.println(word + ", World!"); System.out.println(word + 123); System.out.println(123 + "123" + 12 +3); } }
Run the code in IntelliJ and check your answer!
Session 1 |