-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit1_1_2.java
More file actions
executable file
·42 lines (37 loc) · 2.25 KB
/
Copy pathunit1_1_2.java
File metadata and controls
executable file
·42 lines (37 loc) · 2.25 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
31
32
33
34
35
36
37
38
39
40
41
42
class unit1_1_2 {
public static void main(String[] args) {
// Encounter the String data type, and know that strings are
// specified using double quotes
// In Java, a String can be considered as an array of characters,
// and the sequence of characters is called a string.
String str1 = "Hello World";
System.out.println("str1: " + str1);
// The above string is equivalent to an array of char as follows:
char ch[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
String str2 = new String(ch);
System.out.println("str2: " + str2);
// Length of a String is represented by an integer
System.out.println("Length of str2: " + str2.length());
System.out.println("char at index 0 of str2: " + str2.charAt(0));
System.out.println("char at index 1 of str2: " + str2.charAt(1));
System.out.println("char at index 2 of str2: " + str2.charAt(2));
System.out.println("char at index 3 of str2: " + str2.charAt(3));
System.out.println("char at index 4 of str2: " + str2.charAt(4));
System.out.println("char at index 5 of str2: " + str2.charAt(5));
System.out.println("char at index 6 of str2: " + str2.charAt(6));
System.out.println("char at index 7 of str2: " + str2.charAt(7));
System.out.println("char at index 8 of str2: " + str2.charAt(8));
System.out.println("char at index 9 of str2: " + str2.charAt(9));
System.out.println("char at index 10 of str2: " + str2.charAt(10));
// The size of int in Java is 4 bytes (included a signed bit, i.e. MSB). [MSB-> Most Significant Bit]
// 4 bytes: [sign bit]bbbbbbb bbbbbbbb bbbbbbbb bbbbbbbb where b stands for 0 or 1;
// Sign bit 0 is +ve, 1 is -ve
//
// The range of integer data type is -2^31 to (2^31)-1 (-2147483648 to 2147483647).
// Remember that we cannot use negative values for indexing.
// The indexing is done within the maximum range.
// It means that we cannot store the 2147483648th character.
// Therefore, the maximum length of String in Java is 0 to 2147483647.
// So, we can have a String with the length of 2,147,483,647 characters, theoretically.
}
}