Skip to content

The chapter discuss the various ways of structuring an operating system.

Chapter 02

operating systems chapter 1

Arrays in java

favourite interview question- Hashcode and equals

Hi folks,

Today we are going to deal with interview strategy of hashcode() and equals() method.

By default, these methods are present in Object class of java.lang package.

Let’s discuss these methods one by one.

public boolean equals(Object o);

The equals() method takes an Object type argument and returns a boolen false or true.

This method checks if some object is passed to it as an argument is equal to the object on which the method is invoked or not.

Default implementation of the equals() method in the Object class says: Check whether two different object references, let r1 and r2,  refer to the same object or not. This method  returns true if and only if r1 and r2 refer to the same object. This is equivalent to something like:

if(r1==r2) or not.

The default implementation performs Shallow Comparison, because the Object class has no data members that defines its state.

The implementation classes that overrides this method performs Deep Comparison, by actually comparing the relevant data members.

Equals() method follows Equivalence Relation properties that you might have studied during your school days.

Equivalence Relation follows Reflexive, Symmetric and Transitive in nature.

Let’s see what does it mean, in Java context.

Assume two object references r1 and r2,

Reflexive:  r1.equals(r1) always returns True.

Symmetric: If r1.equals(r2) returns true/false, then

r2.equals(r1) also returns true/false.

means you can use this property interchangeably

Transitivity: If r1.equals(r2) returns true and r2.equals(r3) returns true, then

r1.equals(r3) also returns true.

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Note that it is generally necessary to override the hashCode method, if you are overriding equals() method, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hashCodes.

Relationship status of these methods on FACEBOOK.

If two objects are equal, then they must have the same hash code, however the opposite is NOT true

Synchonized keyword in java

The synchronized keyword is used with the definition of a method. This would ensure that only one thread can enter that method at the same time. Another threads which is calling this method would wait until the first threads leaves this method.

Static keyword in java

Hi folks.

Be a java beginner or expertise, most of us will see the keyword static in many situations throughout the programming world of java.

A common scenario where you have already seen this keyword is:

public static void main(String args[])

 

So, lets see what does this static keyword do, if used with a method or field.

Java is a purely object oriented language, which means that for accessing the data fields and methods of a class, we should first create the objects of that class.

But are we going to create object for every method or field??

Lets see some examples

In the java package “java.lang” there is one class called as Thread.

The Thread class is having different inbuilt data fields which are declared as static.

Please refer to the link http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MAX_PRIORITY , if don’t know much about the Thread class.

The data field MAX_PRIORITY, MIN_PRIORITY and NORM_PRIORITY are declared as static int.

In order to access these data fields, we don’t require to create object of Thread class.

Instead we require the Thread class itself.

In order to access MAX_PRIORITY,

we simply write Thread.MAX_PRIORITY

that is the syntax is somewhat like this

classname.fieldname

Next….

What about the static methods, does the same rule applies to static methods.??

Yes, ofcourse

For example, the class Integer is having one static method named as ParseInt(String s).

If you don’t know about Integer class, please refer to the link http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html

 

Inorder to access the static method ParseInt(String s), i use the same rule, that is, DON’T CREATE THE OBJECT FOR THE CLASS INTEGER for accessing ParseInt(String s).

Syntax: CLASS_NAME.STATIC_METHOD_NAME

In this case, it would be Integer.ParseInt(String s)

 

 

 

 

I believe you have understood the concept of static in java.

I will get back soon with newer topics, or you can say what topic should i post.

 

Java man

 

 

Quote

A person should not be too honest. Straight trees are cut first and honest people are screwed first….chanakya

Follow

Get every new post delivered to your Inbox.