Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
I want to understand the default behaviour of int | Integer datatype in talend. Any urgent help would be appreciated greatly.
If I have defined two context variables as int | Integer datatype and default value 0 (attached pic) then in the job, I am comparing these two context variables in "IF" condition as below
(context.recordCountInFooter==context.recordCountInFile)
I believe this should return true if my both values are same because both are int. But sometimes it returns false even if both values are same. Then when I change my code to as below it returns true.
(context.recordCountInFooter).equals(context.recordCountInFile)
So here i want to know that when values are same, how does Talend interpret the datatype ? Does it interpret as int or Integer ?
Moreover, I have found that, if my value is 127 then == returns true, but as soon as the value is 128 == returns false. Can anyone plz help why == does not work when my value exceeds 127 ?
hi,
yes for integers we have to compare the int values as below then u wont get false
(context.recordCountInFooter.intValue()==context.recordCountInFile.intValue())
is that solution correct ?
Yes, correct but also possible :
(x.compareTo(y) >= 127 )
It has to do something with primitive types: check this
Hi Dear,
For this we have to understand the Difference between equals method and "==" operator in Java.
Both equals() method and == operator is used to compare two objects in Java. == is an operator and equals() is method. But == operator compare reference or memory location of objects in the heap whether they point to the same location or not . Whenever we create any object using the operator new it will create new memory location for that object. So we use == operator to check memory location or address of two objects are same or not.
When we talk about equals() method the main purpose is to compare the state of two objects or contents of the object. But there is one relation between this two is default implementation of equals() method work like == means it will check the memory reference of the object if they point to the same location then two objects are equals and it is defined in Object class .as we know java.lang.Object class is parent for every other object so default implementation is common for every object but if we want to override the method and want to give own implementation for checking the equality of two objects we can do, and most of the Java classes have their own implementation for equals method where they check the contents of the object.
Coding Example:
// Java program to understand
// the concept of == operator
public class Test {
public static void main(String[] args)
{
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
|
Output:
false true
Explanation: Here we are creating two objects namely s1 and s2.
Let us understand both the operators in detail:
Equality operator(==)
We can apply equality operators for every primitive types including boolean type. we can also apply equality operators for object types.
// Java program to illustrate
// == operator for compatible data
// types
class Test {
public static void main(String[] args)
{
// integer-type
System.out.println(10 == 20);
// char-type
System.out.println('a' == 'b');
// char and double type
System.out.println('a' == 97.0);
// boolean type
System.out.println(true == true);
}
}
|
Output:
false false true true
.equals()
In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.
public class Test {
public static void main(String[] args)
{
Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = t1;
String s1 = new String("GEEKS");
String s2 = new String("GEEKS");
System.out.println(t1 == t3);
System.out.println(t1 == t2);
System.out.println(t1.equals(t2));
System.out.println(s1.equals(s2));
}
}
|
Output:
true false false true
Explanation: Here we are using .equals method to check whether two objects contains the same data or not.