The Object class is the ancestor of every class. It has been inherited. It has the following methods.
--ʻEquals () : Check if an instance is the same as your own self. --
toString ()`: Returns a string representation of its own content.
By having an Object class, you can use polymorphism to create "methods that can pass instances because they can be of any type", and you can define "methods that all classes want to have at least".
Something like System.out.println
can accept an object as an argument.
So
Sub.java
public class Sub{
//Assuming that something is written
}
Main.java
public class Main
public static void main(String[] args){
Sub s = new Sub();
System.out.println(s); // (1)
System.out.println(s.toString()); // (2)
}
The output results of (1)
and (2)
are the same.
Execution result.
test.Sub@5c8da962
test.Sub@5c8da962
Override and use. In the previous example,
Sub.java
public class Sub {
String name = "Honda Mio";
public String toString(){
return this.name;
}
}
Main.java
public class Main
public static void main(String[] args){
Sub s = new Sub();
System.out.println(s); // (1)
System.out.println(s.toString()); // (2)
}
Execution result.
Honda Mio// (1)
Honda Mio// (2)
What kind of program is this? (Too rough)
The toString
method itself of the String
class is
String.java
public class toString(){
return this;
}
This seems to be the only one. I pressed F3 in Eclipse and read it, but it was on line 2806.
It may be used for equivalence judgment and equality judgment.
-** Equal value : The referenced address is the same. Determined by the ==
operator.
- Equivalent **: The referenced addresses are different, but the values are the same. Determined by the ʻequals` method.
Rough recognition. The plain ʻequals` method itself is
String.java
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
(It was on line 1002) If you take a closer look at this now, it's night and it looks like you'll stay up all night. But, very roughly speaking, it means that they are actually doing equality judgment. So, when you actually use it, decide ** what is equivalent **.
Here, if the name
field is the same, Yoshi! So ...
Sub.java
public class Sub {
String name = "Honda Mio";
public boolean equals(Object o){
//If they are equal(Definitely equivalent)
if (this == o) {
return true;
}
//If they are equivalent
if (o instanceof Sub) {
Sub s = (Sub) o;
if (this.name == s.name) {
return true;
}
}
//If neither equal nor equivalent
return false;
}
}
Main.java
public class Main {
public static void main(String[] args) {
//Create 3 instances
Sub s1 = new Sub();
Sub s2 = new Sub();
Sub s3 = new Sub();
//Enter a value in each name field
s1.name = "Honda Mio";
s2.name = "Honda Mio";
s3.name = "Keisuke Honda";
//Output the result of equivalence judgment
System.out.pritln( s1.equals(s2) );
System.out.pritln( s1.equals(s3) );
}
}
Output result.
true
false
It's a really crude program Well, it's a reminder of how to use it, so anything is fine ...
[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%8F%E3%81%8B%E3%82%8BJava%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E4%B8%AD%E5%B1%B1-%E6%B8%85%E5%96%AC/dp/484433638X) Pp.541-551
Recommended Posts