Cafe Duet (Java) tips for the NetLinx programmer.
Spire_Jeff
Posts: 1,917
I decided to start a new thread specifically for those little tips, tricks and gotchas that might help a NetLinx programmer writing Duet code.
- To use non-printable characters (think "10,13" from NetLinx), typecast them to char. For example: "POW" + nPowState + (char)13 + (char)10
- Use .equal() instead of == for logical comparisons. == will often check to see if the two variables point to the same object, not compare the values of the objects. For example: variable.equals("test text");
- switch()..case statements in Java only work on intrinsic variables.
- insert break; statements at the end of each case: in a switch case, or all cases after the matching case will be executed.
- When using the .substring command, remember that java indexes start at 0, not 1. Also, when providing the second int, the command does not return the last value. So, if you do someString.substring(0,7) you will get the first seven chars in the string.
- There is no Select..Active command in java, use nested if..else commands.
I will add more as they come along.
Jeff
- To use non-printable characters (think "10,13" from NetLinx), typecast them to char. For example: "POW" + nPowState + (char)13 + (char)10
- Use .equal() instead of == for logical comparisons. == will often check to see if the two variables point to the same object, not compare the values of the objects. For example: variable.equals("test text");
- switch()..case statements in Java only work on intrinsic variables.
- insert break; statements at the end of each case: in a switch case, or all cases after the matching case will be executed.
- When using the .substring command, remember that java indexes start at 0, not 1. Also, when providing the second int, the command does not return the last value. So, if you do someString.substring(0,7) you will get the first seven chars in the string.
- There is no Select..Active command in java, use nested if..else commands.
I will add more as they come along.
Jeff
0
Comments
Or use C-string style hex escapes: "POW" + nPowState + "\x0D\x0A". (or \n for this particular case)
Only for strings. == is fine for integers and other primitives.
== is fine ONLY for primitives or comparing to null: every other type should be checked using .equals, specially for immutable objects whose value is "more important" than their identity
For example the following code will print false: