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

Permutation in Java Explained [With Coding Examples]

Updated on 23 November, 2022

7.05K+ views
7 min read

JAVA was developed by James Gosling at Sun Microsystems in the year 1995. Using JAVA, developers can create stable, secure, and resilient applications that are capable of running on almost any platform — including Linux, Windows, and macOS based operating systems.

The Write Once, Run Always property of JAVA (also known as WORA) also saves the need of recompiling code that has been compiled satisfactorily once already. This versatility of JAVA makes it an ideal language to integrate in-built complex logical and mathematical functionality into almost all types of cross-platform applications. 

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

The advantages of using JAVA to develop web-apps, as opposed to any other alternative (or popular) web development environment, extend to three main pillars of sustained application benefits, namely:

  1. JAVA syntax is easy to learn and easy to adapt to, and suitable for coders at every level of pace and complexity. This makes JAVA a safe bet for development teams that are subject to frequent personnel changes. The time taken for a new resource to align themselves to the common development platform (JAVA), is one of the least in the industry.
  2. JAVA is a high-level, object oriented programming language, which makes it completely modular in approach, and gives developers the ability to create functionally reusable chunks of code, thereby reducing effort. A vast library of in-built functions also reduces dependencies on other tertiary plugins or compiler add-ons, scripts, etc – which in turn gives JAVA one of the least implementation times.
  3. JAVA blends the best of all worlds by being platform-independent as discussed above. A web application that is developed in JAVA can be easily moved from one operating system to another with zero loss in functionality, and that’s a priceless utility to possess in any development environment.

Check out upGrad’s Advanced Certification in Cyber Security

As one of the aspects that makes JAVA so lucrative as a development platform for the web, its minimal implementation dependencies and mathematically sound syntax can be generously exploited to perform almost any mathematical or logical instruction by simply invoking an in-built function, with minimum additional customisation.

One of the brightest examples of this flexibility in code, is the ability to perform permutation in JAVA — more specifically, to truly randomise and find all possible permutations of all the characters available in a particular string.

In this guided tutorial, we will learn how to attempt to perform this permutation in JAVA by storing the first character of the string in a “tracked” location of the string, and invoking the generatePermutation() function to randomize different variations with all the other characters in the string.

Check out upGrad’s Advanced Certification in Data Science

Read: Java Interview Questions & Answers

Permutation in Java — the Concept of the Backtracking Algorithm

To find a solution to this problem of permutation in JAVA, we must first familiarise ourselves with a concept that has become widely accepted within the web development community, as the backtracking algorithm.

The backtracking algorithm is a recursive process that depends on periodically fixing one character of a given string and combining it with all possible permutations of all the other characters, and repeating this process with characters in every position of the string individually, to arrive at the maximum (and all possible) number of variations for that string.

This algorithm of permutation in JAVA is easier to understand in the context of a given string with three characters. Let’s say these three characters in a given string are A, B, and C.

The first step to perform this permutation in JAVA is to begin by fixing one character (we will always start by fixing the character available in the first position), and then performing a swap with all the remaining characters, by combining them with the original (fixed) character.

For example, in the ABC string example, we will perform our first three string iterations by fixing the character A (which is the first character in the ABC string), and then perform the swaps by replacing A with A, B, and C respectively.

We then arrive at the following combinations: ABC, BAC, and CBA.

The next step is to similarly “fix” the character found in the next position of the string (in this case, the character found at the second position of the string, namely B) and swapping the remaining characters (including the character that was fixed in the previous iteration).

Eventually, we will keep repeating this step with each level of randomisation achieved, till we arrive at a complete set of all possible permutations:

ABC

ACB

BAC

BCA

CBA

CAB

In the sample code/desired output section available at the end of this article, we will turn this attempt to perform permutation in JAVA, into legible code and thereby see the permutations being calculated in real time.

Performing a Permutation in JAVA — the Recursive Algorithm to Backtrack and Swap

A succinct summary of the process to take a random string and perform a thorough permutation in JAVA, can be described with the following step by step recursive algorithm:

  1. String Definition – First, define a string. You can always choose to accept a random string as an input variable, but for clarity of purpose and effort, we will use the predefined string ABC in this example.
  2. Character Fixing – Fix one character (start with the character in the first position and continue to swap it out against all the remaining characters).
  3. generatePermutation() – Invoke and use the output from the generatePermutation() function, for all the remaining characters.
  4. Recursive Backtracking – Perform the “backtracking” algorithm, and keep recursively swapping characters till your code reaches the end of the string (In our current example, till C).

Performing a Permutation in JAVA — Sample Code:

public class PermuteString {  

    //defining a function to swap the characters at position i with all the characters at position j  

    public static String swapString(String a, int i, int j) {  

// character definitions  for fixed characters in string

      char[] b =a.toCharArray();  

        char ch;  

// performing the swap in the next step

        ch = b[i];  

        b[i] = b[j];  

        b[j] = ch;  

        return String.valueOf(b);  

    }    

    public static void main(String[] args)  

    {  

//defining the default string ABC

        String str = “ABC”;  

//counting the string length

        int len = str.length();  

//printing all randomizations

        System.out.println(“The following are all the possible permutations of the string ABC: “);  

        generatePermutation(str, 0, len);  

    }  

          //defining a function to generate all possible permutations of the string

    public static void generatePermutation(String str, int start, int end)  

    {  

        // printing all the permutations  

        if (start == end-1)  

            System.out.println(str);  

        else  

        {  

            for (int i = start; i < end; i++)  

            {  

                //fixing a character to swap the string  

                str = swapString(str,start,i);  

                //calling the generatePermutation()function recursively for all remaining characters                generatePermutation(str,start+1,end);  

                //performing backtracking algorithm to swap characters all over again  

                str = swapString(str,start,i);  

            }  

        }  

    }  

}  

Performing a Permutation in JAVA — Output Derived from the Sample Code:

When you compile and run this sample could, the desired output should look somewhat like this:

The following are all the possible permutations of the string ABC:

ABC

ACB

BAC

BCA

CBA

CAB

Conclusion

We hope you found this tutorial to perform a permutation in JAVA useful.  

If you’re interested to learn more about Java, OOPs & full-stack software development, check out upGrad & IIIT-B’s PG Diploma 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. When do you use permutation and combination?

Permutation and combination constitute the fundamental principles of counting. These are applicable in many cases when it comes to finding out the possible outcomes in different situations. While permutations indicate the arrangements in a specific order, combinations are indicative of the various possible selections or groupings. Permutation is applied when the sequence or order of arrangement is required. Combination is applied only when the count of possible groups is needed irrespective of the sequence. While permutation is applied to different kinds of objects, combination can only be applied to similar objects. For a particular set of values, the value of permutation is always greater than that of combination.

2. How is probability related to permutation?

Probability in mathematics is defined as the chance of an event happening. When we are calculating the probability, it is vital to ensure that the fraction is always in the simplest form. In probability theory and other areas of mathematics, permutations indicate sequences of results where order matters. The count of favorable outcomes divided by the count of total outcomes gives us the probability of an event happening. When we calculate the total count of outcomes, permutation is often used. This is because permutation is used to calculate the count of events where the sequence is important.

3. Is learning Java good for your career today?

According to hiring managers of top organizations all over the world, Java ranks third in the most sought-after programming languages. Java consistently remains the number one choice for enterprise application development even today. It is simple to understand and code and flexible and keeps evolving with the changing needs of the IT industry. So Java will always remain relevant in the years to come. And there will always be a high demand for experts in Java with the right professional experience and skills. Besides, there are different lucrative jobs that Java professionals can take up and launch a highly rewarding career path.

RELATED PROGRAMS