Changes

Jump to navigation Jump to search
3,974 bytes added ,  09:16, 12 May 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 79: Line 79:  
                 System.out.println("My integer is: " + myInt);
 
                 System.out.println("My integer is: " + myInt);
 
}
 
}
}</source>
+
}</source><syntaxhighlight lang="java">
 +
//Java 11
 +
" ".isBlank();  // --> true
 +
" lr ".strip();  //Java 11
 +
" lr ".stripLeading().replace(" ", "@");
 +
" lr ".stripTrailing().replace(" ", "@");
 +
"line1\nline2\nline3".lines().forEach(System.out::println);
 +
 
 +
//Java 12
 +
"UPPER".transform(s -> s.substring(2));
 +
 
 +
//Java 13
 +
"My name is %s. My age is %d".formatted("Rafa", 40);
 +
 
 +
//Java 14
 +
 
 +
 
 +
</syntaxhighlight>
 +
 
 
==Enum type==
 
==Enum type==
 
See code example:<br />
 
See code example:<br />
Line 279: Line 297:  
words.contains("Dog");
 
words.contains("Dog");
 
words.indexOf("Cat");
 
words.indexOf("Cat");
</syntaxhighlight>
+
</syntaxhighlight>List.of() and List.copyOf() creates unmodifiable lists copyOf is introduced on Java 10
    
===ArrayList===
 
===ArrayList===
Line 947: Line 965:  
         List<String> list = List.of("Apfel", "Junge", "Hund");
 
         List<String> list = List.of("Apfel", "Junge", "Hund");
 
         Files.write(filePath, list);
 
         Files.write(filePath, list);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 +
===readString writeString (Java 11)===
 +
<syntaxhighlight lang="java">
 +
import java.io.IOException;
 +
import java.nio.file.Files;
 +
import java.nio.file.Path;
 +
import java.nio.file.Paths;
 +
 +
public class FileReadWriteRunner {
 +
    public static void main(String[] args) throws IOException {
 +
 +
        // Read
 +
        Path path = Paths.get("./file.txt");
 +
        String fileContent = Files.readString(path);
 +
        System.out.println(fileContent);
 +
 +
        // Write
 +
        String newFileContent = fileContent.replace("Line", "Lines");
 +
        Path path = Paths.get("./newfile.txt");
 +
        Files.writeString(newFilePath, newFileContent);
 
     }
 
     }
 
}
 
}
Line 955: Line 996:  
==Exceptions==
 
==Exceptions==
 
[http://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html Class Exception]
 
[http://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html Class Exception]
 +
 +
[https://github.com/in28minutes/in28minutes-initiatives/blob/master/The-in28Minutes-TroubleshootingGuide-And-FAQ/quick-start.md Exception Troubleshooting]
 
===trows===
 
===trows===
 
<source lang="java">
 
<source lang="java">
Line 1,399: Line 1,442:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
== Java tips ==
+
==Java tips==
   −
=== Static imports ===
+
===Static imports===
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
import static java.lang.System.out;
 
import static java.lang.System.out;
Line 1,411: Line 1,454:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
=== Blocks ===
+
===Blocks===
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
pubublic class BlocksRunner {
 
pubublic class BlocksRunner {
Line 1,424: Line 1,467:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
=== equals and hashCode methods from an object ===
+
===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)
 
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
 
Hash function should return the same value if 2 objects are equal
   −
=== Access modifiers ===
+
===Access modifiers===
 
public: accessible every where.
 
public: accessible every where.
   Line 1,438: Line 1,481:  
private: only inside the class.
 
private: only inside the class.
   −
=== non access modifier ===
+
===non access modifier===
   −
==== final ====
+
====final====
 
final classes can not be extended.
 
final classes can not be extended.
   Line 1,449: Line 1,492:  
final argument can not be changed.
 
final argument can not be changed.
   −
==== static ====
+
====static====
 
static class variables are common to all class instances.
 
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.
 
static methods can be called from class, no need for instance, inside static methods instance variables or methods are not accessible.
   −
==== Constants ====
+
====Constants====
 
public static final
 
public static final
   −
==== Enums ====
+
====Enums====
 
like: java.time.Month, java.time.DayOfWeek<syntaxhighlight lang="java">
 
like: java.time.Month, java.time.DayOfWeek<syntaxhighlight lang="java">
 
enum Season {
 
enum Season {
Line 1,484: Line 1,527:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
== Sort strings ==
+
====module-info.java====
<syntaxhighlight lang="java">
+
requires: allows usage of other modules
 +
 
 +
requires transitive: allows usage of other modules and modules that use this module will also have access to it
 +
 
 +
exports: allows other packages to use your classes
 +
<br />
 +
 
 +
==Predicate Not==
 +
Java 11<syntaxhighlight lang="java">
 +
import java.util.List;
 +
import java.util.function.Predicate;
 +
 
 +
public class PredicateNotRunner {
 +
    public static boolean isEven(Integer number){
 +
        return number&2==0;
 +
    }
 +
    public static void main(String[] args){
 +
        List<Integer> numbers = List.of(3, 4, 5, 57, 65, 88);
 +
        // Predicate<Integer> evenNumberPredicate = number -> number%2==0;
 +
        // numbers.stream().filter(evenNumberPredicate).forEach(System.out::println);
 +
       
 +
        numbers.stream().filter(Predicate.not(PredicateNotRunner::isEven)).forEach(System.out::println);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
==Sort strings==
 +
<syntaxhighlight lang="java">
 
import java.util.ArrayList;
 
import java.util.ArrayList;
 
import java.util.Collections;
 
import java.util.Collections;
Line 1,508: Line 1,578:  
</syntaxhighlight>
 
</syntaxhighlight>
    +
==Logger==
 +
<syntaxhighlight lang="java">
 +
import java.util.logging.Logger
 +
 +
public class BubbleSort {
 +
    public List<String> sort(List<String> names){
 +
        return names;
 +
    }
 +
}
 +
 +
public class MySortingUtil {
 +
    public List<String> sort(List<String> names){
 +
        BubbleSort bubbleSort = new BubbleSort();
 +
        return bubbleSort.sort(names);
 +
    }
 +
}
 +
 +
public class MySortingUtilConsumer {
 +
    private static Logger logger = Logger.getLogger(MySortingUtilConsumer.class.getName());
 +
 +
    public static void main(String args){
 +
        MySortingUtil util = MySortingUtil();
 +
        List<String> sorted = util.sort(List.of("Elephant", "Bat", "Mosquito"));
 +
        logger.info(sorted.toString());
 +
    }
 +
}
 +
</syntaxhighlight><br />
 
==Eclipse shortcuts==
 
==Eclipse shortcuts==
 
Ctrl + Space → Autocomplete.<br />
 
Ctrl + Space → Autocomplete.<br />
Line 1,517: Line 1,614:  
==Notes==
 
==Notes==
 
Hibernate → Java ORM framework for web design
 
Hibernate → Java ORM framework for web design
 +
 +
==java cli==
 +
<syntaxhighlight lang="bash">
 +
# Compile class
 +
javac someclass.java
 +
 +
# List modules
 +
java --list-modules
 +
 +
# Java shell
 +
jshell
 +
 +
# Run java application
 +
java -jar someclass.jar
 +
 +
# List module dependencies
 +
java -d java.sql
 +
 +
 +
</syntaxhighlight>
 +
 +
==Frameworks==
 +
[[Java: Spring|Spring]]
 +
 +
==Java and Eclipse troubleshooting==
 +
 +
====Default Home Folder for JDK====
 +
 +
*Windows: C:\Program Files\Java\jdk-{version}
 +
**Example for JDK 16 - C:\Program Files\Java\jdk-16
 +
**Example for JDK 17 - C:\Program Files\Java\jdk-17
 +
*Mac: /Library/Java/JavaVirtualMachines/jdk-{version}.jdk/Contents/Home
 +
**Example for JDK 16 - /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
 +
**Example for JDK 17 - /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
 +
 +
====Default Home Folder for JDK====
 +
 +
*Windows: C:\Program Files\Java\jdk-{version}\bin
 +
**Example for JDK 16 - C:\Program Files\Java\jdk-16\bin
 +
*Mac: /Library/Java/JavaVirtualMachines/jdk-{version}.jdk/Contents/Home/bin
 +
**Example for JDK 16 - /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home/bin
 +
 +
[[Category:Java]]

Navigation menu