-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringConstructorExample1.java
More file actions
24 lines (21 loc) · 1.06 KB
/
StringConstructorExample1.java
File metadata and controls
24 lines (21 loc) · 1.06 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
public class StringConstructorExample1 {
public static void main(String[] args) {
// Creating String from String literal
String str = "abc"; // internally converted to -> String str = new String("abc");
String str1 = new String("abcdef"); // It says (new String) is redundant
System.out.println(str1); // Prints -> abcdef
// creating String from char[] array
char arr[] = {'o','b','j','e','c','t'};
String str2 = new String(arr);
System.out.println(str2); // Prints -> object
String str3 = new String(arr,2,3);
System.out.println(str3); // output -> jec
// Creating string from byte array
byte bytes[] = {45,65,90,78,23,89};
String str4 = new String(bytes);
System.out.println(str4); // output - > -AZNY // It takes ascii values
String str5 = new String(bytes,2,4);
System.out.println(str5); // output - > ZNY
//String str6 = new String(bytes,3,4); // java.lang.StringIndexOutOfBoundsException
}
}