Here's my code for printing a string in reverse. It works perfectly.

I printed out the string normal and not in reverse no problem. I then just switched the else statement around and it worked. Great huh? But I have no idea how it prints the letter o first then the rest.

Could someone explain it to me?

Much appreciated.



static void printReverse(String s){
		if(s==null || s.equals("")){
			return;
		}
		else{ 
			printReverse(s.substring(1));        //  print in reverse
			System.out.println(s.charAt(0));
 
                        // print normal char per line
                        // System.out.println(s.charAt(0));
                        // printReverse(s.substring(1));
 
		}
}
 
public static void main(String[] args) {
 
       printReverse("hello");
 
}
	/*
		output is -
		o
		l
		l
                e
		h
	*/