Algorithm
- Traverse the character array(characterArr), character by character.
- The initial letter of the string at index = 0 is either converted to lowerCase (when following lowerCamelCase) or UpperCase (when following UpperCamelCase).
- The array is checked for blank spaces, and the letter immediately following the space will going to convert to the UpperCase.
- 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 CodeOutput
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 CodeOutput
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 CodeOutput
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 CodeOutput
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.