Fiveable

🧵Programming Languages and Techniques I Unit 11 Review

QR code for Programming Languages and Techniques I practice questions

11.1 String Declaration and Initialization

11.1 String Declaration and Initialization

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🧵Programming Languages and Techniques I
Unit & Topic Study Guides

Strings are fundamental data structures in programming languages, used to represent text. They can be declared and initialized using literals, constructors, or by converting character arrays. String manipulation often creates new objects due to immutability.

String objects and literals differ in memory allocation and comparison behavior. Understanding these differences, along with concepts like the string pool and interning, is crucial for optimizing memory usage and performance in string-heavy applications.

String Fundamentals

String declaration and initialization

  • String literals enclosed in double quotes "Hello, World!" or single quotes 'Hello, World!' in some languages
  • String constructor using the new keyword new String("Hello") creates object in heap memory
  • Character arrays converted to strings using methods like String.valueOf() (Java)
  • String concatenation joins strings with + operator or concat() method ("Hello" + "World")
  • String formatting with printf() or format() methods for complex string construction
  • String interpolation embeds expressions directly in string literals (f"Value: {variable}" in Python)
String declaration and initialization, How does the String class work internally in Java? - Stack Overflow

String literals vs objects

  • String literals stored in string pool for memory efficiency with duplicate values
  • String objects created with new keyword stored in heap memory, always create new instance
  • Comparison using == operator compares references for objects, values for literals
  • Intern() method adds string objects to string pool for optimization
String declaration and initialization, C++ 문자열 연결(String Concatenation) 총정리

Immutability of strings

  • Immutability means strings cannot be changed after creation, ensuring thread safety and security
  • String manipulation creates new string objects, leaving original unchanged
  • StringBuilder and StringBuffer provide mutable alternatives for efficient string manipulation

Memory allocation for strings

  • String pool (constant pool) optimizes memory usage for string literals
  • Heap memory stores string objects created with new keyword
  • Stack memory holds references to string objects
  • Garbage collection automatically manages memory for unused strings
  • String interning stores only one copy of each distinct string value
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly → and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →