From 704d0c84a356ac6581266932e11ad56086417200 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Fri, 6 Jun 2025 19:36:10 +0530 Subject: [PATCH 1/2] [Edit] Java: Strings --- content/java/concepts/strings/strings.md | 139 ++++++++++++++++++++--- 1 file changed, 122 insertions(+), 17 deletions(-) diff --git a/content/java/concepts/strings/strings.md b/content/java/concepts/strings/strings.md index 1004d5729d3..186b888ca94 100644 --- a/content/java/concepts/strings/strings.md +++ b/content/java/concepts/strings/strings.md @@ -1,43 +1,148 @@ --- Title: 'Strings' -Description: 'A string in Java is an object that holds a sequence of characters contained within a pair of double quotes (").' +Description: 'A string in Java is an object that holds a sequence of characters contained within a pair of double quotes ("").' Subjects: - 'Computer Science' + - 'Web Development' Tags: - - 'Strings' - - 'Data Types' - 'Characters' + - 'Data Types' + - 'Methods' + - 'Strings' CatalogContent: - 'learn-java' - 'paths/computer-science' --- -**Strings** in Java are objects that can hold a sequence of characters contained within a pair of double quotes (`"`). It is not a primitive data type. +**Strings** in Java are objects that can hold a sequence of characters contained within a pair of double quotes (""). Strings are immutable, meaning once a string is created, it cannot be changed. This immutability provides advantages in terms of performance, security, and thread safety. + +## Creating Strings + +In Java, there are two ways to create a string: + +- Using string literals +- Using the `new` keyword + +### Creating Strings Using String Literals + +This example uses string literals ("...") to create a string in Java: + +```java +class Main { + public static void main(String[] args) { + String s1 = "Hello"; + + System.out.println(s1); + } +} +``` + +Here is the output: + +```shell +Hello +``` + +### Creating Strings Using `new` + +This example uses the `new` keyword to create a string in Java: + +```java +class Main { + public static void main(String[] args) { + String s1 = new String("Hello"); + + System.out.println(s1); + } +} +``` + +Here is the output: + +```shell +Hello +``` + +## Accessing String Characters -Strings can either be compared by value via method (e.g., [`.equals()`](https://www.codecademy.com/resources/docs/java/strings/equals)) or by reference, or location in memory, (e.g., `==`) via operator. +In Java, an index refers to the numerical position of an element within a data structure like an array or a string. -## Example +The [`charAt()`](https://www.codecademy.com/resources/docs/java/strings/charAt) method is used to access string characters. -Java strings provide a way to store text such as words, sentences, or whole paragraphs. They can be any length and may contain letters, numbers, symbols, and spaces: +Here is an example that uses this [method](https://www.codecademy.com/resources/docs/java/methods) to access the first character (index `0`) of a Java string: ```java -import java.util.*; +class Main { + public static void main(String[] args) { + String str = "Java"; + + // Indexing starts at '0' + char ch = str.charAt(0); -class StringExample { + System.out.println(ch); + } +} +``` + +Here is the output: + +```shell +J +``` + +## Get the Length of a String + +The [`length()`](https://www.codecademy.com/resources/docs/java/strings/length) method can be used to get the length or the number of characters in a string: + +```java +String str = "Java Programming"; + +int len = str.length(); + +System.out.println("Length: " + len); +``` + +Here is the output: + +```shell +Length: 16 +``` + +## Join Two Strings + +The [`concat()`](https://www.codecademy.com/resources/docs/java/strings/concat) method is used to join two strings in Java: + +```java +class Main { public static void main(String[] args) { - // Using a string literal - System.out.println("Codecademy"); + String first = "Hello "; + String second = "World"; - // Creating a String variable - String address = "575 Broadway #5, New York, NY 10012"; - System.out.println(address); + String result = first.concat(second); + + System.out.println(result); } } ``` -This will output the following: +Here is the output: ```shell -Codecademy -575 Broadway #5, New York, NY 10012 +Hello World ``` + +## Frequently Asked Questions + +### 1. Are strings in Java mutable? + +No, strings in Java are immutable. To create mutable strings, use `StringBuilder` or `StringBuffer`. + +### 2. What is the difference between `String`, `StringBuilder`, and `StringBuffer` in Java? + +- `String` is immutable. +- `StringBuilder` is mutable and not thread-safe. +- `StringBuffer` is mutable and thread-safe. + +### 3. Can we override methods of the `String` class in Java? + +No. the `String` class in Java is final, which means it cannot be subclassed. From 3c43f46b0ee1d65accad7432b520cb1a49e59c56 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sun, 8 Jun 2025 17:04:34 +0530 Subject: [PATCH 2/2] minor content fixes --- content/java/concepts/strings/strings.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/java/concepts/strings/strings.md b/content/java/concepts/strings/strings.md index 186b888ca94..6ca79a69fee 100644 --- a/content/java/concepts/strings/strings.md +++ b/content/java/concepts/strings/strings.md @@ -1,6 +1,6 @@ --- Title: 'Strings' -Description: 'A string in Java is an object that holds a sequence of characters contained within a pair of double quotes ("").' +Description: 'Strings in Java are immutable objects that represent sequences of characters enclosed in double quotes.' Subjects: - 'Computer Science' - 'Web Development' @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -**Strings** in Java are objects that can hold a sequence of characters contained within a pair of double quotes (""). Strings are immutable, meaning once a string is created, it cannot be changed. This immutability provides advantages in terms of performance, security, and thread safety. +**Strings** in Java are objects that can hold a sequence of characters contained within a pair of double quotes (`""`). Strings are immutable, meaning once a string is created, it cannot be changed. This provides benefits such as better performance, security, and thread safety. ## Creating Strings