Home AMX User Forum Duet/Cafe Duet

Cafe Duet (Java) tips for the NetLinx programmer.

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

Comments

  • jweatherjweather Posts: 320
    Spire_Jeff wrote: »
    - To use non-printable characters (think "10,13" from NetLinx), typecast them to char. For example: "POW" + nPowState + (char)13 + (char)10

    Or use C-string style hex escapes: "POW" + nPowState + "\x0D\x0A". (or \n for this particular case)
    - 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");

    Only for strings. == is fine for integers and other primitives.
  • mighemighe Posts: 39
    jweather wrote: »
    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:
    public static void main(String[] args) throws Exception {
    		
    	Integer i1 = new Integer(4);
    	Integer i2 = new Integer(4);
    		
    	System.out.println( i1 == i2);
    		
    }
    
Sign In or Register to comment.