- 2
About Java MCQ
What is the output of this code? String[]a=(â76.88â).split(â.â); System.out.Println(a.length); But how the output is 0?
4 RĂ©ponses
+ 2
Mustakim Rahman
dot (.) is a special character of Java Regex so you cannot split string on dot that's why length is 0
If there is \\. then output is 2
+ 3
You have syntax errors in your snippet. Quotation marks are not standard, and println must be lowercase.
Anyway, split argument is actually a regular expression. In regex world, the dot means "any character". So if you split the string across any character, then there is nothing left!
If you want to split at the dot, you need to escape it in the regex, and in Java you need to use double backslashes.
String[] a = "76.88".split("\\.");
System.out.println(a.length);
// output: 2
0
I also think 2 but in mcq challenge it shows 0 as correct answere
0
As I said, the code in your description does not work, because it has syntax errors.