|
1 | 1 | --- |
2 | 2 | Title: 'Strings' |
3 | | -Description: 'A string in Java is an object that holds a sequence of characters contained within a pair of double quotes (").' |
| 3 | +Description: 'Strings in Java are immutable objects that represent sequences of characters enclosed in double quotes.' |
4 | 4 | Subjects: |
5 | 5 | - 'Computer Science' |
| 6 | + - 'Web Development' |
6 | 7 | Tags: |
7 | | - - 'Strings' |
8 | | - - 'Data Types' |
9 | 8 | - 'Characters' |
| 9 | + - 'Data Types' |
| 10 | + - 'Methods' |
| 11 | + - 'Strings' |
10 | 12 | CatalogContent: |
11 | 13 | - 'learn-java' |
12 | 14 | - 'paths/computer-science' |
13 | 15 | --- |
14 | 16 |
|
15 | | -**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. |
| 17 | +**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. |
| 18 | + |
| 19 | +## Creating Strings |
| 20 | + |
| 21 | +In Java, there are two ways to create a string: |
| 22 | + |
| 23 | +- Using string literals |
| 24 | +- Using the `new` keyword |
| 25 | + |
| 26 | +### Creating Strings Using String Literals |
| 27 | + |
| 28 | +This example uses string literals ("...") to create a string in Java: |
| 29 | + |
| 30 | +```java |
| 31 | +class Main { |
| 32 | + public static void main(String[] args) { |
| 33 | + String s1 = "Hello"; |
| 34 | + |
| 35 | + System.out.println(s1); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Here is the output: |
| 41 | + |
| 42 | +```shell |
| 43 | +Hello |
| 44 | +``` |
| 45 | + |
| 46 | +### Creating Strings Using `new` |
| 47 | + |
| 48 | +This example uses the `new` keyword to create a string in Java: |
| 49 | + |
| 50 | +```java |
| 51 | +class Main { |
| 52 | + public static void main(String[] args) { |
| 53 | + String s1 = new String("Hello"); |
| 54 | + |
| 55 | + System.out.println(s1); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Here is the output: |
| 61 | + |
| 62 | +```shell |
| 63 | +Hello |
| 64 | +``` |
| 65 | + |
| 66 | +## Accessing String Characters |
16 | 67 |
|
17 | | -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. |
| 68 | +In Java, an index refers to the numerical position of an element within a data structure like an array or a string. |
18 | 69 |
|
19 | | -## Example |
| 70 | +The [`charAt()`](https://www.codecademy.com/resources/docs/java/strings/charAt) method is used to access string characters. |
20 | 71 |
|
21 | | -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: |
| 72 | +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: |
22 | 73 |
|
23 | 74 | ```java |
24 | | -import java.util.*; |
| 75 | +class Main { |
| 76 | + public static void main(String[] args) { |
| 77 | + String str = "Java"; |
| 78 | + |
| 79 | + // Indexing starts at '0' |
| 80 | + char ch = str.charAt(0); |
25 | 81 |
|
26 | | -class StringExample { |
| 82 | + System.out.println(ch); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Here is the output: |
| 88 | + |
| 89 | +```shell |
| 90 | +J |
| 91 | +``` |
| 92 | + |
| 93 | +## Get the Length of a String |
| 94 | + |
| 95 | +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: |
| 96 | + |
| 97 | +```java |
| 98 | +String str = "Java Programming"; |
| 99 | + |
| 100 | +int len = str.length(); |
| 101 | + |
| 102 | +System.out.println("Length: " + len); |
| 103 | +``` |
| 104 | + |
| 105 | +Here is the output: |
| 106 | + |
| 107 | +```shell |
| 108 | +Length: 16 |
| 109 | +``` |
| 110 | + |
| 111 | +## Join Two Strings |
| 112 | + |
| 113 | +The [`concat()`](https://www.codecademy.com/resources/docs/java/strings/concat) method is used to join two strings in Java: |
| 114 | + |
| 115 | +```java |
| 116 | +class Main { |
27 | 117 | public static void main(String[] args) { |
28 | | - // Using a string literal |
29 | | - System.out.println("Codecademy"); |
| 118 | + String first = "Hello "; |
| 119 | + String second = "World"; |
30 | 120 |
|
31 | | - // Creating a String variable |
32 | | - String address = "575 Broadway #5, New York, NY 10012"; |
33 | | - System.out.println(address); |
| 121 | + String result = first.concat(second); |
| 122 | + |
| 123 | + System.out.println(result); |
34 | 124 | } |
35 | 125 | } |
36 | 126 | ``` |
37 | 127 |
|
38 | | -This will output the following: |
| 128 | +Here is the output: |
39 | 129 |
|
40 | 130 | ```shell |
41 | | -Codecademy |
42 | | -575 Broadway #5, New York, NY 10012 |
| 131 | +Hello World |
43 | 132 | ``` |
| 133 | + |
| 134 | +## Frequently Asked Questions |
| 135 | + |
| 136 | +### 1. Are strings in Java mutable? |
| 137 | + |
| 138 | +No, strings in Java are immutable. To create mutable strings, use `StringBuilder` or `StringBuffer`. |
| 139 | + |
| 140 | +### 2. What is the difference between `String`, `StringBuilder`, and `StringBuffer` in Java? |
| 141 | + |
| 142 | +- `String` is immutable. |
| 143 | +- `StringBuilder` is mutable and not thread-safe. |
| 144 | +- `StringBuffer` is mutable and thread-safe. |
| 145 | + |
| 146 | +### 3. Can we override methods of the `String` class in Java? |
| 147 | + |
| 148 | +No. the `String` class in Java is final, which means it cannot be subclassed. |
0 commit comments