The enako () metoda je metoda razreda Integer pod java.lang paket. Ta metoda primerja vrednost parametra z vrednostjo trenutnega celega števila predmet . Vrača se Boolean (True ali False), kar ustreza enakosti tega predmeta Integer in metode argumenta. Prav tako preglasi metodo equals() razreda Object.
Sintaksa:
Sledi izjava enako () metoda:
public boolean equals(Object obj)
Parameter:
DataType | Parameter | Opis | Obvezno/izbirno |
---|---|---|---|
Objekt | obj | Preveri enakost s podanim celim številom | Obvezno |
Vrne:
The enako () metoda bo vrnila true, če argument ni nič in če so celoštevilski objekti enaki objektu argumenta metode, sicer bo vrnila false.
Izjeme:
InputMismatchException
shrani youtube video vlc
Združljivostna različica:
Java 1.2 in novejši
Primer 1
public class IntegerEqualsExample1 { public static void main(String[] args) { Integer obj1 = new Integer(43); Integer obj2 = new Integer(78); System.out.print('obj1 and obj2 are equal. True or False? = '); System.out.println(obj1.equals(obj2)); obj1 = new Integer(55); obj2 = new Integer(55); System.out.print('obj1 and obj2 are equal. True or false? = '); System.out.println(obj1.equals(obj2)); } }Preizkusite zdaj
Izhod:
obj1 and obj2 are equal. True or False? = false obj1 and obj2 are equal. True or false? = true
Primer 2
import java.util.Scanner; public class IntegerEqualsExample2 { public static void main(String[] args) { Scanner readInput = new Scanner(System.in); System.out.print('Input the first Integer Number to be Compare: '); Integer intComp1 = readInput.nextInt(); System.out.print('Input the second Integer Number to be Compare: '); Integer intComp2 = readInput.nextInt(); boolean Result = intComp1.equals(intComp2); if (Result){ System.out.println('Both the Integer numbers are same...'); } else { System.out.println('Both the Integer numbers are different...'); } readInput.close(); } }
Izhod:
amrita rao igralec
1. Input the first Integer Number to be Compare: 34 Input the second Integer Number to be Compare: 34 Both the Integer numbers are same... 2. Input the first Integer Number to be Compare: 45 Input the second Integer Number to be Compare: 87 Both the Integer numbers are different...
Primer 3
public class IntegerEqualsExample3 { public static void main(String[] args) { Integer objA = 20; Integer objB = 20; Integer objC = 10; System.out.println('objA == objB? ' + objA.equals(objB)); System.out.println('objB == objC? ' + objB.equals(objC)); System.out.println('objA == objC? ' + objA.equals(objC)); System.out.println('objC == objA? ' + objC.equals(objA)); System.out.println('objB == objA? ' + objB.equals(objA)); } }Preizkusite zdaj
Izhod:
objA == objB? true objB == objC? false objA == objC? false objC == objA? false objB == objA? true
Primer 4
public class IntegerEqualsExample4 { public static void main(String[] args) { Float ObjFloat = 55.55f; Double ObjDouble = 55.55d; System.out.println('ObjFloat == ObjDouble? ' +ObjFloat.equals(ObjDouble)); } }Preizkusite zdaj
Izhod:
ObjFloat == ObjDouble? false