The object class in Java

Source: Internet
Author: User

1. What is the Object class? What are the common methods?

Answer:1). The Object class is the parent class of all classes, in the java.lang package.

2). arrays are also Object the subclass of the class.

3). common methods of the Object class are:

---toString ();

---equals ();

---hashcode ();

---.....

2. How do I use the Object class? Examples show

Answer: The Object class , under the Java.lang package, is the root of all classes. Objects of any class can call Methods in the object class, including array objects.

Example:public class example{

public void f (Object obj) {

}

}

public class test{

public static void Main () {

Example exam = new Example ();

int[] array = new INT[4];

...// any array

EXAM.F (array);

}

}

What is the difference between 3.Object and object[] ?

Answer:1). the parameters in the method are Object type, any type of parameter can be passed in to execute.

2). the parameter in the method is object[] type, only an array of objects can be passed in to execute.

Example:public static void Arraytest (object[] obj) {

}

public static void Main () {

int[] array = new INT[4];

Arraytest (array)//error occurred

}

4. What is the toString method? Examples show

A :the toString method can convert any object to a string return, and the resulting algorithm for the return value is:getclass (). GetName () + ' @ ' + Integer. Tohexstring (Hashcode ()).

Example 1): Calling the ToString method of a class directly when printing an object

public static void Main () {

Student stu1 = new Student ();

Stu1.setname ("Zhang San");

Stu1.setage (23);

System.out.println (STU1);

System.out.println (STU1);

}

print : [email protected];

Example 2). class itself overrides toString method Overrides Student Class of toString method After

public static void Main () {

Student stu1 = new Student ();

Stu1.setname ("Zhang San");

Stu1.setage (23);

System.out.println (STU1);

}

Print: Student:name = Zhang San,age= all;

Example 3). public class student{

Private String name = "123"; Student Name

private int age; Age

Public String GetName () {

Return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

Return age;

}

public void Setage (int.) {

This.age = age;

}

Public String toString () {

Return "Student:" + "name=" + name + ", age=" + age;

}

}

5. How do I use The Equals method and "= ="? Examples show

Answer:1). Equals method

the virtual address of the object is compared, but can be overridden in the class.

Example: The string class overrides the two string object of the same value compared to a more

True

String str = new String ("123");

String str = new String ("123");

System.out.println (Str.equals (str1)); Print to true

2). "="

The virtual address in memory is compared

String str = new String ("123");

String str = new String ("123");

System.out.println (str = = STR1); Print to false

6. What is the hashcode method ?

A: Gets the hash code value of the object, which is a binary

What is the relationship between the 7.equals method and the hashcode method?

Answer:1). if two objects use a equals Compare back true, then their hashcode must be the same value

2). If two objects equals Compare back false, then their hashcode values are not necessarily different

   3). overlay equals hashcode, You can use the eclipse auto-generated, guaranteed equals return true, hashcode equals return false, hashcode different

4). in the Set The collection part has practical application

The object class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.