×

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter, such as maxMarks(), lastName, or ClassTest, and all whitespace will be removed.

Camel case can be used in two different ways:

  1. Use lowercase camel case, when the first letter of the initial word is lowercase. When naming the methods as well as variables, this convention is typically followed. For examples, FirstName, LastName, ActionEvent, printArray(), etc.
  2. The title case, also known as upper camel case, is used when the first letter of the beginning word is capitalized. This naming style is typically used for classes and interfaces. For examples, Employee, printable, etc.

Changing a Standard String to a Camel Case

Simply deleting the spaces from a string will transform it to either the lowercase or uppercase camel case style.

Example of a Lower Camel Case

Input: Learn the programming languages

Output: learnTheProgrammingLanguages

Example of an Upper Camel Case

Input: learn java language

Output: LearnJavaLanguage

Steps

Step 1: The character array is traversed character by character until the end using the following algorithm.

Step 2: When lower camel case comes next, the initial letter of a string at index = 0 is changed to lower case, otherwise, it is converted to upper case 

Step 3: When there are any spaces in the array, the letter that follows the space gets capitalized.

Step 4: If the non-space characters appear, it is copied to the array that results.

Time complexity

Time complexity: O(n)

Since all operations need one traversing of the string, the total time needed by the approach is linear.

Auxiliary Space:

Auxiliary Space: O(1)

Since no additional array is utilized, the algorithm's space requirements are constant.

Programs on Camel Case in Java

Lower Camel Case Conversion of String

public class LowerCamel
{ 
    static String convertString( String s )  
    { 
        int ctr = 0 ;
        int n = s.length( ) ;  
        char ch[ ] = s.toCharArray( ) ;  
        int c = 0 ;
        for ( inti = 0; i< n; i++ )  
        {  
if( i == 0 ) 
ch[i ] = Character.toLowerCase( ch[ i ] ) ;  
            if ( ch[ i ] == ' ' )  
            { 
ctr++ ;
ch[i + 1 ] = Character.toUpperCase( ch[ i + 1 ] ) ; 
continue ;
            }  
            else  
ch[c++ ] = ch[ i ] ;  
        } 
        return String.valueOf( ch, 0, n - ctr ) ;  
    }  
    public static void main( Stringargs[ ] )  
    {  
        String str = "Hello People" ;
System.out.println( convertString( str ) ) ; 
        String str1 = "Learn" ;
System.out.println( convertString( str1 ) ) ; 
        String str2 = " The best content from the javatpointtutourial" ;
System.out.println( convertString( str2 ) ) ;  
    }  
}  

Output

helloPeople
learn
TheBestContentFromTheJavatpointTutourial

Upper Camel Case Conversion for String

public class UpperCamel
{  
    static String convertString( String s )  
    { 
        int ctr = 0 ;
        int n = s.length( ) ; 
        char ch[ ] = s.toCharArray( ) ;  
        int c = 0 ;
        for ( inti = 0; i< n; i++ )  
        { 
if( i == 0 )  
ch[i ] = Character.toUpperCase( ch[ i ] ) ;  
            if ( ch[ i ] == ' ' )  
            {  
ctr++ ;
ch[i + 1 ] = Character.toUpperCase( ch[ i + 1] ) ;  
continue ;
            } 
            else  
ch[c++ ] = ch[ i ] ;  
        }  
        return String.valueOf( ch, 0, n - ctr ) ;  
    }  
    public static void main( Stringargs[ ] )  
    {  
        String str = "programming language" ;
System.out.println( convertString( str ) ) ;  
        String str1 = "java t point" ;
System.out.println( convertString( str1 ) ) ;   
        String str2 = "i love java language" ;
System.out.println(convertString( str2 ) ) ;  
    }  
}   

Output

ProgrammingLanguage
JavaTPoint
ILoveJavaLanguage

Example

public class UpperCamel
{  
    static String convertString( String s )  
    { 
        int ctr = 0 ;
        int n = s.length( ) ; 
        char ch[ ] = s.toCharArray( ) ;  
        int c = 0 ;
        for ( inti = 0; i< n; i++ )  
        { 
if( i == 0 )  
ch[i ] = Character.toUpperCase( ch[ i ] ) ;  
            if ( ch[ i ] == ' ' )  
            {  
ctr++ ;
ch[i + 1 ] = Character.toUpperCase( ch[ i + 1] ) ;  
continue ;
            } 
            else  
ch[c++ ] = ch[ i ] ;  
        }  
        return String.valueOf( ch, 0, n - ctr ) ;  
    }  
    public static void main( Stringargs[ ] )  
    {  
        String str = " programming language" ;
System.out.println( convertString( str ) ) ;  


    }  
}

Output

ProgrammingLanguage

Related Topics

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.