diff options
author | Adam Bard <github@adambard.com> | 2013-11-16 12:53:04 -0800 |
---|---|---|
committer | Adam Bard <github@adambard.com> | 2013-11-16 12:53:04 -0800 |
commit | ad848fe6ca71a9b5621dbadb763db7bfd6c6558b (patch) | |
tree | fd68d2ae89539286de1ff44f3d3cc311a83d293b | |
parent | 13822f9c154883987eb6383576482b0bd1bf4431 (diff) | |
parent | 8121793f32c36b6727474c6d41f31c27cea2ebf0 (diff) |
Merge pull request #411 from adambrenecki/java-errors
[java/en] Fix errors specified in #410.
-rw-r--r-- | java.html.markdown | 34 |
1 files changed, 19 insertions, 15 deletions
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: //<datatype> [] <var name> = new <datatype>[<array size>]; 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(<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), @@ -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 <first value>, otherwise, use <second value>" @@ -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; |