Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What happens if the first letter of a method in a java program is a capital / upper case letter?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading