Tag Archivio per: Compare

Git is the most popular version control system. Many developers and teams use Git for their activities. One common practice when working with Git is to create branches that help create a separate working environment. With branches, you can mess around with things without affecting the other sections of the code, and at long last, you can compare your branches and then merge them. The question is, “how do you compare two branches using Git?”

Comparing Branches in Git

Git offers the Git diff command, which lets you easily compare branches. You can compare the files in the branches and their commits. Let’s see the various ways of comparing branches.

1. Comparing Two Branches

Creating a new branch and working on it is safe when you’ve cloned a project. That way, you separate it from the main branch without messing up things. Before you merge the two branches, you should compare them to see the differences.

The syntax for comparing branches is:

$ git diff branch0..branch1

In the syntax above, you are trying to check what files or information are available in branch1 but not in branch0. Ideally, the double-dot will compare the branches’ tips or HEAD. If solving a conflict between the branch, the double-dot will give more details about the branches.

In the image above, we are comparing two branches: linuxhint e master. We can see all the commits in the master branch that are not in the linuxhint branch.

2. Comparing Files in Branches

It’s common to have specific files sharing the same name but in different branches. It could be some code or a file and you want to check the difference between the two versions of the files in different branches. In that case, use the syntax below.

$ git diff branch0..branch1 filename

With the above syntax, we are comparing the given file based on the HEAD of the two branches to outline the difference in the versions.

For instance, in the image below, we can see the difference in the file named one.txt in the file’s content. The words in the two files are not the same. This command could come in handy if you compare code that had a conflict or your version of the code with another.

3. Comparing Commits

Different branches have different commits, when working on the same version of a project in a separate branch, it makes sense that you would want to check the difference in the commit of two branches. Here, you will use the git log command with the syntax below.

$ git log branch0..branch1

In the example below, we can see the different commits for each branch, the date of the commit, the author, and the commit message. You can also note the commit ID. Comparing commits between branches is a great way of analyzing a merge conflict.

4. Using Triple-dots

Most comparisons involve the HEAD, which is why you will often use the double-dots. However, if you need to compare one branch with the ancestor of both branches, you use the triple-dots.

In the syntax below, we are comparing branch1 with the common ancestor of branch0 and branch1.

$ git diff branch0…branch1


Git is an excellent and easy-to-use version control system. It offers short commands that achieve great functionality, when working on a project, it’s recommended to create a branch to act as the safe zone for trying things without messing with the flamante code. This guide covered the various ways of comparing branches on Git to see their difference in commits, HEAD, and files.

Source link

Java is a collection of packages, classes, and objects. One of the Java classes is a wrapper called Character, which belongs to the Java.lang package. The object of the Character class holds/wraps a particular value, “char”. In Java, characters or chars are often compared to check their differences or similarities.

This tutorial will guide you on how to compare chars in Java.

How to Compare chars in Java?

To compare chars in Java, you can use:

    • compare() method
    • equals() method
    • Relational operators

Let’s check out each of them one by one.

How to Compare chars by Using compare() Method?

The “compare()” method of the Java “Character” class numerically compares two characters. This method is used to compare the primitive chars and the Character objects. It takes two characters as parameters to compare and returns a numeric value.


The syntax of compare() method is given as:

If both chars “x” e “y” are the same, the compare() method will return “0”. If the first char is less than the second char, it will return a negative value. Similarly, the specified method will return a positive value when the first char is greater than the second char.

Example 1: Compare Primitive chars by Using compare() Method

Let’s compare two primitive characters, “f” e “b”, by using the compare() method. These two characters are stored in “ch1” e “ch2”:

char ch1 = ‘f’;
char ch2 = ‘b’;

Now, we will use the “compare()” method to compare these two characters with conditional statements:

if(Character.compare(ch1, ch2) > 0) {
    System.out.println(«character 1 ‘f’ is greater than character 2 ‘b'»);}
 else if(Character.compare(ch1, ch2) < 0) {
    System.out.println(«character 1 ‘f’ is less than character 2 ‘b'»);}
    System.out.println(«Both characters are equal»);


The output shows that the “ch1” is numerically greater than “ch2”:

Example 2: Compare Character Objects by Using compare() Method

In a Java program, Character objects can also be compared by using the “compare()” method. First, we will create two Character objects “chr1” e “chr2” with “c” e “v” values respectively:

Character chr1 = ‘c’;
Character chr2 = ‘v’;

Now, we call the “compare()” method of the Character class to compare these two character objects using the given conditions:

if(Character.compare(chr1, chr2) > 0) {
    System.out.println(«c is greater than v»);}
else if(Character.compare(chr1, chr2) < 0) {
    System.out.println(«c is less than v»);}
    System.out.println(«Both characters are equal»);



Let’s head towards the second method!

How to Compare chars by Using equals() Method?

The “equals()” method also belongs to the “Character” class that can be used to compare the Character objects. It simply compares the values and checks whether these characters are equal or not based on their case.


The syntax of the equals() method is as follows:

The “equals()” method takes a character object as a parameter. If the values of the “obj1” e “obj2” are equal, the equals() method returns true; otherwise, it returns false.


In this example, we will compare two Character objects, “k” e “K”, by creating two objects, “ch1” e “ch2” of Character class:

Character ch1 = ‘k’;
Character ch2 = ‘K’;

If the value of ch1 is equal to ch2, the equals() method will return true; otherwise it will return false.:

    System.out.println(«Both characters are equal»);
    System.out.println(«Both characters are not equal»);


As we know, Java is a case-sensitive language, so the equals() method will return false because “k” e “K” are not equal:

How to Compare chars by Using Relational Operators?

There is one more approach for comparing both primitive characters and Character objects, which is using relational operators such as “==”, “<”, and “>”. These operators can be added in the condition based on the requirements.

The following example will compare two Character objects, “ch1” e “ch2,” using the greater than “>” and less than “<” relational operators.


First, we will create two Character objects, “ch1” e “ch2,” with the following values:

Character ch1 = ‘n’;
Character ch2 = ‘m’;

Now, we will use relational operators to compare and check if the ch1 object is greater than or less than the other object:

 if(ch1<ch2) {
      System.out.println(«ch1 ‘n’ is less than ch2 ‘m’ «);
 }else if (ch1>ch2) {
      System.out.println(«ch1 is ‘n’ greater than ch2 ‘m’ «);
   System.out.println(«Both characters are equal»);



We have provided all the necessary instructions to compare chars in Java.


To compare Character objects in Java, you can use compare() method, and to compare both primitive chars and Character objects, utilize the equals() method and relational operators. Java compares primitive values numerically, whereas the Character objects are compared based on their case or values, depending upon the method you utilize. In this tutorial, we thoroughly discussed methods to compare chars in Java.

Source link

“A string is a data variable that contains several characters to form a collective string. The variable string is declared as an object of the string and used to inhabit several features used in the string class. This tutorial will highlight the comparison methods in C sharp programming language.”

Example 1

In this example, we will use a built-in function of C sharp to compare two strings. As string class in C sharp is responsible for applying different features. So “string.Equals” is one of the functions in C sharp that is used to compare strings in the .Net framework. To elaborate on the concept, we have used the program’s source code. We have taken three strings. These three strings will be compared with one another by using the string.Equals function.

This function takes two parameters simultaneously; not all three strings will be compared at merienda. Whereas the return type of this function is a boolean. It returns either true or false values depending on the condition applied in the program.

# String.Equals(string1 string2);

As it is a comparison method, we will use an if-else statement to display the resultant values. If both the first two strings are the same, then the function will return True, so the first statement will be displayed that both strings are the same; otherwise, in the case of a False return, it will be displayed that both are different values.

# If(String.Equals (mystring1, mystring2))

# Console.Writeline($»{mystring1} and mystring2} are same»);

Both the values inside the variables will be displayed in the resultant statement.

A similar if-else statement will be applied for the other two comparisons between string 1 and string 3 and the comparison between string 2 and string 3. As input strings are taken so that the first two strings are the same while the third one is different, the results will be seen on execution.

To execute any source code in C sharp, we need to use a compiler to compile the code for the Ubuntu terminal. So here, we have used the MCS compiler. Afterward, the code is compiled; there is a need to execute the code in the file with the .exe extension. Macaco is recommended for this purpose.

$ Mcs file.cs

$ Macaco file.exe

Example 2

The second method used for string comparison is a string.Compare() method. String.Compare is also part of the String class in C sharp. This function also takes two strings as a parameter.

# String.Compare(string1, string2);

Irrespective of the first example, this function contains the return type as an integer value. Both the strings are compared. In case the first string is lexicographically less as compared to the second one, then the return value is less than zero. And if the second string is smaller in terms lexicographically, the value is greater than zero. And it returns zero if both are similar.

Hint: Lexicographical is a term of the order. Lexicographic order is a generalized form of alphabetical order. In other words, it is said to be a dictionary of a sequence of order symbols.

Three strings and one integer type data variable are declared. The integer data variable is used to store the resultant value in it. The string values are taken so that the first two strings are the same, while the third one is different.

# Result = string.Compare(str1 , str2);

# Console.writeline (result);

The same function is applied to all comparisons between these three strings.

From the results, you will see that all three different values are obtained. The first one is zero, which means both strings are the same; the second result is -1, which means that the second string is greater than the first, whereas 1 shows that the second string is smaller than the first one.

Example 3

The third method in use is the “CompareTo()” method. This function is a direct comparison method because it takes a single string in the parameter. This function is called through the object of string, and to whom it is compared is written inside the parameter of the function.

# String1. CompareTo(String2);

In this illustration, we have taken two different strings. We use an if-else statement to compare the returned value of two strings. This function also returns the integer values. And the criteria for the return data are the same as described for the string.compare() method. When the program is executed, both the strings will be compared with each other. When the compiler goes towards the if statement, according to that, if the result of the comparison is zero, then display statement as both the strings are different so it will not true, the result is minus 1, so according to this situation, the else part will be executed, and the statement will be displayed to console.

On the execution, you can see that it is stated that red is smaller than yellow.

Example 4

This example includes a comparison by character by character. This is also known as a custom comparison as we declare this function by ourselves; this is not a built-in function of a string. At a time, this also comprises a comparison of the two strings. First, we declare a method that is static outside the main function to compare two strings.

Now move towards the code to elaborate the working of this phenomenon in C sharp. A function that is declared for the comparison purpose will take two strings as an argument. Inside the function, a long integer type variable is declared to store the minimum of both string lengths. This is done through the built-in mathematics function Math.min().

# int len = Math.Min(myString1.Length, myString2.Length);

After that, we will use the “For” loop to iterate through the whole string character by character comparison. The loop will iterate till the last character of both the strings is compared. We have utilized the if statement to check two strings and compare them with each other.

# If (Mystring1[index] < mystring2[index])

And similarly, if that character of the first string is greater than the second string’s first character, then return 1, and zero is returned if both are equal.

In the end, the lengths of both strings are compared. If the second string is greater in length than the first one, then it returns 1; on the other hand, -1 is returned.

In the main program, two strings are declared. A function call is made, and the returned value is stored in the variable.


Comparison of strings is made through different approaches in C sharp programming language. We have implemented all the approaches in the Linux operating system. All methods are built-in functions of Strings, except the one that includes the user-defined function manually to compare two strings. String.compare(), String.equal(), and compareTo() are the basic functions of strings that assist in comparing two strings with each other by using strings as arguments. This tutorial will provide you with all the possible methodologies by using some elementary examples.

Source link