From 995013c92119395564d303a2b6b5e67fcb66a5fc Mon Sep 17 00:00:00 2001 From: Madison Dickson Date: Wed, 4 Sep 2013 11:30:44 -0400 Subject: Including for each loop example --- java.html.markdown | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index cdcf620c..a2fc3630 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -210,7 +210,19 @@ public class LearnJava { //Iterated 10 times, fooFor 0->9 } 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( : ) + //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), -- cgit v1.2.3 From 338862e732e82a45fbd8843d48492b4bd3121c0b Mon Sep 17 00:00:00 2001 From: Madison Dickson Date: Fri, 13 Sep 2013 11:58:46 -0400 Subject: Added '?' conditional logic example Also added myself as a contributor. --- java.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index a2fc3630..0dec51d1 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 --- @@ -245,6 +246,13 @@ public class LearnJava { break; } 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 , otherwise, use " + int foo = 5 + String bar = (foo < 10) ? "A" : "B"; + System.out.println(bar); // Prints A, because the statement is true /////////////////////////////////////// -- cgit v1.2.3 From 925ded87809a5ae9f40a3c825324cb083eaba81e Mon Sep 17 00:00:00 2001 From: Christos Kontas Date: Tue, 8 Oct 2013 19:09:12 +0300 Subject: [java] Missing semi-colon at code example --- java.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 0dec51d1..3d0cb1d7 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -250,7 +250,7 @@ public class LearnJava { // Conditional Shorthand // You can use the '?' operator for quick assignments or logic forks. // Reads as "If (statement) is true, use , otherwise, use " - int foo = 5 + int foo = 5; String bar = (foo < 10) ? "A" : "B"; System.out.println(bar); // Prints A, because the statement is true -- cgit v1.2.3 From 8121793f32c36b6727474c6d41f31c27cea2ebf0 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Sat, 16 Nov 2013 19:40:06 +1030 Subject: Fix errors specified in #410. --- java.html.markdown | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'java.html.markdown') diff --git a/java.html.markdown b/java.html.markdown index 3d0cb1d7..b4624d5e 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -26,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 @@ -84,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 @@ -99,7 +100,7 @@ public class LearnJava { System.out.println(bazString); // Arrays - //The array size must be decided upon declaration + //The array size must be decided upon instantiation //The format for declaring an array is follows: // [] = new []; int [] intArray = new int[10]; @@ -161,10 +162,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 @@ -211,19 +215,19 @@ public class LearnJava { //Iterated 10 times, fooFor 0->9 } 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( : ) //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), @@ -246,7 +250,7 @@ public class LearnJava { break; } 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 , otherwise, use " @@ -294,14 +298,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: @@ -319,7 +323,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; @@ -327,7 +331,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; -- cgit v1.2.3