Java Basic Syntax
Java Basic Syntax
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Taught by the author of Core Servlets and JSP, More Servlets and JSP, and this tutorial. tutorial Available at public venues, Customized or customized Java EE Training: versions http://courses.coreservlets.com/ can be held on-site at your Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. organization. Contact hall@coreservlets.com for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Basics
Customized Java EE Training: http://courses.coreservlets.com/
6
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
You can also copy/paste existing class, then give it new name
Details
Processing starts in main
Eclipse can create main automatically
When creating g class: choose main as option p Eclipse shortcut inside class: type main then hit Control-space
System.out.println, System.out.print, System.out.printf Eclipse shortcut: type sysout then hit Control-space
Compiling Executing
Eclipse: just save file DOS> javac HelloWorld.java Eclipse: R-click, Run As, Java Application DOS> java HelloWorld Hello, world.
More Basics
Use + for string concatenation Arrays are accessed with [ ]
Array indices are zero-based The argument to main is an array of strings that correspond to the command line arguments
args[0] returns first command-line argument args[1] [1] returns t second d command-line d li argument, t etc. t Error if you try to access more args than were supplied
Command-line Arguments
Are useful for learning and testing
Command-line args are helpful for practice But, programs given to end users should almost never use command-line arguments
They should pop up a GUI to collect input.
Example (Continued)
Compiling (automatic on save in Eclipse)
DOS> javac ShowTwoArgs.java
Manual execution
DOS> j java ShowTwoArgs Sh T A H ll Cl Hello Class First args Hello Second arg: Class DOS> java ShowTwoArgs [Error message]
To assign command line args: R-click, Run As, Run Configurations, click on Arguments tab
Loops
Customized Java EE Training: http://courses.coreservlets.com/
14
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Looping Constructs
for/each
for(variable: collection) { body; } for(init; continueTest; updateOp) { body; } while (continueTest) { body; } do { body; } while (continueTest);
15
for
while
do
For/Each Loops
public static void listEntries(String[] entries) { for(String entry: entries) { System.out.println(entry); } }
Result
String[] test = {"This", "is", "a", "test"}; listEntries(test); This is a test
16
For Loops
public static void listNums1(int max) { f (i t i for(int i=0; 0 i i<max; i++) { System.out.println("Number: " + i); } }
Result
listNums1(4); Number: Number: Number: Number:
17
0 1 2 3
While Loops
public static void listNums2(int max) { int i = 0; while (i < max) { System.out.println("Number: " + i); i++; // "++" means "add one" } }
Result
listNums2(5); ( ); Number: Number: Number: Number: Number: 0 1 2 3 4
18
Do Loops
public static void listNums3(int max) { i t i = 0 int 0; do { System.out.println("Number: y p ( " + i); ); i++; } while (i < max); // ^ Dont t f forget t semicolon i l }
Result
listNums3(3); Number: 0 Number: 1 Number: 2
19
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
No
blah; blah; for(...) { blah; blah; for(...) { bl h blah; blah; } }
Indentation: blocks that are nested the same should be indented the same
Yes
blah; blah; for(...) { blah; blah; for(...) { bl h blah; blah; } }
23
No
blah; blah; for(...) { blah; blah; for(...) { bl h blah; blah; } }
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
If Statements
Single option
if (boolean-expression) { statement1; ... statementN; } if (boolean-expression) { ... } else { ... }
T o options Two
Multiple options
if (boolean-expression) { ... } else if (boolean-expression) { ... } else if (boolean-expression) { ... } else { ... }
The value inside parens must strictly be boolean, unlike C, C++, and JavaScript. A widely accepted best practice is to use the braces even if there is s only yas single g s statement inside s the if or else. s
26
Switch Statements
Example
int month = ...; String monthString; switch(month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; ... g = "Invalid month"; ; break; ; default: monthString }
Boolean Operators
==, !=
E Equality, lit inequality. i lit In I addition dditi to t comparing i primitive i iti types, == tests if two objects are identical (the same object), not just if they appear equal (have the same fields) More details when we introduce objects. fields). objects Numeric less than, less than or equal to, greater than, greater t than th or equal l to. t Logical g AND, , OR. Both use short-circuit evaluation to more efficiently compute the results of complicated expressions. Logical negation.
Example: If Statements
public bli static t ti i int t max(int (i t n1, 1 i int t n2) 2) { if (n1 >= n2) { return(n1); } else { return(n2); } }
29
Strings
Basics
String is a real class in Java, not an array of characters as in C and C++. The String g class has a shortcut method to create a new object: j just use double quotes
This differs from normal objects, where you use the new construct to build an object
Arrays
Customized Java EE Training: http://courses.coreservlets.com/
32
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Examples:
int[] values = { 10 10, 100 100, 1000 }; String[] names = {"Joe", "Jane", "Juan"}; Point[] points = { new Point(0, 0), new Point(1, 2), new Point(3, 4) };
33
Default value is 0 for numeric arrays Default value is null for object arrays
Multidimensional Arrays
Multidimensional arrays y
Implemented as arrays of arrays
int[][] [][] twoD = new int[64][32]; [ ][ ] String[][] cats = {{ "Caesar", "blue-point" }, { "Heather", "seal-point" }, { "Ted" Ted , "red-point" red point }};
Note:
Number N b of f elements l t in i each h row need d not t be b equal l
int[][] irregular = { { { { {
37
1 }, 2 2, 3 3, 4} 4}, 5 }, 6, 7 } };
TriangleArray: Example
public class TriangleArray { public static void main(String[] args) { int[][] triangle = new int[10][]; for(int i=0; i<triangle.length; i++) { triangle[i] = new int[i+1]; } for (int i=0; i<triangle.length; i++) { for(int j=0; j<triangle[i].length; j++) { System out print(triangle[i][j]); System.out.print(triangle[i][j]); } System.out.println(); }
}
38
TriangleArray: Result
> java TriangleArray 0 00 000 0000 00000 000000 0000000 00000000 000000000 0000000000
39
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
random (Math.random() returns from 0 inclusive to 1 exclusive). See Random class for more control over randomization.
42
44
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
45
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Basics
L Loops, conditional di i l statements, and d array access is i similar i il to C and C++
But new for loop: for(String s: someStrings) { }
Use U Math. M th blah bl h() for f simple i l math th operations ti Simple input from command window
Use command line for strings supplied at program startup Use Scanner to read values after prompts
46
Questions?
Java 6 or 7, JSF 2.0, PrimeFaces, Servlets, JSP, Ajax, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.