0

Getting No Output. Can anyone say why?

class Myclass { public static void main(String[] args) { String [] arr = {"a" , "b" , "c"}; String a="abc"; String [] part = a.split(""); for(int i=0;i<part.length;i++) { if(arr[i]==part[i]) { System.out.print(part[i]); } } } }

3rd Jul 2017, 11:26 AM
Joseph Nithish
Joseph Nithish - avatar
2 Respostas
+ 2
Because if(arr[i] == part[i]) is false on every line. Change it to if(arr[i].equals(part[i])) and it should work. Always use equals() method to compare Strings (and other objects too). Because (arr[i] == part[i]) checks if it is exactly the same object
3rd Jul 2017, 11:43 AM
Eligijus Silkartas
Eligijus Silkartas - avatar
0
You are using wrong comparator == is for object reference. Use equals method to compare content arr[i].equals(part[i])
3rd Jul 2017, 11:42 AM
zdena