GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaInterviewQuestionsAndAnswers/docs/object-methods.md at master · Nishith1/JavaInterviewQuestionsAndAnswers

Expand file tree

/

object-methods.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
78 lines (62 loc) · 1.5 KB
/

object-methods.md

Copy path

File metadata and controls

78 lines (62 loc) · 1.5 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

/com/in28minutes/java/object/EqualsHashCodeExamples.java

package com.in28minutes.java.object;

class Client {
	private int id;

	public Client(int id) {
		this.id = id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (id != other.id)
			return false;
		return true;
	}

}

public class EqualsHashCodeExamples {

	public static void main(String[] args) {

		// == comparison operator checks if the object references are pointing
		// to the same object. It does NOT look at the content of the object.
		Client client1 = new Client(25);
		Client client2 = new Client(25);
		Client client3 = client1;

		System.out.println(client1.equals(client2));// true
		System.out.println(client1.equals(client3));// true

	}
}

/com/in28minutes/java/object/ToStringExamples.java

package com.in28minutes.java.object;

class Animal {

	public Animal(String name, String type) {
		this.name = name;
		this.type = type;
	}

	String name;
	String type;

	public String toString() {
		return "Animal [name=" + name + ", type=" + type + "]";
	}

}

public class ToStringExamples {
	public static void main(String[] args) {
		Animal animal = new Animal("Tommy", "Dog");
		System.out.println(animal);// Animal [name=Tommy, type=Dog]
	}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.