The argument you pass from command line is not actually unicode character but it's a String
of unicode character which is escaped with \
. Ultimately, your String will become \\u0001
and that's why it is printing \u0001
. Same way, if you enter \
as a command line argument it will become \\
to escape your backward slash.
While the String
you have declared in main is actually unicode character.
String escapedstring = "\\u0001";//in args[0]
String unicodeChar = "\u0001";// in str
So, now you want \\u0001
to be converted into \u0001
and there are lot of ways to achieve that. i.e. you can use StringEscapeUtils#unescapeJava
method of utility or you can also try following way.
String str = "\\u0001";
char unicodeChar = (char) Integer.parseInt(str.substring(2));
System.out.println(unicodeChar);
NOTE : You can find other ways to convert unicode String to unicode characters in following question.(Already provided in comment by Marcinek
)