summaryrefslogtreecommitdiffhomepage
path: root/java.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'java.html.markdown')
-rw-r--r--java.html.markdown106
1 files changed, 72 insertions, 34 deletions
diff --git a/java.html.markdown b/java.html.markdown
index 418bd649..a6026651 100644
--- a/java.html.markdown
+++ b/java.html.markdown
@@ -15,6 +15,10 @@ Java is a general-purpose, concurrent, class-based, object-oriented computer pro
/*
Multi-line comments look like this.
*/
+/**
+JavaDoc comments look like this. Used to describe the Class or various
+attributes of a Class.
+*/
// Import ArrayList class inside of the java.util package
import java.util.ArrayList;
@@ -59,11 +63,15 @@ public class LearnJava {
// Long - 64-bit signed two's complement integer
// (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
long fooLong = 100000L;
+ // L is used to denote that this variable value is of type Long;
+ // anything without is treated as integer by default.
- // (Java has no unsigned types)
+ // Note: Java has no unsigned types
// Float - Single-precision 32-bit IEEE 754 Floating Point
float fooFloat = 234.5f;
+ // f is used to denote that this variable value is of type float;
+ // otherwise it is treated as double.
// Double - Double-precision 64-bit IEEE 754 Floating Point
double fooDouble = 123.4;
@@ -83,8 +91,11 @@ public class LearnJava {
// \n is an escaped character that starts a new line
String barString = "Printing on a new line?\nNo Problem!";
+ // \t is an escaped character that adds a tab character
+ String bazString = "Do you want to add a tab?\tNo Problem!";
System.out.println(fooString);
System.out.println(barString);
+ System.out.println(bazString);
// Arrays
//The array size must be decided upon declaration
@@ -128,12 +139,12 @@ public class LearnJava {
System.out.println("11%3 = "+(11 % 3)); // => 2
// Comparison operators
- System.out.println("3 == 2? " + (3 == 2)); // => 0 (false)
- System.out.println("3 != 2? " + (3 != 2)); // => 1 (true)
- System.out.println("3 > 2? " + (3 > 2)); // => 1
- System.out.println("3 < 2? " + (3 < 2)); // => 0
- System.out.println("2 <= 2? " + (2 <= 2)); // => 1
- System.out.println("2 >= 2? " + (2 >= 2)); // => 1
+ System.out.println("3 == 2? " + (3 == 2)); // => false
+ System.out.println("3 != 2? " + (3 != 2)); // => true
+ System.out.println("3 > 2? " + (3 > 2)); // => true
+ System.out.println("3 < 2? " + (3 < 2)); // => false
+ System.out.println("2 <= 2? " + (2 <= 2)); // => true
+ System.out.println("2 >= 2? " + (2 >= 2)); // => true
// Bitwise operators!
/*
@@ -147,7 +158,7 @@ public class LearnJava {
*/
// Incrementations
- int i=0;
+ 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
@@ -160,12 +171,13 @@ public class LearnJava {
System.out.println("\n->Control Structures");
// If statements are c-like
- if (false){
- System.out.println("I never run");
- }else if (false) {
- System.out.println("I am also never run");
+ int j = 10;
+ if (j == 10){
+ System.out.println("I get printed");
+ } else if (j > 10) {
+ System.out.println("I don't");
} else {
- System.out.println("I print");
+ System.out.println("I also don't");
}
// While loop
@@ -200,16 +212,24 @@ public class LearnJava {
System.out.println("fooFor Value: " + fooFor);
// Switch Case
+ // A switch works with the byte, short, char, and int data types.
+ // It also works with enumerated types (discussed in Enum Types),
+ // the String class, and a few special classes that wrap
+ // primitive types: Character, Byte, Short, and Integer.
int month = 3;
String monthString;
switch (month){
- case 1: monthString = "January";
+ case 1:
+ monthString = "January";
break;
- case 2: monthString = "February";
+ case 2:
+ monthString = "February";
break;
- case 3: monthString = "March";
+ case 3:
+ monthString = "March";
break;
- default: monthString = "Some other month";
+ default:
+ monthString = "Some other month";
break;
}
System.out.println("Switch Case Result: " + monthString);
@@ -251,10 +271,10 @@ public class LearnJava {
Bicycle trek = new Bicycle();
// Call object methods
- trek.speedUp(3);
+ trek.speedUp(3); // You should always use setter and getter methods
trek.setCadence(100);
- // toString is a convention
+ // toString is a convention to display the value of this Object.
System.out.println("trek info: " + trek.toString());
} // End main method
@@ -266,15 +286,17 @@ public class LearnJava {
// Class Declaration Syntax:
// <public/private/protected> class <class name>{
-// //data fields, constructors, functions all inside
+// //data fields, constructors, functions all inside.
+// //functions are called as methods in Java.
// }
class Bicycle {
// Bicycle's Fields/Variables
public int cadence; // Public: Can be accessed from anywhere
- private int speed; // Private: Only accessable from within the class
+ private int speed; // Private: Only accessible from within the class
protected int gear; // Protected: Accessible from the class and subclasses
+ String name; // default: Only accessible from within this package
// Constructors are a way of creating classes
// This is a default constructor
@@ -282,13 +304,15 @@ class Bicycle {
gear = 1;
cadence = 50;
speed = 5;
+ name = "Bontrager";
}
// This is a specified constructor (it contains arguments)
- public Bicycle(int startCadence, int startSpeed, int startGear) {
- gear = startGear;
- cadence = startCadence;
- speed = startSpeed;
+ public Bicycle(int startCadence, int startSpeed, int startGear, String name) {
+ this.gear = startGear;
+ this.cadence = startCadence;
+ this.speed = startSpeed;
+ this.name = name;
}
// Function Syntax:
@@ -319,10 +343,21 @@ class Bicycle {
speed -= decrement;
}
+ public void setName(int newName) {
+ name = newName;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ //Method to display the attribute values of this Object.
+ @Override
public String toString() {
- return "gear: "+Integer.toString(gear)+
- " cadence: "+Integer.toString(cadence)+
- " speed: "+Integer.toString(speed);
+ return "gear: " + gear +
+ " cadence: " + cadence +
+ " speed: " + speed +
+ " name: " + name;
}
} // end class Bicycle
@@ -333,7 +368,7 @@ class PennyFarthing extends Bicycle {
public PennyFarthing(int startCadence, int startSpeed){
// Call the parent constructor with super
- super(startCadence, startSpeed, 0);
+ super(startCadence, startSpeed, 0, "PennyFarthing");
}
// You should mark a method you're overriding with an @annotation
@@ -350,13 +385,18 @@ class PennyFarthing extends Bicycle {
## 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:
-* [Inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
+* [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)
-* [Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
+* [Java Access level modifiers](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)
-* [Abstraction](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)
+* [Object-Oriented Programming Concepts](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
+ * [Inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
+ * [Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
+ * [Abstraction](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)
* [Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)
@@ -365,5 +405,3 @@ 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)
-
-* The links provided are just to get an understanding of the topic, feel free to google and find specific examples