I've been developing for a CraftBukkit Minecraft server. This gives me a good API to work with. This API includes the onCommand method, which allows me to implement new commands.  I've just been trying to do that, but when I send a command such as /example arg0, for some reason I can't compare with arg0 from my code. I'm trying to check for when args[0] == "on", yet although args and args[0] are both defined and I set args[0] to "on", the comparison still returns false.  private CBPlugin pluginReference;  public NerfTntExecutor(CBPlugin plugin) {     // (...)     pluginReference = plugin; }  public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {     if (sender.hasPermission("command.op.nerftnt")) {         if(args.length &gt; 0) {             // (...)             sender.sendMessage(args[0]);             sender.sendMessage(args[0].getClass().getName());             if(args[0] == "on")                 sender.sendMessage("true");             return true;         }         else {             sender.sendMessage("Not enough arguments!");             return false;         }     }     else {         pluginReference.getLogger().info("No permission");         return false;     } }   The sender.sendMessage calls display the data given back to the person who sent the command. In this case, args[0] is displayed as "on" and args[0].getClass().getName() gets shown as java.lang.String - the type of the argument. However, the next line if(args[0] == "on") is false.  Why is this? I seem to be comparing a String to another String, and they both have the same contents. Why should a comparison of the two return false?
93
I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.  Is == bad? When should it and should it not be used? What's the difference?
9770
I'm sure you all know the behaviour I mean - code such as:  Thread thread = new Thread(); int activeCount = thread.activeCount();   provokes a compiler warning.  Why isn't it an error?  EDIT:  To be clear: question has nothing to do with Threads.  I realise Thread examples are often given when discussing this because of the potential to really mess things up with them.  But really the problem is that such usage is always nonsense and you can't (competently) write such a call and mean it.  Any example of this type of method call would be barmy.  Here's another:  String hello = "hello"; String number123AsString = hello.valueOf(123);   Which makes it look as if each String instance comes with a "String valueOf(int i)" method.
96695
Using any minecraft command that requires a entity to run (like /tp /testfor and etc) requires a entity piece, you can do it with players using the playername, and you can do with @e, @r, @p and @a, providing arguments or not.  That is my question: What are all Minecraft selector arguments (in 1.8) The ones I currently know are score_scorename=value, score_scorename_min=value.
4656
I have this piece of code (summarized)...  AnsiString working(AnsiString format,...) {     va_list argptr;     AnsiString buff;      va_start(argptr, format);     buff.vprintf(format.c_str(), argptr);      va_end(argptr);     return buff; }   And, on the basis that pass by reference is preferred where possible, I changed it thusly.  AnsiString broken(const AnsiString &amp;format,...) { ... the rest, totally identical ... }   My calling code is like this:-  AnsiString s1, s2;     s1 = working("Hello %s", "World");     s2 = broken("Hello %s", "World");   But, s1 contains "Hello World", while s2 has "Hello (null)". I think this is due to the way va_start works, but I'm not exactly sure what's going on.
80357
Inspired by a , I have some that essentially all boil down to asking  What are the differences between \def and \newcommand?  In particular, I wonder   Is it possible to have parameters passed to \defed commands, both optional and required? Is there a \redef command equivalent to \renewcommand? I have only ever used \newcommand -- is there any reason I should change that habit?   but I welcome comments on anything else I may not have thought of in this list!
77756
I am trying to write a bash script which run a command and compare the result with another string.   #!/bin/bash -x  STATUS=`/root/setup_ha show --password-file=/root/password | grep "&gt;HA State" | awk '{print $3}' |  cut -c 2-`  TEST=`echo $STATUS` if [[ "$TEST" == "ON Master" ]]; then echo CLUSTER CRITICAL else   echo CLUSTER OK MASTER fi   As the Original string is on two lines, I echo it in a new variable TEST. The New variable have the output of the command on one line.  Here is the Bash debug output :  ++ /root/setup_ha show --password-file=/root/password ++ grep '&gt;HA State' ++ awk '{print $3}' ++ cut -c 2- + STATUS='ON Master' ++ echo 'ON' Master + TEST='ON Master' + [[ ON Master == \O\N\ \M\a\s\t\e\r ]] + echo CLUSTER OK MASTER CLUSTER OK MASTER   I also tried the following test :  if [[ "$TEST" =~ "ON Master" ]]   The thing is bash is not able to compare the strings it is always false.  Any idea ?   EDIT :  Here is the output with the first answer :  + STATUS='ON Master' ++ echo 'ON' Master + TEST='ON Master' + '[' 'ON Master' == 'ON Master' ']' + echo CLUSTER OK MASTER CLUSTER OK MASTER   Still  not working ON seems weird on line 3, plus in my bash it takes green color !
83024
I have two arrays:  arrayA=(1 2 3) arrayB=(a b c)   and I want to print out one of them using a command line argument, i.e., without any if else.  I tried a few variations on the syntax with no success. I am wanting to do something like this:  ARG="$1"  echo ${array${ARG}[@]}   but I get a "bad substitution" error. How can I achieve this?
79610
an example code what I try to do will surely do better than my english:  public bool IsNumericValueInBounds (string value, Type numericType) {   double d = double.NaN;         bool inBounds = (bool)numericType.GetMethod ("TryParse").Invoke (null, new object[] { value, d });    return inBounds; }   Unfortunately the TryParse method needs an out parameter so this doesn't work. any ideas how to solve this?  (ps.: would'nt this be a nice example for duck typing? - because i know every numericType has an "TryParse" or I am mistaken?)
7905
What is the difference between == and ===?  How exactly does the loosely == comparison work? How exactly does the strict === comparison work?  What would be some useful examples?
76187
How can I test whether a command's output is an empty string while not   interfering with buffering?  Example:  command | ??? if [ $? = 0 ] &amp;&amp; [ -n "$output_from_command"]; then    ...   I want the output from the code above to be the same as when running command.
36461
I have a PHP script that needs to determine if it's been executed via the command-line or via HTTP, primarily for output-formatting purposes. What's the canonical way of doing this? I had thought it was to inspect SERVER['argc'], but it turns out this is populated, even when using the 'Apache 2.0 Handler' server API.
453
I need to write a Java Comparator class that compares Strings, however with one twist.  If the two strings it is comparing are the same at the beginning and end of the string are the same, and the middle part that differs is an integer, then compare based on the numeric values of those integers.  For example, I want the following strings to end up in order they're shown:   aaa bbb 3 ccc bbb 12 ccc ccc 11 ddd eee 3 ddd jpeg2000 eee eee 12 ddd jpeg2000 eee   As you can see, there might be other integers in the string, so I can't just use regular expressions to break out any integer.  I'm thinking of just walking the strings from the beginning until I find a bit that doesn't match, then walking in from the end until I find a bit that doesn't match, and then comparing the bit in the middle to the regular expression "[0-9]+", and if it compares, then doing a numeric comparison, otherwise doing a lexical comparison.  Is there a better way?  Update I don't think I can guarantee that the other numbers in the string, the ones that may match, don't have spaces around them, or that the ones that differ do have spaces.
69850
I have the following code:  Using cmd As SqlCommand = Connection.CreateCommand     cmd.CommandText = "UPDATE someTable SET Value = @Value"     cmd.CommandText &amp;= " WHERE Id = @Id"     cmd.Parameters.AddWithValue("@Id", 1234)     cmd.Parameters.AddWithValue("@Value", "myValue")     cmd.ExecuteNonQuery End Using   I wonder if there is any way to get the final SQL statment as a String, which should look like this:  UPDATE someTable SET Value = "myValue" WHERE Id = 1234   If anyone wonders why I would do this:   for logging (failed) statements for having the possibility to copy &amp; paste it to the Enterprise Manager for testing purposes
55175
I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform):  public class Metadata implements Comparable&lt;Metadata&gt; {     private String name;     private String value;  // Imagine basic constructor and accessors here // Irrelevant parts omitted }   I want the natural ordering for these objects to be: 1) sorted by name and 2) sorted by value if name is the same; both comparisons should be case-insensitive. For both fields null values are perfectly acceptable, so compareTo must not break in these cases.   The solution that springs to mind is along the lines of the following (I'm using "guard clauses" here while others might prefer a single return point, but that's beside the point):  // primarily by name, secondarily by value; null-safe; case-insensitive public int compareTo(Metadata other) {     if (this.name == null &amp;&amp; other.name != null){         return -1;     }     else if (this.name != null &amp;&amp; other.name == null){         return 1;     }     else if (this.name != null &amp;&amp; other.name != null) {         int result = this.name.compareToIgnoreCase(other.name);         if (result != 0){             return result;         }     }      if (this.value == null) {         return other.value == null ? 0 : -1;     }     if (other.value == null){         return 1;     }      return this.value.compareToIgnoreCase(other.value); }   This does the job, but I'm not perfectly happy with this code. Admittedly it isn't very complex, but is quite verbose and tedious.  The question is, how would you make this less verbose (while retaining the functionality)? Feel free to refer to Java standard libraries or Apache Commons if they help. Would the only option to make this (a little) simpler be to implement my own "NullSafeStringComparator", and apply it for comparing both fields?  Edits 1-3: Eddie's right; fixed the "both names are null" case above  About the accepted answer  I asked this question back in 2009, on Java 1.6 of course, and at the time  was my preferred accepted answer. I never got round to changing that until now (2017).  There are also —a 2009 Apache Commons Collections one and a 2013 Guava one, both posted by me—that I did prefer at some point in time.  I now made the clean  the accepted answer. That should definitely be preferred if on Java 8, and these days Java 8 should be available to nearly all projects.
67060
I want to be able to have LinkedList.contains() return true for a custom comparator.  Suppose that I have 1 LinkedList and 2 objects  LinkedList&lt;MyObject&gt; myList = new LinkedList&lt;MyObject&gt;();  MyObject a = new MyObject("HELLO"); MyObject b = new MyObject("HELLO");   Technicaly, both objects are identical in terms of comparison (MyObject implements Comparable)   ( a == b ) == true  however, when I do the following, myList does not return true for myList.contains(b)  myList.add(a) myList.contains(b) // == false   I think its because contains will check object reference and see that a and b are 2 distinct objects. Is there any way I can make it so I don't have to extend LinkedList to compare those objects?
58615
Have you ever tried this before?  static void Main(string[] args) {     int x = 10;     x = x++;     Console.WriteLine(x); }   Output: 10.  but for   static void Main(string[] args) {     int x = 10;     x++;     Console.WriteLine(x); }   Output: 11.  Could anyone explain why this?
92333
if ('text'.equals(str))   or   if ('text' == str)   Is there any difference? If yes, then what is it?   Added, after answered.  String q = 'teXt'; system.debug('teXt'.equals(q)); // true system.debug('teXt' == q);      // true system.debug('text'.equals(q)); // false system.debug('text' == q);      // true