I am aware that in Java language, the first letter of a method should desirably be lower case, but what happens if it becomes a capital letter?
I tried changing the first letter of the methods in my program to capital letters, but it made no difference.
import java.util.Scanner;
class Replace3meth
{
String str, newstr;
int len, vcount;
public void Accept()
{ // obtaining input
System.out.println("\f");//clearing the screen
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
str = sc.nextLine();
str = str.toLowerCase();
} // accept() method ends
public void ReplaceVowel()
{
len = str.length();
newstr = "";
vcount = 0;
char c;
for (int i = 0; i < len; i++)
{ // finding no. of vowels
c = str.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
c = Character.toUpperCase(c);
vcount++;
} // if function ends
newstr = newstr + c;
} // for loop ends
} // replaceVowel() method ends
public void Display()
{ // printing output
System.out.println("Original sentence:" +str);
System.out.println("New sentence: "+newstr);
System.out.println("No. of vowels: "+vcount);
} // display() method ends
public static void Main()
{ // calling all methods
Replace3meth obj = new Replace3meth();
obj.Accept();
obj.ReplaceVowel();
obj.Display();
} // main() method ends
} // class ends
This is a simple program to convert all the vowels in a sentence to upper case. Even after changing all the method names to start with a capital letter, the program worked properly.
PS: I use BlueJ (not VSCode), so I can used main() instead of main(String[] args)
>Solution :
Method names don’t have to start with a lower case letter. It’s just a shared convention so all developers are on the same page and people reading your code don’t have to add extra cognitive load to quickly parse it.
Method names are case-sensitive, though. So if, for any reason, a name is expected to have a certain case it has to be respected.
One example is methods that need to be implemented because they are declared in an interface or abstract class (if the method you have to implement is called doStuff you can’t implement it as DoStuff or dostuff).
Another example is the main method, which is the method the JVM expects to call when you run a class. It has to maintain the same case (and the fact that it has to be public, static, void and have an array of strings as its only argument), so you can’t call it Main or MaiN. But that is a requirement of the JVM, not of the language.