Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Char Array in Java | Java Character Array

Updated on 16 November, 2022

19.21K+ views
7 min read

Introduction

Most of the data we process today has characters in it, a programming language will be said flexible if it is familiar to a programmer and provides a way to handle character data.

Char arrays in java are very useful and easy to use, strings are immutable in java but we can manipulate the entries in the char array. Char arrays are faster when compared to lists and sets and even simple than sets and lists.

Let’s explore the working of char arrays now!

Check out our free courses to get an edge over the competition.

Declaration and Initialization

Declaration of a char array is similar to the declaration of a regular array in java. “char[] array_name” or “char array_name[]” are the syntaxes to be followed for declaration.

After declaration, the next thing we need to do is initialization. “array_name = new char[array_length]” is the syntax to be followed. Anyways we can do both declaration and initialization in a single by using “char array_name[] = new char[array_length]” syntax. The length of the array should be declared at the time of initialization in a char array.

Check out upGrad’s Advanced Certification in DevOps 

public class Test{
    public static void main(String args[]) 

    {

      //declaration-1

      char arr[];

      arr=new char[4];

      //declaration-2

      char arr2[]=new char[4];

      //assigning-1

      char arr1[]=new char[]{‘a‘,‘b‘,‘c‘,‘d‘};

      //assigning-2

      arr[0]=‘a‘;

      arr[1]=‘b‘;

      arr[2]=‘c‘;

      arr[3]=‘d‘;

      System.out.println(arr);

      System.out.println(arr1);

    } 

}

In the above snippet, declaration-1 and declaration-2 are both ways of declaration and initialization. And assigning-1 and assigning-2 are both ways of pushing entries to an array. Remember that when we print a char array it prints all the characters merged as a string, in the above case output will be “abcd”.

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

Check out upGrad’s Advanced Certification in Cyber Security

Learn more: Event Handling in Java: What is that and How Does it Work?

Looping over a char array

Looping over a char array is a fun task in java, we can use a regular “for loop” or a “for: each” loop for looping over the array.

//loop-1
for(int i=0;i<arr.length;i++)

   System.out.print(arr[i]);

//loop-2

for(char c:arr)

   System.out.print(c);

In the above snippet, loop-1 is a regular for loop and the final output of loop one will be “abcd”. And loop-2 is a for: each loop and the final output of loop-2 will be “abcd”.

Lenght of a character array can be attained using the length attribute. “Array_name.length” is the syntax to be followed.

char array[]=new char[]{‘h‘,‘e‘,‘l‘,‘l‘,‘o‘};
System.out.println(array.length);

The above snippet prints 5, as expected.

Enroll in Software Engineering Courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Sorting a Char Array

Arrays in java can be sorted using “Arrays.sort(array_name)”. This will sort the passed character array lexicographically.

char[] array = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘};
Arrays.sort(array);

System.out.println(Arrays.toString(array)); //line1

System.out.println(array);  //line2

Note that we need to import java.util.Arrays inorder to get Arrays.sort() and Arrays.toString() worked.

In the above snippet, the char array gets sorted lexicographically and printing the array prints the characters merged as a string. The output of “line2” will be “ehllo” if you don’t the output as a merged string instead you want to print the whole array with square brackets at the ends, then Arrays.toString() does the job. The output of “line1” will be [e, h, l, l, o].

In the previous code snippets, we’ve declared the char arrays by filling the entries in the array, but what if we want to convert a string to a char array?

Anyways we initialize a char array with string length, loop over the string, and append each character into the array will do the job.

String s=“hello“;
char[] arr=new char[s.length()];

for(int i=0;i<s.length();i++)

  arr[i]=s.charAt(i);

System.out.println(Arrays.toString(arr));

Above snippet will print [h, e, l, l,o] as expected.

The above snippet will do the art of converting a string to a char array, but do we need to write this long code every time we want to convert a string to a char array?. Well, the answer would be NO.

Java provides a beautiful method called toCharArray() which has to be applied on a string and returns the char array.

String s = “aejou“;
char[] arr = s.toCharArray();

System.out.println(Arrays.toString(arr));  //line1

arr[2]=‘i‘;

System.out.println(Arrays.toString(arr));  //line2

As expected, “line1” prints [a, e, j, o, u] as output. And as already discussed, unlike a string we can edit or manipulate the entries of a char array. So the output of the line2 will be [a, e, i, o, u].

Now that we’ve seen a method for converting a string to char array in java, what if we want a vice-versa task?

Never mind java also has a method for doing such tasks.

char[] arr = {‘h‘, ‘e‘, ‘y‘, ‘ ‘, ‘t‘, ‘h‘, ‘e‘, ‘r‘, ‘e‘};
String s1 = new String(arr); //method1

System.out.println(s1);

String s2 = String.valueOf(arr); //method2

System.out.println(s2);

In the above snippet, we can see both methods for converting a char array to a string. The output of the above snippet will be “hey there”, as expected.

Also Read: 17 Interesting Java Project Ideas & Topics For Beginners

Conclusion

We came to know what are char arrays, learned various ways of initializing and declaring char arrays. Walked through a snippet for looping over a char array. Sorted a char array using predefined Arrays.sort() method.

And observed a naive and predefined implementation of string to char array conversion, also observed a vice-versa implementation using various methods. Now that you are well versed with the concepts of char arrays, start implementing them!

If you’re interested to learn more about Java, OOPs & full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. Why do people use character arrays?

An array is a data structure in computer science that allows components of the same data type to be indexed and accessed using integer indexes. An array is a group of data elements, each of which is identified by an index. The position of the data item in the array is indicated by an index, which is an integer number. A character array is a collection of characters, each of which has a unique number assigned to it. A character array is similar to a string, but it can be any length, whereas a string is confined to a set amount of characters. They are simple to use. They can also store a lot of information.

2. Which is the most used loop in codes? Why?

The for loop is the most commonly used loop in programming. The for loop is a control flow statement in the C programming language that allows you to run a block of code a set number of times. The syntax for the for loop is for(initialization; condition; increment). The initialization statement is executed first, and it sets the value of the loop control variable to its default value. After that, the condition statement is checked, and if it is true, the code in the block is run. The increment line is then executed, incrementing the loop control variable by the desired amount. After that, the loop continues until the condition statement evaluates to false. When you need to cycle through a list of items or repeat a set of code a specific number of times, you'll want to use a for loop.

3. How do I choose the best course for me to learn?

The best course for you depends on your learning style, your objectives, and your budget. You should also think about the type of learning environment you prefer. Smaller classes appeal to certain people, but more engagement with the lecturer appeals to others. Some people like to study independently, while others want more guidance. You may start narrowing down your options once you've studied all of these aspects. To gain an understanding of what each course covers, start by glancing at the course descriptions online. Then you may compare the several institutions that offer the courses to find which one best suits your needs.

RELATED PROGRAMS