Fri Jan 30, 2009 1:44 am
int x=5;
int y=5;
if(x==y)
{
System.out.println("x is equal y");
}
float xFloat = 5.1f;
float yFloat = 5.1f;
if (xFloat == yFloat) {
System.out.println("xFloat is equal yFloat");
}
boolean xBoolean = true;
boolean yBoolean = true;
if (xBoolean == yBoolean) {
System.out.println("xBoolean is equal yBoolean");
}
int x = 5;
int y = 5;
int z = 4;
int u = 5;
if ((x == y)&&(z == u) && (x == z)) {
System.out.println("They are equal");
}else
{
System.out.println("They are not equal");
}
int x = 5;
int y = 3;
if (x!=y) {
System.out.println("They are not equal");
}
public class Main {
public static void main(String[] args) {
class1 obj1Ref=new class1();
class1 obj2Ref=new class1();
if(obj1Ref==obj2Ref)
{
System.out.println("# obj1Ref equals obj2Ref ");
}else
{
System.out.println("# obj1Ref not equal to obj2Ref ");
}
// to be looking at the same object
obj1Ref=obj2Ref;
if(obj1Ref==obj2Ref)
{
System.out.println("# obj1Ref equals obj2Ref ");
}
}
}
class class1{
}
class class2{
}
# obj1Ref not equal to obj2Ref
# obj1Ref equals obj2Ref
class1 obj1Ref=new class1();
class2 obj2Ref=new class2();
if(obj1Ref==obj2Ref)
{
System.out.println("# obj1Ref equals obj2Ref ");
}
myObject.equals(otherObject);
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
String st1="String1";
String st2="String1";
if(st1.equals(st2))
{
System.out.println("The String values are equal");
}
public class Main {
public static void main(String[] args) {
class1 ref1=new class1(4,3);
class1 ref2=new class1(4,55);
if(ref1.equals(ref2))
{
System.out.println("The two object are equal");
}
if(ref1!=ref2)
{
System.out.println("The two reference are different");
}
}
}
class class1{
private int x;
private int y;
public class1(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
@Override
public boolean equals(Object obj) {
return this.x== ((class1)obj).getX();
}
The two object are equal
The two reference are different
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
class1 ref1=new class1(4,3);
class1 ref2=new class1(4,55);
class1 ref3=new class1(53,12);
class1 ref4=new class1(43,52);
class1 ref5=new class1(44,44);
class1 ref6=new class1(11,11);
List<class1> arrList =new ArrayList();
arrList.add(ref1);
arrList.add(ref2);
arrList.add(ref3);
arrList.add(ref4);
arrList.add(ref5);
arrList.add(ref6);
Collections.sort(arrList);
for(int i=0;i<arrList.size();i++)
{
System.out.println(" : "+arrList.get(i).getX()+" "+arrList.get(i).getY());
}
}
}
class class1 implements Comparable {
private int x;
private int y;
public class1(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
@Override
public boolean equals(Object obj) {
return this.x== ((class1)obj).getX();
}
@Override
public int compareTo(Object o) {
if(this.x<((class1)o).getX())
{
return 1;
}else if(this.x>((class1)o).getX())
{
return -1;
}else
{
return 0;
}
}
public int getY() {
return y;
}
}
: 53 12
: 44 44
: 43 52
: 11 11
: 4 3
: 4 55
Thu Jun 13, 2013 11:28 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.