Java - Write code to capitalize the first letter of a string

Requirements

Write code to capitalize the first letter of a string

abc becomes Abc.

Hint

You can convert the string to char array.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(capitalize(str));
    }//from   w w  w  .j a v  a  2s .co  m

    public static String capitalize(String str) {

        if (str == null || str.isEmpty()) {
            return "";
        }

        char[] buffer = str.toCharArray();
        buffer[0] = Character.toTitleCase(buffer[0]);

        return new String(buffer);
    }
}