Home » Camel case in Java

Camel case in Java

by Online Tutorials Library

Camel case in Java

Java follows the camel-case syntax for naming the classes, interfaces, methods, and variables. If the name is combined with two words, the second word will start with uppercase letter always, such as maxMarks( ), lastName, ClassTest, removing all the whitespaces.

There are two ways of using Camel case:

  1. Lower camel case where the first character of the first word is in lowercase. This convention is usually followed while naming the methods and variables. Example, firstName, lastName, actionEvent, printArray( ), etc.
  2. The Upper camel case also known as the title case, where the first character of the first word is in uppercase. This convention is usually followed while naming the classes and interfaces. For example, Employee, Printable, etc.

Converting a normal string into camel case

A string can be converted into either the lower or upper camel case convention just by removing the spaces from the string.

Lower Camel Case Example:

Input: TutorAspire is the best tutorial site for programming languages.

Output: tutorAspireIsTheBestTutorialSiteForProgrammingLanguages.

Upper Camel Case Example:

Input: this is the java tutorial

Output: ThisIsTheJavaTutorial

Algorithm:

  1. Traverse the character array character by character till it reaches the end.
  2. The first letter of the string at index = 0 is either converted to lower case (when following lower camel case) or to upper case (when following upper camel case).
  3. The array is checked for spaces, and the letter immediately following the space is converted to the upper case.
  4. If the non-space character is encountered, it is copied to the resulting array.

Let’s implement the algorithm in a Java program.

A. Converting String to Lower Camel Case

LowerCamel.java

Output:

maxMarks()  lastName  tutorAspireIsTheBestTutorialSiteForProgrammingLanguages.  

B. Converting String to Upper Camel Case

UpperCamel.java

Output:

ClassTest  Employee  ThisIsTheJavaTutorial  

You may also like