Table of contents
1.
Ways of Using Camel Case
2.
Normal String to Camel Case
3.
Algorithm
4.
Lower Camel Case Implementation
5.
Upper Camel Case Implementation
6.
Camel Case Vs Snake Case
6.1.
Camel Case to Snake Case Implementation
6.1.1.
Implementation
6.1.2.
Output
6.2.
Snake Case to Camel Case Implementation
6.2.1.
Implementation
6.2.2.
Output
7.
Frequently Asked Questions
7.1.
Does Java use CamelCase?
7.2.
Should constants use camel case in Java?
7.3.
What are the advantages of using camel case in Java programming?
7.4.
What are camel case and Pascal case in Java?
8.
Conclusion
Last Updated: Nov 29, 2024
Easy

Camel Case in Java

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Camel case in Java is a naming convention used for writing identifiers like method names & variable names. In this, each word in the identifier starts with a capital letter and the first word starts with a lowercase letter. For example, "phoneNumber"  where "phone" starts with lowercase letters, and "Number" starts with an uppercase letter. It's named as camel case because the capital letters in the middle of the identifier hump up like a camel's back.

Camel-case syntax is used in Java to name classes, interfaces, methods, and constants. If the name is merged with two words, the second word will always begin with an uppercase letter, such as maxMarks(), lastName, or ClassTest, with all whitespaces removed.

camel case in java

In this article, we will be discussing about camel case in Java. We will discuss about what camel case is and how it can be implemented. We will discuss the camel case vs. the snake case as well. Let us understand what the camel case is.

By capitalizing the initial letter of each word and omitting spaces, camelCase is a style used to divide phrases into individual words. Camel case in Java plays an important role in the naming conventions. It is used to name classes, variables, interfaces, and functions. If some variable name is a combination of two or three words, then we can combine them using camel case style and omit spaces. For example, if the actual word is the student last name, then using camel case, we can make it as studentLastName or StudentLastName. As we can see in the example, we have written the variable name in two different ways. 

Ways of Using Camel Case

There are two ways of using Camel case: 

UpperCamelCase: In this case, where the first letter of the first word is capitalized, also known as the title case. When naming classes and interfaces, this pattern is typically used, for example, Students, Companies, etc. We can also say UpperCamelCase as pascal case. Pascal case is similar to the camel case, but it does not include lowerCamelCase. Examples of the pascal case are IndexOutOfMemory, LinkedList, etc.

LowerCamelCase: It is used when the first letter of the first word is in lowercase. The name of the methods and variables often follows this convention. For example studentName, studentFatherName, displayArray(), etc.
Let us discuss how we can make a normal string in the camel case.

Also see, 

Normal String to Camel Case

By simply deleting the spaces from a normal string, it is possible to turn it into either lowerCamelCase or UpperCamelCase. Before moving on to the implementation of it let us understand the algorithm.

Algorithm

  1. Traverse the character array(characterArr), character by character.
  2. The initial letter of the string at index = 0 is either converted to lowerCase (when following lowerCamelCase) or UpperCase (when following UpperCamelCase).
  3. The array is checked for blank spaces, and the letter immediately following the space will going to convert to the UpperCase.
  4. If the non-space character is present, then it will be copied to the result array(resArr).

Lower Camel Case Implementation

Let us implement lowerCamelCase in Java. Consider the following input:

Input: Hello Ninjas

Expected Output: helloNinjas

public class LowerCamelCase {
    static String convertStringToLowerCamelCase(String str) {
        // Checking Spaces
        int checkSpace = 0;
        // Length of the string
        int len = str.length();
        // String to character array conversion
        char characterArr[] = str.toCharArray();
        // Checking index for characterArr
        int cIndex = 0;
        // Traversing each character of the characterArr
        for (int i = 0; i < len; i++) {
            // The first letter should be small
            if (i == 0)
                characterArr[i] = Character.toLowerCase(characterArr[i]);
            // Check empty spaces
            if (characterArr[i] == ' ') {
                // If space found then
                checkSpace++;
                // Convert the letter to UpperCase after a space
                characterArr[i + 1] = Character.toUpperCase(characterArr[i + 1]);
                continue;
            } else
                characterArr[cIndex++] = characterArr[i];
        }
        // Return string
        return String.valueOf(characterArr, 0, len - checkSpace);
    }

    public static void main(String args[]) {
        String name = "aditya kumar";
        System.out.println(convertStringToLowerCamelCase(name));
        String ageCalculation = "calculate age()";
        System.out.println(convertStringToLowerCamelCase(ageCalculation));
        String str = "Hello ninjas how are you";
        System.out.println(convertStringToLowerCamelCase(str));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

adityaKumar
calculateAge()
helloNinjasHowAreYou

Upper Camel Case Implementation

Let us implement UpperCamelCase in Java. Consider the following input:

Input: Hello Ninjas

Expected Output: HelloNinjas

public class UpperCamelCase {
    static String convertStringToUpperCamelCase(String str) {
        // to keep track of spaces
        // Checking Spaces
        int checkSpace = 0;
        // Length of the string
        int len = str.length();
        // String to character array conversion
        char characterArr[] = str.toCharArray();
        // Checking index for characterArr
        int cIndex = 0;
        // Traversing each character of the characterArr
        for (int i = 0; i < len; i++) {
            // The first letter should be small
            if (i == 0)
                characterArr[i] = Character.toUpperCase(characterArr[i]);
            // Check empty spaces
            if (characterArr[i] == ' ') {
                // If space found then
                checkSpace++;
                // Convert the letter to UpperCase after a space
                characterArr[i + 1] = Character.toUpperCase(characterArr[i + 1]);
                continue;
            } else
                characterArr[cIndex++] = characterArr[i];
        }
        // Return string
        return String.valueOf(characterArr, 0, len - checkSpace);
    }

    public static void main(String args[]) {
        String name = "aditya kumar";
        System.out.println(convertStringToUpperCamelCase(name));
        String ageCalculation = "calculate age()";
        System.out.println(convertStringToUpperCamelCase(ageCalculation));
        String str = "Hello ninjas how are you";
        System.out.println(convertStringToUpperCamelCase(str));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

AdityaKumar
CalculateAge()
HelloNinjasHowAreYou

Now let us understand the difference between the snake case and the camel case.

Camel Case Vs Snake Case

In the camel case, we need to make the second word’s first letter capitalized or small without any space. But in the snake case, the name begins with a little letter, as like in the camel case. If the name consists of more than one word, the last word will begin with a small letter, and the words will be separated by an underscore (_). The snake case is also known as the underscore case. This is also the most used naming convention in Java programming. For example, hello_ninjas, age_calculation(), etc.

Let us discuss the conversion of the camel case into the snake case.

Camel Case to Snake Case Implementation

Here is the implementation of the conversion of camel case to snake case.

Implementation

class CamelToSnakeCase {

    public static String camelToSnakeConversion(String str) {

        String answer = "";
        char firstChar = str.charAt(0);
        answer = answer + Character.toLowerCase(firstChar);

        for (int i = 1; i < str.length(); i++) {

            char isCharacter = str.charAt(i);

            if (Character.isUpperCase(isCharacter)) {
                answer = answer + '_';
                answer = answer + Character.toLowerCase(isCharacter);
            }

            else {
                answer = answer + isCharacter;
            }
        }

        return answer;
    }

    public static void main(String args[]) {
        String string = "HelloNinjas";

        System.out.print(camelToSnakeConversion(string));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

hello_ninjas

Snake Case to Camel Case Implementation

Here is the implementation of the conversion of snake case to camel case.

Implementation

import java.io.*;

class SnakeToCamelCase {

    public static String snakeToCamelConversion(String string) {
        string = string.substring(0, 1).toUpperCase() + string.substring(1);
        StringBuilder stringBuilder = new StringBuilder(string);

        for (int i = 0; i < stringBuilder.length(); i++) {

            if (stringBuilder.charAt(i) == '_') {

                stringBuilder.deleteCharAt(i);
                stringBuilder.replace(i, i + 1, String.valueOf(Character.toUpperCase(stringBuilder.charAt(i))));
            }
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {

        String string = "hello_ninjas";
        System.out.println(snakeToCamelConversion(string));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

HelloNinjas

Must Read Type Conversion in Java

Frequently Asked Questions

Does Java use CamelCase?

Yes, Java uses the camel case. It is used as a naming convention for variables, methods, and classes. It is a common convention in many programming languages and involves writing the first word in lowercase and subsequent words in uppercase, with no spaces or underscores between the words.

Should constants use camel case in Java?

No, constants in Java use UPPER_SNAKE_CASE, not camel case.

What are the advantages of using camel case in Java programming?

Camel case in Java programming enhances readability, reduces ambiguity, and adheres to Java’s standard coding practices, fostering collaboration among developers.

What are camel case and Pascal case in Java?

Camel case and Pascal case are naming conventions used in Java. Camel case involves writing the first word in lowercase and subsequent words in uppercase, while Pascal case involves writing all words in uppercase. Both conventions are used to improve readability and understanding of code.

Conclusion

In conclusion, the camel case is a widely used naming convention in Java that enhances code readability and organization. By combining words with uppercase or lowercase distinctions, it simplifies the naming of variables, classes, methods, and interfaces. Understanding and implementing camel case effectively is essential for adhering to Java programming standards. 

In this article, we have discussed the camel case in Java. We have also discussed how we can use camel case. We have discussed the snake case and how it differs from the camel case. We have also discussed the implementation of the camel case and snake case. If you want to explore more about naming conventions and cases in programming, then you can check out our other articles.

Live masterclass