diff options
Diffstat (limited to 'java.html.markdown')
-rw-r--r-- | java.html.markdown | 114 |
1 files changed, 99 insertions, 15 deletions
diff --git a/java.html.markdown b/java.html.markdown index b4531635..50875491 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -3,6 +3,7 @@ language: java contributors: - ["Jake Prather", "http://github.com/JakeHP"] + - ["Madison Dickson", "http://github.com/mix3d"] filename: LearnJava.java --- @@ -25,7 +26,8 @@ import java.util.ArrayList; // Import all classes inside of java.security package import java.security.*; -// Each .java file contains one public class, with the same name as the file. +// Each .java file contains one outer-level public class, with the same name as +// the file. public class LearnJava { // A program must have a main method as an entry point @@ -83,7 +85,7 @@ public class LearnJava { // Char - A single 16-bit Unicode character char fooChar = 'A'; - // Use final to make a variable immutable + // final variables can't be reassigned to another object final int HOURS_I_WORK_PER_WEEK = 9001; // Strings @@ -98,15 +100,18 @@ public class LearnJava { System.out.println(bazString); // Arrays - //The array size must be decided upon declaration - //The format for declaring an array is follows: + //The array size must be decided upon instantiation + //The following formats work for declaring an arrow //<datatype> [] <var name> = new <datatype>[<array size>]; + //<datetype> <var name>[] = new <datatype>[<array size>]; int [] intArray = new int[10]; String [] stringArray = new String[1]; - boolean [] booleanArray = new boolean[100]; + boolean boolArray [] = new boolean[100]; // Another way to declare & initialize an array int [] y = {9000, 1000, 1337}; + String names [] = {"Bob", "John", "Fred", "Juan Pedro"}; + boolean bools[] = new boolean[] {true, false, false}; // Indexing an array - Accessing an element System.out.println("intArray @ 0: " + intArray[0]); @@ -160,10 +165,13 @@ public class LearnJava { // Incrementations int i = 0; System.out.println("\n->Inc/Dec-rementation"); - System.out.println(i++); //i = 1. Post-Incrementation - System.out.println(++i); //i = 2. Pre-Incrementation - System.out.println(i--); //i = 1. Post-Decrementation - System.out.println(--i); //i = 0. Pre-Decrementation + // The ++ and -- operators increment and decrement by 1 respectively. + // If they are placed before the variable, they increment then return; + // after the variable they return then increment. + System.out.println(i++); //i = 1, prints 0 (post-increment) + System.out.println(++i); //i = 2, prints 2 (pre-increment) + System.out.println(i--); //i = 1, prints 2 (post-decrement) + System.out.println(--i); //i = 0, prints 0 (pre-decrement) /////////////////////////////////////// // Control Structures @@ -186,7 +194,7 @@ public class LearnJava { { //System.out.println(fooWhile); //Increment the counter - //Iterated 99 times, fooWhile 0->99 + //Iterated 100 times, fooWhile 0,1,2...99 fooWhile++; } System.out.println("fooWhile Value: " + fooWhile); @@ -211,6 +219,18 @@ public class LearnJava { } System.out.println("fooFor Value: " + fooFor); + // For Each Loop + // An automatic iteration through an array or list of objects. + int[] fooList = {1,2,3,4,5,6,7,8,9}; + //for each loop structure => for(<object> : <array_object>) + //reads as: for each object in the array + //note: the object type must match the array. + + for( int bar : fooList ){ + //System.out.println(bar); + //Iterates 9 times and prints 1-9 on new lines + } + // Switch Case // A switch works with the byte, short, char, and int data types. // It also works with enumerated types (discussed in Enum Types), @@ -234,6 +254,13 @@ public class LearnJava { } System.out.println("Switch Case Result: " + monthString); + // Conditional Shorthand + // You can use the '?' operator for quick assignments or logic forks. + // Reads as "If (statement) is true, use <first value>, otherwise, use <second value>" + int foo = 5; + String bar = (foo < 10) ? "A" : "B"; + System.out.println(bar); // Prints A, because the statement is true + /////////////////////////////////////// // Converting Data Types And Typcasting @@ -274,14 +301,14 @@ public class LearnJava { trek.speedUp(3); // You should always use setter and getter methods trek.setCadence(100); - // toString is a convention to display the value of this Object. + // toString returns this Object's string representation. System.out.println("trek info: " + trek.toString()); } // End main method } // End LearnJava class -// You can include other, non-public classes in a .java file +// You can include other, non-public outer-level classes in a .java file // Class Declaration Syntax: @@ -299,7 +326,7 @@ class Bicycle { String name; // default: Only accessible from within this package // Constructors are a way of creating classes - // This is a default constructor + // This is a constructor public Bicycle() { gear = 1; cadence = 50; @@ -307,7 +334,7 @@ class Bicycle { name = "Bontrager"; } - // This is a specified constructor (it contains arguments) + // This is a constructor that takes arguments public Bicycle(int startCadence, int startSpeed, int startGear, String name) { this.gear = startGear; this.cadence = startCadence; @@ -381,13 +408,53 @@ class PennyFarthing extends Bicycle { } +//Interfaces +//Interface declaration syntax +//<access-level> interface <interface-name> extends <super-interfaces> { +// //Constants +// //Method declarations +//} + +//Example - Food: +public interface Edible { + public void eat(); //Any class that implements this interface, must implement this method +} + +public interface Digestible { + public void digest(); +} + + +//We can now create a class that implements both of these interfaces +public class Fruit implements Edible, Digestible { + public void eat() { + //... + } + + public void digest() { + //... + } +} + +//In java, you can extend only one class, but you can implement many interfaces. +//For example: +public class ExampleClass extends ExampleClassParent implements InterfaceOne, InterfaceTwo { + public void InterfaceOneMethod() { + + } + + public void InterfaceTwoMethod() { + + } +} + ``` ## Further Reading The links provided here below are just to get an understanding of the topic, feel free to Google and find specific examples. -Other Topics To Research: +**Official Oracle Guides**: * [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html) @@ -405,3 +472,20 @@ Other Topics To Research: * [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html) * [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html) + +**Online Practice and Tutorials** + +* [Learneroo.com - Learn Java](http://www.learneroo.com) + +* [Codingbat.com](http://codingbat.com/java) + + +**Books**: + +* [Head First Java](http://www.headfirstlabs.com/books/hfjava/) + +* [Objects First with Java](http://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660) + +* [Java The Complete Reference](http://www.amazon.com/gp/product/0071606300) + + |