So, you are asking about these two lines:
String s1 = new String(s.getBytes("UTF-8"), "UTF-8"); // line 1
String s2 = new String(s.getBytes(), "UTF-8"); // line 2
Both these lines are not doing anything useful. Line 2 is even worse than line 1; it might not just be useless, but wrong, depending on what the default character encoding of your system is.
Line 1 effectively does nothing. It encodes the string s
into bytes using the UTF-8 character encoding, and then immediately decodes the bytes back into a string using UTF-8. The string s1
will always contain exactly the same as the original string; the encoding and decoding is useless.
What line 2 does, depends on the default character encoding that's being used on your system. If the default character encoding is UTF-8, then it does exactly the same as line 1. If it is something different than UTF-8, then you get an incorrectly decoded string.
Suppose that the default character encoding of your system is ISO-8859-1. Then line 2 encodes the string using ISO-8859-1, and then it immediately decodes the result as if it is UTF-8 - which is wrong. You might get a string with incorrectly decoded characters, or even an exception.
Read the API documentation of the methods you're using to understand what exactly they do: