Soluții

How to Compare Two Objects in Java

Java Object class is the super class of all the Java classes. All Java classes implements the Object class by default. The Java Object class provides the two important methods to compare two obiects in java, i.e. equals() and hashCode() method. In this section, we will learn how equals() and hashCode() method works. Along with this, we will also learn how to compare two objects in Java with proper examples.

Java provides the two methods of the Object class to compare the objects are as follows:

  • Java equals() Method
  • Java hashCode() Method
Java equals() Method

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address.

Syntax:

  1. public boolean equals(Object obj)

The method parses a reference object as a parameter. It returns true if the objects are equal, else returns false.

It is also possible that an object is equal to another given object, then the equals() method follow the equivalence relation to compare the objects.

    • Reflexive: If x is a non-null reference, the calling of x.equals(x) must return true.
    • Symmetric: If the two non-null references are x and y, x.equals(y) will return true if and only if y.equals(x) return true.
    • Transitive: If the three non-null references are x, y, and z, x.equals(z) will also return true if x.equals(y) and y.equals(z) both returns true.
    • Consistent: If the two non-null references are x and y, the multiple calling of x.equals(y) constantly returns either true or false. It does not provide any information used in the comparison.
    • For any non-null reference x, x.equals(null) returns false.

In short, for any non-null reference say x and y, it returns true if and only if both references refer to the same object.Remember: When we override the equals() method, it is necessary to override the hashCode() method. Overriding follow the convention for the hashCode() method that states, the equal object must have equal hash code.

Example of equals() method

In the following example, we have created constructor of the Double and Long class and passes corresponding values, as an argument that stored in their objects, respectively.

After that, in the first println statement, we have invoked equals() method and parse an object y as a parameter that compares the object x and y. It returns false because x holds the double value and y holds the long value that is not equal.

Similarly, in the second println statement, we have invoked equals() method and parse the same value as in the constructor of the Double class. It returns true because the object of double class i.e. x holds the same value as we have passed in the equals() method.

[mai mult...]

How to Create Object in Java

The object is a basic building block of an OOPS language. In Java, we cannot execute any program without creating an object. There is various way to create an obiect in java that we will discuss in this section, and also learn how to create an object in Java.

java provides five ways to create an object.

  • Using new Keyword
  • Using clone() method
  • Using newInstance() method of the Class class
  • Using newInstance() method of the Constructor class
  • Using Deserialization
Using new Keyword

Using the new keyword is the most popular way to create an object or instance of the class. When we create an instance of the class by using the new keyword, it allocates memory (heap) for the newly created object and also returns the reference of that object to that memory. The new keyword is also used to create an array. The syntax for creating an object is:

  • Using new Keyword
  • Using clone() method
  • Using newInstance() method of the Class class
  • Using newInstance() method of the Constructor class
  • Using Deserialization
[mai mult...]

How to Print ASCII Value in Java

ASCII acronym for American Standard Code for Information Interchange. It is a 7-bit character set contains 128 (0 to 127) characters. It represents the numerical value of a character. For example, the ASCII value of A is 65.

In this section, we will learn how to print ASCII value or code through a Java program.

There are two ways to print ASCII value in JAVA

  • Assigning a Variable to the int Variable
  • Using Type-Casting
Assigning a Variable to the int Variable

To print the ASCII value of a character, we need not use any method or class. Java internally converts the character value to an ASCII value. In the following program, we have assigned two characters a and b in the ch1 and ch2 variables, respectively. To find the ASCII value of a and b, we have assigned ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. Finally, we have printed the variable asciivalue1 and asciivalue2 in which ASCII values of the characters are stored.

[mai mult...]