| Line 1: |
Line 1: |
| | All the code can be found at: [https://github.com/rafahsolis/javaTutorial javaTutorial] | | All the code can be found at: [https://github.com/rafahsolis/javaTutorial javaTutorial] |
| − | == Install == | + | ==Install== |
| − | === 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: |
| | /usr/lib/jvm/java-8-oracle | | /usr/lib/jvm/java-8-oracle |
| | | | |
| − | === From Source === | + | ===From Source=== |
| | wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz | | wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz |
| | sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle_jdk8/jre/bin/java 2000 | | sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle_jdk8/jre/bin/java 2000 |
| Line 16: |
Line 16: |
| | update-alternatives --display javac | | update-alternatives --display javac |
| | | | |
| − | == hello world == | + | ==hello world== |
| | <source lang="java"> | | <source lang="java"> |
| | public class aplication { | | public class aplication { |
| Line 27: |
Line 27: |
| | | | |
| | }</source> | | }</source> |
| − | == Numeric variables == | + | ==Numeric variables== |
| | <source lang="java"> | | <source lang="java"> |
| | public class variables { | | public class variables { |
| Line 45: |
Line 45: |
| | | | |
| | }</source> | | }</source> |
| − | == Strings == | + | |
| | + | == Data types == |
| | + | byte b; byte (8 bits) |
| | + | |
| | + | short s; short (16 bits) |
| | + | |
| | + | int i; Integer (32 bits) |
| | + | |
| | + | long lo; long (64 bits) |
| | + | |
| | + | float f=4.0f; (32 bits) Not precise use big decimal instead |
| | + | |
| | + | double d; (64 bits) Not precise use big decimal instead |
| | + | |
| | + | char c; (16 bits) |
| | + | |
| | + | String st; String |
| | + | |
| | + | boolean isTrue; true/false |
| | + | |
| | + | Todo: Arrays etc... |
| | + | |
| | + | ==Strings== |
| | <source lang="java"> | | <source lang="java"> |
| | public class stringstuto { | | public class stringstuto { |
| Line 58: |
Line 80: |
| | } | | } |
| | }</source> | | }</source> |
| − | == Enum type == | + | ==Enum type== |
| | See code example:<br /> | | See code example:<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/EnumTypes.java EnumTypes.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/EnumTypes.java EnumTypes.java]<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Animal.java Animal.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/Animal.java Animal.java]<br /> |
| | | | |
| − | == Operators == | + | ==Operators== |
| − | === Asignment === | + | ===Asignment=== |
| | = Asignment | | = Asignment |
| − | === Arithmetic === | + | ===Arithmetic=== |
| | + Additive<br /> | | + Additive<br /> |
| | - Substraction<br /> | | - Substraction<br /> |
| Line 73: |
Line 95: |
| | % Remainder<br /> | | % Remainder<br /> |
| | | | |
| − | === Increment / Decrement === | + | ===Increment / Decrement=== |
| | ++ Increment<br /> | | ++ Increment<br /> |
| | -- Decrement<br /> | | -- Decrement<br /> |
| − | === Logic === | + | ===Logic=== |
| | == Equal to (*)<br /> | | == Equal to (*)<br /> |
| | != Not Equal to<br /> | | != Not Equal to<br /> |
| Line 88: |
Line 110: |
| | <br /> | | <br /> |
| | (*) Check .equals()<br /> | | (*) Check .equals()<br /> |
| − | === Bitwise and Bit Shift Operators === | + | ===Bitwise and Bit Shift Operators=== |
| | ~ Unary bitwise complement<br /> | | ~ Unary bitwise complement<br /> |
| | << Signed left shift<br /> | | << Signed left shift<br /> |
| Line 96: |
Line 118: |
| | ^ Bitwise exclusive OR<br /> | | ^ Bitwise exclusive OR<br /> |
| | | Bitwise inclusive OR<br /> | | | Bitwise inclusive OR<br /> |
| − | === Type comparison operator === | + | ===Type comparison operator=== |
| | instanceof | | instanceof |
| | | | |
| − | == Loops == | + | ==Loops== |
| − | === While === | + | ===While=== |
| | <source lang="java"> | | <source lang="java"> |
| | int myInt = 0; | | int myInt = 0; |
| Line 109: |
Line 131: |
| | }</source> | | }</source> |
| | | | |
| − | === For === | + | ===For=== |
| | <source lang="java"> | | <source lang="java"> |
| | for(int i=0; i<5;i++){ | | for(int i=0; i<5;i++){ |
| | System.out.printf("The value of i is %d\n", i); | | System.out.printf("The value of i is %d\n", i); |
| | }</source> | | }</source> |
| − | === Do While === | + | ===Do While=== |
| | <source lang="java"> | | <source lang="java"> |
| | public static void main(String[] args) { | | public static void main(String[] args) { |
| Line 128: |
Line 150: |
| | }</source> | | }</source> |
| | | | |
| − | == Conditional == | + | ==Conditional== |
| − | === if === | + | ===if=== |
| | <source lang="java"> | | <source lang="java"> |
| | int value = 20; | | int value = 20; |
| Line 143: |
Line 165: |
| | }</source> | | }</source> |
| | | | |
| − | === switch case === | + | ===switch case=== |
| | <source lang="java"> | | <source lang="java"> |
| | switch(VariableIntOrString){ | | switch(VariableIntOrString){ |
| Line 158: |
Line 180: |
| | }</source> | | }</source> |
| | | | |
| − | == User Input == | + | ==User Input== |
| | <source lang="java"> | | <source lang="java"> |
| | import java.util.Scanner; | | import java.util.Scanner; |
| Line 181: |
Line 203: |
| | | | |
| | *Note: Ctrl + Shift + O in eclipse adds all the imports you need. | | *Note: Ctrl + Shift + O in eclipse adds all the imports you need. |
| − | == Arrays == | + | |
| | + | ==Arrays== |
| | <source lang="java"> | | <source lang="java"> |
| | int[] values; | | int[] values; |
| Line 190: |
Line 213: |
| | | | |
| | values.length; → returns the array lenght<br /> | | values.length; → returns the array lenght<br /> |
| − | === Iterating trough an array === | + | ===Iterating trough an array=== |
| | <source lang="java"> | | <source lang="java"> |
| | String[] fruits = {"apple", "banana", "pear", "kiwi"}; | | String[] fruits = {"apple", "banana", "pear", "kiwi"}; |
| Line 197: |
Line 220: |
| | } | | } |
| | </source> | | </source> |
| − | === Multidimensional arrays === | + | ===Multidimensional arrays=== |
| | <source lang="java"> | | <source lang="java"> |
| | int[][] grid = { | | int[][] grid = { |
| Line 208: |
Line 231: |
| | </source> | | </source> |
| | | | |
| − | == Classes and Objects == | + | ==Classes and Objects== |
| | <source lang="java"> | | <source lang="java"> |
| | class Person { | | class Person { |
| Line 235: |
Line 258: |
| | } | | } |
| | </source> | | </source> |
| − | == Setters and 'this' == | + | ==Setters and 'this'== |
| | <source lang="java"> | | <source lang="java"> |
| | package tutorial1; | | package tutorial1; |
| Line 275: |
Line 298: |
| | } | | } |
| | </source> | | </source> |
| − | == Constructors == | + | ==Constructors== |
| | Methods that are executed at the instantiation of a class.<br /> | | Methods that are executed at the instantiation of a class.<br /> |
| | There can be more than one, diferentiated on the number of parameters that are passed at the instantiation of a class.<br /> | | There can be more than one, diferentiated on the number of parameters that are passed at the instantiation of a class.<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/constructors.java example code] | | [https://github.com/rafahsolis/javaTutorial/blob/master/constructors.java example code] |
| | | | |
| − | == static == | + | ==static== |
| | static is used to define class variables and methods.<br /> | | static is used to define class variables and methods.<br /> |
| | static objects are referenced by the class, not the instance.<br /> | | static objects are referenced by the class, not the instance.<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/staticMethod.java example code] | | [https://github.com/rafahsolis/javaTutorial/blob/master/staticMethod.java example code] |
| | | | |
| − | == final == | + | ==final== |
| | final is used to declare constants. The asignment of value must be at the declaration<br /> | | final is used to declare constants. The asignment of value must be at the declaration<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/constructors.java example code] | | [https://github.com/rafahsolis/javaTutorial/blob/master/constructors.java example code] |
| | | | |
| − | == StringBuilder / formatting == | + | ==StringBuilder / formatting== |
| | StringBuilder is more efficient for strings appending than using += | | StringBuilder is more efficient for strings appending than using += |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/StringBuilderBufferFormatting.java example code] | | [https://github.com/rafahsolis/javaTutorial/blob/master/StringBuilderBufferFormatting.java example code] |
| | | | |
| − | == Inheritance == | + | ==Inheritance== |
| | Use key word extends at the class declaration<br /> | | Use key word extends at the class declaration<br /> |
| | private variables and methods are not inherited.<br /> | | private variables and methods are not inherited.<br /> |
| Line 305: |
Line 328: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Car.java Car.java] | | [https://github.com/rafahsolis/javaTutorial/blob/master/Car.java Car.java] |
| | | | |
| − | == Packages == | + | ==Packages== |
| | Packages allow to have classes with the same name as long as they are in diferent packages. <br /> | | Packages allow to have classes with the same name as long as they are in diferent packages. <br /> |
| | In orther to be able to use the classes from a package it must be imported.<br /> | | In orther to be able to use the classes from a package it must be imported.<br /> |
| Line 318: |
Line 341: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/packageexample.java packageexample.java] | | [https://github.com/rafahsolis/javaTutorial/blob/master/packageexample.java packageexample.java] |
| | | | |
| − | == Interfaces == | + | ==Interfaces== |
| | If you whant to define a method for more than one class, you should use an interface (rigt click on project > new > interface)<br /> | | If you whant to define a method for more than one class, you should use an interface (rigt click on project > new > interface)<br /> |
| | Interfaces file names should start with upper case.<br /> | | Interfaces file names should start with upper case.<br /> |
| Line 327: |
Line 350: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Info.java Info.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/Info.java Info.java]<br /> |
| | | | |
| − | == public/private/protected variables == | + | ==public/private/protected variables== |
| | public → can be accesed from anywhere.<br /> | | public → can be accesed from anywhere.<br /> |
| | public final static → constant.<br /> | | public final static → constant.<br /> |
| Line 334: |
Line 357: |
| | no access specifier → only accesible from the same package.<br /> | | no access specifier → only accesible from the same package.<br /> |
| | | | |
| − | == Polymorphism == | + | ==Polymorphism== |
| | You can point a variable of a parent type to a child type, it will have the methods from it's type but overriden by the subclass.<br /> | | You can point a variable of a parent type to a child type, it will have the methods from it's type but overriden by the subclass.<br /> |
| | You wont be able to call methods from the subclass that are not implemented on the variable type.<br /> | | You wont be able to call methods from the subclass that are not implemented on the variable type.<br /> |
| Line 342: |
Line 365: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Tree.java Tree.java] | | [https://github.com/rafahsolis/javaTutorial/blob/master/Tree.java Tree.java] |
| | | | |
| − | == Encapsulation and the API == | + | ==Encapsulation and the API== |
| | Variables whithin a class should be private and if you need to change them use getters and setters.<br /> | | Variables whithin a class should be private and if you need to change them use getters and setters.<br /> |
| | [https://docs.oracle.com/javase/8/docs/api/ Java 8 API] | | [https://docs.oracle.com/javase/8/docs/api/ Java 8 API] |
| | | | |
| − | == Casting Numerical Values == | + | ==Casting Numerical Values== |
| | See [https://github.com/rafahsolis/javaTutorial/blob/master/Casting.java examples] | | See [https://github.com/rafahsolis/javaTutorial/blob/master/Casting.java examples] |
| | | | |
| − | == Upcasting and Downcasting == | + | ==Upcasting and Downcasting== |
| | Code examples:<br /> | | Code examples:<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/UpcastingDowncasting.java UpcastingDowncasting.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/UpcastingDowncasting.java UpcastingDowncasting.java]<br /> |
| Line 355: |
Line 378: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Machine.java Machine.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/Machine.java Machine.java]<br /> |
| | | | |
| − | == Generic class == | + | ==Generic class== |
| | Can work with other objects specified at the class instantiation.<br /> | | Can work with other objects specified at the class instantiation.<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Generic.java Generic.java] | | [https://github.com/rafahsolis/javaTutorial/blob/master/Generic.java Generic.java] |
| Line 393: |
Line 416: |
| | } | | } |
| | </source> | | </source> |
| − | === Generics and Wildcards === | + | ===Generics and Wildcards=== |
| | See: [https://github.com/rafahsolis/javaTutorial/blob/master/Generic.java Generic.java] | | See: [https://github.com/rafahsolis/javaTutorial/blob/master/Generic.java Generic.java] |
| − | == Files == | + | ==Files== |
| − | === Reading text files === | + | ===Reading text files=== |
| | See: [https://github.com/rafahsolis/javaTutorial/blob/master/ReadTextFile.java ReadTextFile.java] | | See: [https://github.com/rafahsolis/javaTutorial/blob/master/ReadTextFile.java ReadTextFile.java] |
| − | === Reading files (Try-With-Resources) === | + | ===Reading files (Try-With-Resources)=== |
| | This requires at least Java 7<br /> | | This requires at least Java 7<br /> |
| | <source lang="java"> | | <source lang="java"> |
| Line 427: |
Line 450: |
| | } | | } |
| | }</source> | | }</source> |
| − | === Reading files with FileReader === | + | ===Reading files with FileReader=== |
| | <source lang="java"> | | <source lang="java"> |
| | import java.io.BufferedReader; | | import java.io.BufferedReader; |
| Line 467: |
Line 490: |
| | }</source> | | }</source> |
| | | | |
| − | === Writing files === | + | ===Writing files=== |
| | <source lang="java"> | | <source lang="java"> |
| | import java.io.BufferedWriter; | | import java.io.BufferedWriter; |
| Line 496: |
Line 519: |
| | }</source> | | }</source> |
| | | | |
| − | == Anonymous class == | + | ==Anonymous class== |
| | see: [https://github.com/rafahsolis/javaTutorial/blob/master/AnonymousClass.java AnonymousClass.java] | | see: [https://github.com/rafahsolis/javaTutorial/blob/master/AnonymousClass.java AnonymousClass.java] |
| − | == 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] |
| − | === trows === | + | ===trows=== |
| | <source lang="java"> | | <source lang="java"> |
| | package tutorialJava; | | package tutorialJava; |
| Line 519: |
Line 542: |
| | }</source> | | }</source> |
| | | | |
| − | === try/catch === | + | ===try/catch=== |
| | <source lang="java"> | | <source lang="java"> |
| | import java.io.IOException; | | import java.io.IOException; |
| Line 559: |
Line 582: |
| | } | | } |
| | }</source> | | }</source> |
| − | === Runtime exceptions === | + | ===Runtime exceptions=== |
| | The compiler does not force you to handle them. | | The compiler does not force you to handle them. |
| | <source lang="java"> | | <source lang="java"> |
| Line 586: |
Line 609: |
| | | | |
| | }</source> | | }</source> |
| − | == Abstract Class == | + | ==Abstract Class== |
| | Classes that can't be instantiated, only inherited.<br /> | | Classes that can't be instantiated, only inherited.<br /> |
| | See example files:<br /> | | See example files:<br /> |
| Line 593: |
Line 616: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/AbstractClassChild1.java AbstractClassChild1.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/AbstractClassChild1.java AbstractClassChild1.java]<br /> |
| | | | |
| − | == .equals() Method == | + | ==.equals() Method== |
| | It's used to compare if two objects of the same class are equal. In order to be able to use it you must override this method by:<br /> | | It's used to compare if two objects of the same class are equal. In order to be able to use it you must override this method by:<br /> |
| | Right click on the class > source > Generate hashCode() and Equals()...<br /> | | Right click on the class > source > Generate hashCode() and Equals()...<br /> |
| Line 601: |
Line 624: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Persona.java Persona.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/Persona.java Persona.java]<br /> |
| | | | |
| − | == Inner Classes == | + | ==Inner Classes== |
| | Clases declared inside other classes.<br /> | | Clases declared inside other classes.<br /> |
| | Code examples:<br /> | | Code examples:<br /> |
| Line 607: |
Line 630: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Robot.java Robot.java] | | [https://github.com/rafahsolis/javaTutorial/blob/master/Robot.java Robot.java] |
| | | | |
| − | == Recursion == | + | ==Recursion== |
| | See code example:<br /> | | See code example:<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Recursion.java Recursion.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/Recursion.java Recursion.java]<br /> |
| | | | |
| − | == Serialization == | + | ==Serialization== |
| | Turning an object into binary data, deserialization is the opposite.<br /> | | Turning an object into binary data, deserialization is the opposite.<br /> |
| | Code examples:<br /> | | Code examples:<br /> |
| Line 618: |
Line 641: |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/Persona.java Persona.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/Persona.java Persona.java]<br /> |
| | | | |
| − | === Multiple Objects === | + | ===Multiple Objects=== |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/WriteMultipleObjects.java WriteMultipleObjects.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/WriteMultipleObjects.java WriteMultipleObjects.java]<br /> |
| | [https://github.com/rafahsolis/javaTutorial/blob/master/ReadMultipleObjects.java ReadMultipleObjects.java]<br /> | | [https://github.com/rafahsolis/javaTutorial/blob/master/ReadMultipleObjects.java ReadMultipleObjects.java]<br /> |
| − | === Transient Keyword === | + | ===Transient Keyword=== |
| | To prevent the serialization of some variable in a class you can declare it as:<br /> | | To prevent the serialization of some variable in a class you can declare it as:<br /> |
| | <source lang="java"> | | <source lang="java"> |
| Line 627: |
Line 650: |
| | Static fields aren't serialized too. | | Static fields aren't serialized too. |
| | | | |
| − | == Eclipse shortcuts == | + | ==Eclipse shortcuts== |
| | Ctrl + Space → Autocomplete.<br /> | | Ctrl + Space → Autocomplete.<br /> |
| | Ctrl + d → Delete Line<br /> | | Ctrl + d → Delete Line<br /> |
| Line 634: |
Line 657: |
| | Ctrl + m → Toogle fulscreen<br /> | | Ctrl + m → Toogle fulscreen<br /> |
| | | | |
| − | == Notes == | + | ==Notes== |
| | Hibernate → Java ORM framework for web design | | Hibernate → Java ORM framework for web design |