---
title: "String Literal — AP Comp Sci A Definition & Exam Guide"
description: "A string literal is text in double quotes, like \"Hello\", that creates a String object in Java. Learn escape sequences, concatenation, and how AP CSA tests it."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/string-literal"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 1"
---

# String Literal — AP Comp Sci A Definition & Exam Guide

## Definition

In AP Computer Science A, a string literal is a sequence of characters enclosed in double quotes (like "Hello") that represents a fixed text value in code and creates a String object directly, without calling the String constructor (EK 1.3.B.2).

## What It Is

A string literal is text written directly in your code inside [double](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") quotes, like `"Hello World"`. The CED defines a literal as "the code representation of a fixed value" (EK 1.3.B.1), so a string literal is just the fixed-text version of that idea. When Java sees `"Java"` in your code, it creates a [String object](/ap-comp-sci-a/unit-1/string-methods/study-guide/SltCtk8JxBIgHcMfG6G4 "fv-autolink") holding those four characters. Writing `String message = "Hello World";` is the standard way to make a String, even though you could also call the String constructor (EK 1.15.A.1).

Some characters can't be typed inside quotes the normal way, so Java uses **escape sequences** that start with a backslash. The three you need for the exam are `\"` for a double quote, `\\` for a backslash, and `\n` for a newline (EK 1.3.B.3). So `System.out.println("Hello\nWorld")` prints Hello and World on two separate lines. One more thing that trips people up: the quotes are not part of the string. `"Java"` is a 4-character string, and printing it never shows the quotation marks.

## Why It Matters

String literals live in [Unit 1](/ap-comp-sci-a/unit-1 "fv-autolink") (Using Objects and Methods) and show up in two topics. Topic 1.3 gives them their own learning objective, 1.3.B, which says you should "develop code to utilize string literals and determine the result of using string literals." Topic 1.15 picks the idea back up with LO 1.15.A, since a string literal is the most common way to create a String object. They're also the raw material for output. Almost every `System.out.print` and `System.out.println` statement you trace on the exam (LO 1.3.A) has a string literal inside it. If you can't read escape sequences or track what `+` does between strings, you'll miss easy output-tracing points all year, because Strings reappear in loops (Unit 2), conditionals, and [methods](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") that build and return text.

## Connections

### String Concatenation with + and += (Unit 1)

The same `+` symbol that adds numbers joins strings. `"Hello" + "World"` produces "HelloWorld" with no space, because Java only includes the characters you actually typed inside the quotes (EK 1.15.A.4).

### System.out.print and println (Unit 1)

String literals are what you usually hand to these methods. The classic exam trap pairs them together. `print` keeps the cursor on the same line, `println` moves it down, and a `\n` inside a literal forces a line break mid-string.

### String Methods like charAt, indexOf, and substring (Unit 1)

Once a literal creates a String object, every String method works on it. `"Hello".length()` returns 5, and indices run from 0 to length minus 1, so `charAt(5)` on that string throws a StringIndexOutOfBoundsException.

### Operator Precedence and Mixed Expressions (Unit 1)

Expressions like `"Sum: " + 1 + 2` evaluate left to right, so once a string enters the chain, everything after it becomes text. That prints "Sum: 12", not "Sum: 3". This is one of the most reliable MCQ traps in Unit 1.

## On the AP Exam

String literals are mostly tested in Unit 1 multiple-choice questions, in two flavors. The first asks you to identify correct code, like which segment compiles and creates a String containing "Java" or the most conventional way to store "Hello World" in a variable (answer: `String message = "Hello World";`). The second asks you to trace output. Expect a stack of print and println calls, often with a `\n` hidden inside a literal, where you have to track exactly which characters land on which line. No released FRQ centers on the term itself, but nearly every FRQ that builds or returns a String (very common in Methods and Classes questions) requires you to write literals correctly, including spaces and punctuation inside the quotes. A missing space in `"Hello " + name` is a real way to lose a point.

## string literal vs String object

A string literal is the code you type (the characters between double quotes), while a String object is the thing in memory that the literal creates. Per EK 1.15.A.1, you can make a String object two ways, from a literal like `"Hi"` or by calling the String constructor with `new String("Hi")`. The literal is the shortcut syntax; the object is the result. Either way, the resulting String is immutable, so its contents never change.

## Key Takeaways

- A string literal is a sequence of characters enclosed in double quotes, and it's the code representation of a fixed text value (EK 1.3.B.1 and 1.3.B.2).
- Writing a string literal like "Hello" creates a String object directly, which is the standard alternative to calling the String constructor (EK 1.15.A.1).
- The three escape sequences on the AP exam are \" for a double quote, \\ for a backslash, and \n for a newline (EK 1.3.B.3).
- The quotation marks are not part of the string, so "Java" has a length of 4 and printing it shows no quotes.
- Concatenation with + evaluates left to right, so "Sum: " + 1 + 2 prints "Sum: 12" because the numbers become text once a string is involved.
- String objects created from literals are immutable, so methods called on them return new Strings instead of changing the original (EK 1.15.A.3).

## FAQs

### What is a string literal in AP Computer Science A?

It's a sequence of characters enclosed in double quotes, like "Hello World", that represents a fixed text value in code (EK 1.3.B.2). Writing one creates a String object you can store in a variable or print.

### Do I need to use new String() to create a String in Java?

No. Assigning a literal directly, like String s = "Hi";, is the standard and most conventional way on the AP exam. The [constructor](/ap-comp-sci-a/unit-1/creating-and-storing-objects/study-guide/rUOTKl6Ih5noXJ0GtxJF "fv-autolink") (new String("Hi")) works too, but the literal is what you'll write in FRQs.

### What's the difference between a string literal and a char?

A string literal uses double quotes and can hold any number of characters, while a char uses single quotes and holds exactly one. So "A" is a String of length 1, but 'A' is a char, and they are different types in Java.

### What does \n do in a string literal?

It's an [escape sequence](/ap-comp-sci-a/key-terms/escape-sequence "fv-autolink") that inserts a newline, so System.out.println("Hello\nWorld") prints Hello and World on two separate lines. The other escape sequences on the exam are \" for a double quote and \\ for a backslash.

### Are the quotation marks part of the string?

No. The quotes just mark where the literal starts and ends, so "Java" contains exactly 4 characters and length() returns 4. To actually include a quote character in the text, you need the escape sequence \".

## Related Study Guides

- [1.3 Expressions and Assignment Statements](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/string-literal#resource","name":"String Literal — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/string-literal","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/string-literal#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:18.343Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/string-literal#term","name":"string literal","description":"In AP Computer Science A, a string literal is a sequence of characters enclosed in double quotes (like \"Hello\") that represents a fixed text value in code and creates a String object directly, without calling the String constructor (EK 1.3.B.2).","url":"https://fiveable.me/ap-comp-sci-a/key-terms/string-literal","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a string literal in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"It's a sequence of characters enclosed in double quotes, like \"Hello World\", that represents a fixed text value in code (EK 1.3.B.2). Writing one creates a String object you can store in a variable or print."}},{"@type":"Question","name":"Do I need to use new String() to create a String in Java?","acceptedAnswer":{"@type":"Answer","text":"No. Assigning a literal directly, like String s = \"Hi\";, is the standard and most conventional way on the AP exam. The [constructor](/ap-comp-sci-a/unit-1/creating-and-storing-objects/study-guide/rUOTKl6Ih5noXJ0GtxJF \"fv-autolink\") (new String(\"Hi\")) works too, but the literal is what you'll write in FRQs."}},{"@type":"Question","name":"What's the difference between a string literal and a char?","acceptedAnswer":{"@type":"Answer","text":"A string literal uses double quotes and can hold any number of characters, while a char uses single quotes and holds exactly one. So \"A\" is a String of length 1, but 'A' is a char, and they are different types in Java."}},{"@type":"Question","name":"What does \\n do in a string literal?","acceptedAnswer":{"@type":"Answer","text":"It's an [escape sequence](/ap-comp-sci-a/key-terms/escape-sequence \"fv-autolink\") that inserts a newline, so System.out.println(\"Hello\\nWorld\") prints Hello and World on two separate lines. The other escape sequences on the exam are \\\" for a double quote and \\\\ for a backslash."}},{"@type":"Question","name":"Are the quotation marks part of the string?","acceptedAnswer":{"@type":"Answer","text":"No. The quotes just mark where the literal starts and ends, so \"Java\" contains exactly 4 characters and length() returns 4. To actually include a quote character in the text, you need the escape sequence \\\"."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 1","item":"https://fiveable.me/ap-comp-sci-a/unit-1"},{"@type":"ListItem","position":4,"name":"string literal"}]}]}
```
