The Complete List of Java Keywords

Discussion in 'Plugin Development' started by BungeeTheCookie, Nov 27, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Vexil

    This is really well made, great job! Sure it will help a lot of people :)
     
  2. Offline

    BungeeTheCookie

  3. Offline

    afistofirony

    Looks nice, but I'd like to point out a couple of things for the sake of accuracy.

    Firstly, your continue/label example is incorrect - the label must be specified in the code itself; it can't refer to the method name. Also, continue can only be used inside a loop (i.e. for, for-each, while, do-while):
    Code:
    public void test () {
        // Label format is <title>:
        loop: for (Player player : Bukkit.getOnlinePlayers()) {
            BlockIterator it = new BlockIterator(player, 50);
            while (it.hasNext()) {
                if (it.next().getType() != Material.AIR) {
                    player.sendMessage(ChatColor.RED + "You must not target anything.");
                    continue loop;
                }
                // etc.
            }
        }
    }
    Also, the keyword extends can be used as a wildcard for type parameters:
    Code:
    List<? extends HumanEntity> humans
    List<?> objects // This implies List<? extends Object>
    class Name<T extends HumanEntity & CommandSender> // This can ONLY be used for type declarations on classes
    This allows any subclass of the given class to be used (however, it's generally unnecessary for variables or fields since subclasses can be used anyway).

    This is not as well-known or common, but super can also be used with type parameters:
    Code:
    List<? super Player> humans
    // You cannot use the super keyword for class type declarations
    This allows any superclass of the given class to be used (i.e. any object that the given class extends).
     
  4. Offline

    BungeeTheCookie

  5. Offline

    The_Doctor_123


    I'm not sure how a JavaScript coder would need to know Java keywords?
     
  6. Offline

    BungeeTheCookie

    Alot of these keywords here are reserved in JavaScript. It is still useful to know as if they could learn just plain Java one day. It's still a useful resource. :p
     
  7. Offline

    The_Doctor_123

    BungeeTheCookie
    JavaScript's keywords may function a little differently from Java's. The examples do them no good since you wrote them in Java.
     
  8. Offline

    BungeeTheCookie

    It still may come in handy one day. :D
     
  9. Offline

    BungeeTheCookie

  10. Offline

    BungeeTheCookie

    Instead of having another thread for this (since the first post was over 30,000 characters) I continued the thread in the next post that I reserved (Good thing I did).
     
  11. Offline

    xTrollxDudex

  12. Offline

    pokepal101

    ‘strict-ff-peh’…

    Well, I guess you learn something new every day.

    BungeeTheCookie
    Let's say you have two Threads and a data-storing class of some sort:

    SomeClass:
    Code:java
    1. public static int number = 0;


    Threads:
    Code:java
    1. public void run() {
    2. int myNumber = SomeClass.number;
    3. myNumber = myNumber + 1; //Do some fancy calculations.
    4. SomeClass.number = myNumber;
    5. }


    Now let's say you start the two threads. The expected behaviour would be that SomeClass.number would increment by 2. However, Java manages multithreading by alternating between the two threads. If the alternating happens at just the right/wrong time, you can end up with something like the following:
    1. The JVM decides to activate Thread 1.
    2. Thread 1 reads SomeClass.number (0) and stores it in myNumber.
    3. Thread 1 reads myNumber (0), does some calculations (1) and stores it in myNumber.
    4. The JVM decides to pause Thread 1 and activate Thread 2.
    5. Thread 2 reads SomeClass.number (0) and stores it in myNumber.
    6. Thread 2 reads myNumber (0), does some calculations (1) and stores it in myNumber.
    7. Thread 2 reads myNumber (1) and stores it in SomeClass.number.
    8. The JVM switches back to Thread 1.
    9. Thread 1 reads myNumber (1) and stores it in SomeClass.number.
    As you can see, because the JVM switched back and forth between the two functions, the result got muddled up, and SomeClass.number was set to 1 instead of 2. The functions are therefore not thread safe.
    This could be solved by declaring the functions as synchronized:
    Code:java
    1. public synchronized void run() { //Could put it in the method declaration,
    2. synchronized { //Or use it as a code block
    3. int myNumber = SomeClass.number;
    4. myNumber = myNumber + 1; //Do some fancy calculations.
    5. SomeClass.number = myNumber;
    6. }
    7. }


    When the JVM sees a function or code block defined as synchronized, it will not interrupt it to switch to a different thread, fixing the problem.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Offline

    BungeeTheCookie

    Thanks, there is still alot of things I have to fix :|. I will add this later, I appreciate it!
     
  14. Offline

    Mrawesomecookie

  15. Offline

    BungeeTheCookie

    I know. I have to update this later...
     
  16. BungeeTheCookie When you do, I suggest you change all of the keywords to lowercase, since they should be, and here's another correction for you: abstract classes can be static, it's just that top-level classes cannot.
     
  17. Offline

    yoft


    BungeeTheCookie
    I just started reading the first post and got a weird look from the person beside me.
    It was because I was making weird noises. Noises of pain and disgust.
    Please, please, PLEASE tell me that you know that
    Code:java

    and
    Code:java
    1. long

    are NOT THE SAME.

    EDIT:
    Even the syntax highlighter on here knows...
     
  18. yoft Wouldn't be a very good syntax highlighter if it didn't recognise syntax :)
     
    yoft likes this.
  19. Offline

    yoft

    AdamQpzm likes this.
  20. Offline

    BungeeTheCookie

    Uhm yea... I haven't updated this since December. I made a few improvements but I am not really maintaining this any more, but I may work on it in July. I am not stupid. A Long is a wrapper class for the primitive long. The End.
     
    yoft likes this.
  21. Offline

    yoft

    This is just kind of a big deal, Java being case-sensitive and all, would you be able to spend the 10 minutes to change this?
     
  22. Offline

    BungeeTheCookie

    I know Java is case sensitive, but keywords are always lowercase.. And since this is the complete list of Java keywords, common sense will tell you to make everything lowercase... I'll do a remake on this soon. Eventually..
     
  23. Offline

    mythbusterma


    Volatile is specifically so that: 1. the variable is thread synchronized with mutual exclusion (i.e. two threads cannot read and write to it at the same time). 2. the variable's value will updated across all threads when changed, so that there are no memory inconsistencies.

    Synchronized is used similarly, in that it is a mutex. Syncrhronization can be done around one object i.e.

    Code:java
    1.  
    2. synchronized (o) {
    3. // modify o
    4. }


    In this way, any other block that tries to synchronize around "o" will be blocked until this thread releases the lock on it.

    It can also be applied to methods such that:


    Code:java
    1. public synchronized void putValue(int val) {
    2. value = val;
    3. }
    4.  
    5. public synchronized int getValue () {
    6. return val;
    7. }


    Would be thread safe, the actual effect of this is something to the nature of:
    Code:java
    1. public void putValue(int val) {
    2. synchronized(this) {
    3. value = val;
    4. }
    5. }
    6.  
    7. public int getValue() {
    8. synchronized(this) {
    9. return value;
    10. }
    11. }


    In that the entire method's body is synchronized, locking the instance of the object that it is in.

    Furthermore, the synchronize keyword will establish that all updates by other threads are seen, as per the Java documentation:

    Also, synchronized (this) {} is the same as synchronized {}.
     
  24. Offline

    1Rogue


    This is incorrect
     
    ZodiacTheories likes this.
  25. Offline

    ZodiacTheories

  26. Offline

    1Rogue


    The synchronized keyword is merely a class lock that makes it so synchronized methods within a class will not execute before another thread invoking it finishes. The method can only have a single thread executing it at a time.
     
  27. Offline

    ZodiacTheories

    1Rogue

    Thanks for the explanation, I haven't really had to deal with Threads much :)
     
  28. Offline

    mythbusterma

  29. Offline

    ZodiacTheories

  30. Offline

    BungeeTheCookie

    I know. I haven't updated this in a while.
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page