Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Ways of Using Camel Case
1.1.
UpperCamelCase 
1.2.
LowerCamelCase
2.
Normal String to Camel Case
3.
Algorithm
3.1.
Lower Camel Case Implementation
3.2.
Upper Camel Case Implementation
4.
Camel Case Vs Snake Case
4.1.
Camel Case to Snake Case Implementation
4.1.1.
Implementation
4.1.2.
Output
4.2.
Snake Case to Camel Case Implementation
4.2.1.
Implementation
4.2.2.
Output
5.
Frequently Asked Questions
5.1.
Does Java use CamelCase?
5.2.
What are camel case and Pascal case in Java?
6.
Conclusion
Last Updated: Jun 11, 2024
Easy

Camel Case in Java

Author Aditya kumar
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

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, Duck Number in Java

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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));
    }
}


Output

output

Try it on Online Java Compiler.

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));
    }
}

Output

output

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));
    }
}

Output

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));
    }
}

Output

Output

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.

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 this article, we have discussed about camel case in Java. We have also discussed how we can use camel case. We have discussed about snake case and how it is different 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.

To learn more about Data Structures and Algorithms, you can enroll in our course on DSA in Java.

Previous article
What is J2ee?
Next article
Executor Framework in Java
Live masterclass