Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Equals' - 4 code snippet(s) found

 Sample 1. Tricky code for String Comparison and String Pool

class BuggyBread { 
public static void main(String[] args)
{
String s2 = "I am unique!";
String s5 = "I am unique!";

System.out.println(s2 == s5); // prints true
}
}

   Like      Feedback     tricky code example  string  string comparison  object comparison   object equality   string equality   string pool


 Sample 2. Using Pojomatic for overriding equals,hashcode and toString methods

import org.pojomatic.Pojomatic;
import org.pojomatic.annotations.AutoProperty;

@AutoProperty
public class Employee {
   public String name;
   public int age;
   public int salary;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public int getSalary() {
      return salary;
   }

   public void setSalary(int salary) {
      this.salary = salary;
   }

   @Override
   public int hashCode() {
      return Pojomatic.hashCode(this);
   }

   @Override
   public boolean equals(Object other) {
      return Pojomatic.equals(this, other);
   }

   @Override
   public String toString() {
      return Pojomatic.toString(this);
   }
}

   Like      Feedback     pojomatic  toString method  toString  hashcode method  equals method   overrding equals method  overriding hashcode method  overrding tostring method  @autoproperty  pojomatic autoproperty


 Sample 3. Overriding equals method

public class ClassInfoBean {
public String url;

@Override
public boolean equals(Object o) {
if (this.url.equals(((ClassInfoBean)o).url)){
return true;
}
return false;
}
}

   Like      Feedback     equals  overriding equals method   @override


 Sample 4. Code Sample / Example / Snippet of com.google.common.base.Joiner

public static void assertArrayEqual(
   String message, Object[] expected, Object[] actual) {
   Joiner joiner = Joiner.on(' ');
   String strExpected = expected == null ? null : joiner.join(expected);
   String strActual = actual == null ? null : joiner.join(actual);
   assertEquals(message, strExpected, strActual);
}

   Like      Feedback      com.google.common.base.Joiner  ternary operator  joiner.join  assertEquals



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus