## Behaviour of int|Integer in context variables

Highlighted Six Stars

## Behaviour of int|Integer in context variables

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 ?

Accepted Solutions
Highlighted Six Stars

## Re: Behaviour of int|Integer in context variables

As a solution, either I have to use .equals() method or convert the variable to its primitive type by using intValue() and then compare it with ==.
But Talend I think by default considers every object as a wrapper datatype and hence we need such conversions or equals() method.

All Replies
Highlighted Six Stars

## Re: Behaviour of int|Integer in context variables

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 ? Six Stars

## Re: Behaviour of int|Integer in context variables

hi,

yes for integers we have to compare the int values as below then u wont get false

`(context.recordCountInFooter.intValue()==context.recordCountInFile.intValue())`
Highlighted Six Stars

## Re: Behaviour of int|Integer in context variables

is that solution correct ?

Highlighted Ten Stars

## Re: Behaviour of int|Integer in context variables

Yes, correct but also possible :

`(x.compareTo(y) >= 127 )`

It has to do something with primitive types: check this

StackOverflow similar question

Highlighted Six Stars

## Re: Behaviour of int|Integer in context variables

As a solution, either I have to use .equals() method or convert the variable to its primitive type by using intValue() and then compare it with ==.
But Talend I think by default considers every object as a wrapper datatype and hence we need such conversions or equals() method.
Highlighted Six Stars

## Re: Behaviour of int|Integer in context variables

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 == operatorpublic 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 ```

1. Explanation: Here we are creating two objects namely s1 and s2.

• Both s1 and s2 refers to different objects.
• When we use == operator for s1 and s2 comparison then the result is false as both have different addresses in memory.
• Using equals, the result is true because its only comparing the values given in 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// typesclass 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.

• In the above example, we are creating 3 Thread objects and 2 String objects.
• In the first comparison, we are checking that t1 == t3 or not. As we know that both t1 and t3 pointing to same object that’s why it returns true.
• When we are comparing 2 String objects by .equals() operator then we are checking that is both objects contains the same data or not.
• Both the objects contains the same String i.e. GEEKS that’s why it returns true.

## OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

## Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

## Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

## Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog