public class Exercise {
public static void main(String[] args)
{
String str = "This is a sample string.";
// Copy the contents of the String to a byte array.
// Only copy characters 4 through 10 from str.
// Fill the source array starting at position 2.
char[] arr = new char[] { ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ' };
str.getChars(4, 10, arr, 2);
// Display the contents of the byte array.
System.out.println("The char array equals \"" +
arr + "\"");
}
}
The char array equals "[C@15db9742"
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.