Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
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 or print - these methods print a line of text;
  • System class and its out 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 SequenceDescription
\nnew line
\rcarriage-return
\ttab
\"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
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment