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

Time:09-30

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)

CodePudding user response:

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.

CodePudding user response:

Nothing happens, the code will still compile. You can name your methods getName, GETname, getNAME, GETNAME, or GeTnAmE and the compiler will not care.

Who will care however are human people reading and trying to understand the code. Make it easy for your fellow programmers and future you to read code.

You could also name them föΘBÄRẞ and your code will still compile. But nobody reading the code could make any sense of it.

CodePudding user response:

Usually nothing special happens to the code itself, but other people used to the conventional style will have troubles understanding it and therefore you will e.g. have a lower rate of answers to your SO-questions if they contain code that doesn't follow the conventions.

But because you mentioned it extra:

To start a java program with the java command (this is the standard way), your main method has to have a defined signature, as stated in the documentation The java Command:

The method declaration has the following form:

public static void main(String[] args)

So in this case, if your main method is named Main and doesn't have that exact give signature (public static, no return value, array of String as parameter), your program will not start.

And of course if you are overloading or overwriting or implementing methods from other classes or interfaces, you have to copy their name exactly and must not change the case - and as e.g. overwriting of equals and hashcode from the Object class are quit common, it would result in a mixture of casing, and from my experience I can say this is super confusing - even if you want to understand your own code a few days after you wrote it.

CodePudding user response:

Syntax wise it wont make any difference but the standard says that you should be following camel case when naming methods or variables. An Excerpt from official doc.

"Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized."

https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

  • Related