Camel case is a naming convention used in programming where multiple words are combined into one identifier. It is called camel case because the capital letters within the identifier resemble the humps of a camel. Camel case eliminates spaces between words. Instead, words are concatenated (combined together), and each subsequent word starts with a capital letter. There are two main styles of camel case: upper and lower camel case:
Lower Camel Case - The first letter of the identifier starts with a lowercase letter, and each subsequent word's initial letter is capitalized.
lowerCamelCase
Upper Camel Case - The first letter of the identifier starts with an uppercase letter, and each subsequent word's initial letter is capitalized. Upper camel case is sometimes also called PascalCase.
UpperCamelCase
Standard Practice
When using camel case, there are standard practices, or guidelines, that should be followed:
Avoid using special characters like hyphens, underscores, or any other punctuation in camel case identifiers. Stick to letters and numbers only.
You can use numbers in a camel case name, but the name should never begin with a number.
projectVersion1 - This is acceptable.
1stProjectVersion - This is NOT.
When Is Lower Case Preferred?
Lower Camel Case is commonly used for naming variables and methods, but usage is up to personal preference. Here are a few examples:
Variables// Declaring and initializing variables int myAge = 25; double accountBalance = 1000.50; String firstName = "John"; boolean isLoggedIn = false;
// Method declaration public void printMessage() { System.out.println("Hello, world!"); } public double calculateSum(double num1, double num2) { return num1 + num2; }
When Is Upper Case Preferred?
Upper Camel Case is often used for naming classes, interfaces, and enums. Here's how you can use upper camel case in these scenarios:
Classes
public class Person { // Class code goes here } public class BankAccount { // Class code goes here }
Interfaces
public interface MyInterface { // Interface code goes here }
Enums
public enum DaysOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
Camel case is a widely adopted convention across different programming languages. It's a useful naming convention for creating readable and consistent identifiers in code. Whatever your preference, whether you choose to code using lower or upper camel case, the key is to remain consistent. Following the accepted standard will make your code easier to understand for other developers on your team.