mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Merge pull request #411 from adambrenecki/java-errors
[java/en] Fix errors specified in #410.
This commit is contained in:
commit
ad848fe6ca
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user