2 of 3 make title about the code itself, not the objectives of code review ("_State what your code does in your title, not your main concerns about it._" - see https://codereview.stackexchange.com/questions/how-to-ask); update formatting, add tag

Finding count of character occurrences in a string

I am taking an introductory course in java in high school, however I have some experience in python. I was tasked to find the number of times a character occurs in a string so I did this.

String a = "hello";
String letter = "l";
int occurrence = a.length() - a.replace(letter, "").length();

Happy that I came up with a clever one-liner, I showed it to my teacher and she said that it's more elegant to use a for loop. What does she mean by that? How do we define elegance in code? Is it subjective? As an aside is this more efficient than a for loop or vice versa?