1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
---
language: ColdFusion
contributors:
- ["Wayne Boka", "http://wboka.github.io"]
filename: LearnColdFusion.cfm
---
ColdFusion is a scripting language for web development.
[Read more here.](http://www.adobe.com/products/coldfusion-family.html)
```ColdFusion
<!--- Comments start with "<!---" and end with "--->" --->
<!---
Comments can
also
span
multiple lines
--->
<!--- CFML tags have a similar format to HTML tags. --->
<!--- Variable Declaration: Variables are loosely typed, similar to javascript --->
<cfset myVariable = "myValue" />
<cfset myNumber = 3.14 />
<!--- Displaying simple data --->
<!--- Use <cfoutput> for simple values such as strings, numbers, and expressions --->
<cfoutput>#myVariable#</cfoutput> <!--- myValue --->
<cfoutput>#myNumber#</cfoutput> <!--- myValue --->
<!--- Declaring complex variables --->
<!--- Declaring an array of 1 dimension: literal or bracket notation --->
<cfset myArray1 = [] />
<!--- Declaring an array of 1 dimension: function notation --->
<cfset myArray2 = ArrayNew(1) />
<!--- Outputting complex variables --->
<cfdump var="#myArray1#" /> <!--- An empty array object --->
<cfdump var="#myArray1#" /> <!--- An empty array object --->
<!--- Operators --->
<!--- Arithmetic --->
<cfoutput>#1 + 1#</cfoutput> = 2
<cfoutput>#10 - 8#</cfoutput> = 2
<cfoutput>#1 * 2#</cfoutput> = 2
<cfoutput>#10 / 5#</cfoutput> = 2
<cfoutput>#12 % 5#</cfoutput> = 0
<!--- Comparison --->
<cfoutput>#1 eq 1#</cfoutput> <!--- TRUE --->
<cfoutput>#15 neq 1#</cfoutput> <!--- TRUE --->
<cfoutput>#10 gt 8#</cfoutput> <!--- TRUE --->
<cfoutput>#1 lt 2#</cfoutput> <!--- TRUE --->
<cfoutput>#10 gte 5#</cfoutput> <!--- TRUE --->
<cfoutput>#1 lte 5#</cfoutput> <!--- TRUE --->
<!--- Control Structures --->
<cfset myCondition = "Test" />
<cfif myCondition eq "Test">
<cfoutput>#myCondition#</cfoutput>
<cfelseif myCondition eq "Production">
<cfoutput>#myCondition#. Proceed Carefully!!!</cfoutput>
<cfelse>
myCondition is unknown
</cfif>
```
<!-- // While loop
int fooWhile = 0;
while(fooWhile < 100) {
System.out.println(fooWhile);
// Increment the counter
// Iterated 100 times, fooWhile 0,1,2...99
fooWhile++;
}
System.out.println("fooWhile Value: " + fooWhile);
// Do While Loop
int fooDoWhile = 0;
do {
System.out.println(fooDoWhile);
// Increment the counter
// Iterated 99 times, fooDoWhile 0->99
fooDoWhile++;
} while(fooDoWhile < 100);
System.out.println("fooDoWhile Value: " + fooDoWhile);
// For Loop
int fooFor;
// for loop structure => for(<start_statement>; <conditional>; <step>)
for (fooFor = 0; fooFor < 10; fooFor++) {
System.out.println(fooFor);
// Iterated 10 times, fooFor 0->9
}
System.out.println("fooFor Value: " + fooFor);
// For Each Loop
// The for loop is also able to iterate over arrays as well as objects
// that implement the Iterable interface.
int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// for each loop structure => for (<object> : <iterable>)
// reads as: for each element in the iterable
// note: the object type must match the element type of the iterable.
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), 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";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
default: monthString = "Some other month";
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>"
int foo = 5;
String bar = (foo < 10) ? "A" : "B";
System.out.println(bar); // Prints A, because the statement is true
////////////////////////////////////////
// Converting Data Types And Typecasting
////////////////////////////////////////
// Converting data
// Convert String To Integer
Integer.parseInt("123");//returns an integer version of "123"
// Convert Integer To String
Integer.toString(123);//returns a string version of 123
// For other conversions check out the following classes:
// Double
// Long
// String
// Typecasting
// You can also cast Java objects, there's a lot of details and deals
// with some more intermediate concepts. Feel free to check it out here:
// http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
///////////////////////////////////////
// Classes And Functions
///////////////////////////////////////
System.out.println("\n->Classes & Functions");
// (definition of the Bicycle class follows)
// Use new to instantiate a class
Bicycle trek = new Bicycle();
// Call object methods
trek.speedUp(3); // You should always use setter and getter methods
trek.setCadence(100);
// 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 outer-level classes in a .java file
// Class Declaration Syntax:
// <public/private/protected> class <class name> {
// // 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 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 constructor
public Bicycle() {
gear = 1;
cadence = 50;
speed = 5;
name = "Bontrager";
}
// This is a constructor that takes arguments
public Bicycle(int startCadence, int startSpeed, int startGear,
String name) {
this.gear = startGear;
this.cadence = startCadence;
this.speed = startSpeed;
this.name = name;
}
// Function Syntax:
// <public/private/protected> <return type> <function name>(<args>)
// Java classes often implement getters and setters for their fields
// Method declaration syntax:
// <scope> <return type> <method name>(<args>)
public int getCadence() {
return cadence;
}
// void methods require no return statement
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void speedUp(int increment) {
speed += increment;
}
public void slowDown(int decrement) {
speed -= decrement;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
//Method to display the attribute values of this Object.
@Override
public String toString() {
return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
" name: " + name;
}
} // end class Bicycle
// PennyFarthing is a subclass of Bicycle
class PennyFarthing extends Bicycle {
// (Penny Farthings are those bicycles with the big front wheel.
// They have no gears.)
public PennyFarthing(int startCadence, int startSpeed){
// Call the parent constructor with super
super(startCadence, startSpeed, 0, "PennyFarthing");
}
// You should mark a method you're overriding with an @annotation.
// To learn more about what annotations are and their purpose check this
// out: http://docs.oracle.com/javase/tutorial/java/annotations/
@Override
public void setGear(int gear) {
gear = 0;
}
}
// 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 {
@Override
public void eat() {
// ...
}
@Override
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 {
@Override
public void InterfaceOneMethod() {
}
@Override
public void InterfaceTwoMethod() {
}
}
// Abstract Classes
// Abstract Class declaration syntax
// <access-level> abstract <abstract-class-name> extends <super-abstract-classes> {
// // Constants and variables
// // Method declarations
// }
// Methods can't have bodies in an interface, unless the method is
// static. Also variables are NOT final by default, unlike an interface.
// Also abstract classes CAN have the "main" method.
// Abstract classes solve these problems.
public abstract class Animal
{
public abstract void makeSound();
// Method can have a body
public void eat()
{
System.out.println("I am an animal and I am Eating.");
// Note: We can access private variable here.
age = 30;
}
// No need to initialize, however in an interface
// a variable is implicitly final and hence has
// to be initialized.
private int age;
public void printAge()
{
System.out.println(age);
}
// Abstract classes can have main function.
public static void main(String[] args)
{
System.out.println("I am abstract");
}
}
class Dog extends Animal
{
// Note still have to override the abstract methods in the
// abstract class.
@Override
public void makeSound()
{
System.out.println("Bark");
// age = 30; ==> ERROR! age is private to Animal
}
// NOTE: You will get an error if you used the
// @Override annotation here, since java doesn't allow
// overriding of static methods.
// What is happening here is called METHOD HIDING.
// Check out this awesome SO post: http://stackoverflow.com/questions/16313649/
public static void main(String[] args)
{
Dog pluto = new Dog();
pluto.makeSound();
pluto.eat();
pluto.printAge();
}
}
-->
## Further Reading
The links provided here below are just to get an understanding of the topic, feel free to Google and find specific examples.
|