Changes

Jump to navigation Jump to search
2,829 bytes added ,  14:30, 28 April 2022
m
Line 3: Line 3:  
===With apt===
 
===With apt===
 
  <nowiki>sudo apt-add-repository ppa:webupd8team/java
 
  <nowiki>sudo apt-add-repository ppa:webupd8team/java
                                              sudo apt-get update
+
                                                sudo apt-get update
                                              sudo apt-get install oracle-java8-installer</nowiki>
+
                                                sudo apt-get install oracle-java8-installer</nowiki>
    
Also ensure your JAVA_HOME variable has been set to:
 
Also ensure your JAVA_HOME variable has been set to:
Line 1,379: Line 1,379:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
=== CopyOnWriteArrayList ===
+
===CopyOnWriteArrayList===
 
Expensive write, fast concurrent read, synchronized  writes, free reads. <syntaxhighlight lang="java">
 
Expensive write, fast concurrent read, synchronized  writes, free reads. <syntaxhighlight lang="java">
 
import java.utils.List;
 
import java.utils.List;
Line 1,397: Line 1,397:  
     }
 
     }
 
}
 
}
</syntaxhighlight><br />
+
</syntaxhighlight>
 +
 
 +
== Java tips ==
 +
 
 +
=== Static imports ===
 +
<syntaxhighlight lang="java">
 +
import static java.lang.System.out;
 +
import static java.util.Collections.*;
 +
 
 +
out.println("System.out.printl  System not needed");
 +
 
 +
sort(new ArrayList<String>());
 +
</syntaxhighlight>
 +
 
 +
=== Blocks ===
 +
<syntaxhighlight lang="java">
 +
pubublic class BlocksRunner {
 +
    public static void main(String[] args){
 +
      {
 +
          int i;
 +
          System.out.println("i can be accessed here: " + i);
 +
      }
 +
      System.out.println("i can not be accessed here")
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
=== equals and hashCode methods from an object ===
 +
2 objects only obj.equals if they are the same object, but equals can be overriden (generate hashcode and equals)
 +
 
 +
Hash function should return the same value if 2 objects are equal
 +
 
 +
=== Access modifiers ===
 +
public: accessible every where.
 +
 
 +
protected: accessible in same package or subclasses.
 +
 
 +
(default): only inside the class or package.
 +
 
 +
private: only inside the class.
 +
 
 +
=== non access modifier ===
 +
 
 +
==== final ====
 +
final classes can not be extended.
 +
 
 +
final methods can not be overridden.
 +
 
 +
final variable can not change (Constants).
 +
 
 +
final argument can not be changed.
 +
 
 +
==== static ====
 +
static class variables are common to all class instances.
 +
 
 +
static methods can be called from class, no need for instance, inside static methods instance variables or methods are not accessible.
 +
 
 +
==== Constants ====
 +
public static final
 +
 
 +
==== Enums ====
 +
like: java.time.Month, java.time.DayOfWeek<syntaxhighlight lang="java">
 +
enum Season {
 +
    WINTER, SPRING, SUMMER, FALL;
 +
 
 +
  // to store in db
 +
    private int value;
 +
    WINTER(1), SPRING(2), SUMMER(3), FALL(4);
 +
 
 +
    private Season(int value){
 +
        this.value = value;
 +
    }
 +
}
 +
 
 +
public class EnumRunner{
 +
    public static void main(String[] args){
 +
        Season season = Season.WINTER;
 +
        Season season1 = Season.valueOf("WINTER");
 +
 
 +
        System.out.print(season1);
 +
        System.out.print(Season.SPRING.ordinal());
 +
        System.out.print(Season.SPRING.getValue());
 +
        System.out.print(Arrays.toString(Season.values()));
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Sort strings ==
 +
<syntaxhighlight lang="java">
 +
import java.util.ArrayList;
 +
import java.util.Collections;
 +
import java.util.List;
 +
import java.util.Comparator;
 +
 
 +
class LengthComparator implements Comparator<String> {
 +
    @Override
 +
    public int compare(String str1, String str2){
 +
        return Integer.compare(str1.length(), str2.length());
 +
    }
 +
}
 +
 
 +
public class AnonymousClassRunner{
 +
    public static void main(String args){
 +
        List<String> animals = new ArrayList<String>(List.of("Ant", "Cat", "Ball", "Elephant"));
 +
        // Collections.sort(animals);  //Alphabetical order
 +
        Collections.sort(animals, new LengthComparator());  //Length order
 +
        System.out.println(animals);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 
==Eclipse shortcuts==
 
==Eclipse shortcuts==
 
Ctrl + Space → Autocomplete.<br />
 
Ctrl + Space → Autocomplete.<br />

Navigation menu