It converts characters to prevent script execution.
HTML encoding transforms characters like '' into HTML entities, preventing browsers from executing malicious scripts. In Java, libraries like OWASP's Java Encoder can be used to apply HTML encoding on user-generated content before rendering it.
Additional Notes
How does HTML encoding prevent stored XSS attacks?