×

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

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 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.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

How to add 6 Months to the Current Date in Java?

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

3 minutes read.