-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics.java
More file actions
30 lines (24 loc) · 1.02 KB
/
Copy pathBasics.java
File metadata and controls
30 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Strings;
public class Basics {
public static void main(String[] args) {
// creation of String
String fruit = "Apple"; // initial creation of a string reference variable pointing to an object "Apple" & "Apple" is stored in string pool
System.out.println(fruit);
fruit = "Banana"; // A new string "Banana" is created in the pool & fruit now points to "Banana"
System.out.println(fruit);
// "Apple still exists in the pool only the reference changed, not the string."
/*
Strings are immutable → "Apple" cannot become "Banana"
Variable fruit is mutable → it can point to a different string
*/
// accessing the single char of string :
String s = "Guitar";
System.out.println(s.charAt(4));
// converting a string into an array of characters
for(char ele : s.toCharArray()){
// System.out.print(ele + " ");
if(ele == 't')
System.out.println("found");
}
}
}