Arvutiteaduse instituut
  1. Kursused
  2. 2017/18 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (P2NC.01.083)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2017/18 kevad

  • 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
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused