on 03-30-2011 10:23 AM
Hi,
Strangest thing.
I write one simple UDF to determine on basis of input var1 if output should be var2 or var3; all var1,2 and 3 are strings.
public String determineAddress(String var1, String var2, String var3, Container container) throws StreamTransformationException{
String out = "";
if(var1=="1") {
out = var2;
} else {
out = var3;
}
return out;
I have tested this UDF and strangely enough it always passes var3 as output. I even created same program in Eclipse to make sure i did it correctly and there it works good.
Then i did the following:
I changed var1 to type int. (and my code to: if(var1==1) etc)
This had the result that my udf produced the correct outcome.
Can anyone explain?
kr
Robert
try if (var1.equals("1"))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
As was stated in this article:
http://abhisays.com/java/what-is-difference-between-and-equals-in-java.html
== compares the reference value of string object whereas equals() compares content of the string object.
If you still want to use the == sign, you need to do something similar to this
int temp = Integer.parseInt(var1);
if(temp == "1")
Hope this helps,
Mark
Edited by: Mark Dihiansan on Mar 30, 2011 12:26 PM
Hi Beena, Mark,
Hmmmm.....
It makes sense what you say, but as i said i tried same thing in Eclipse and it does give me the right result
public class Determine {
public static void main(String [] args) {
String out = "";
String var1 = "1";
String var2 = "X";
String var3 = "Y";
if (var1 == "1") {
out = var2;
} else {
out = var3;
}
System.out.println(out);
}
}
This realyy gives me an X as output.
If you both are correct i cannot see why this works.
What is then the difference between what i create in UDF and here in JAVA class?
kr
Robert
The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value .
When you run this in xi its an argument to function and its different string object.
Try var1 = "1" in UDF before comparison , i think it will work.
Regards,
Beena.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.