This document provides an overview of basic Java training. It covers:
- Setting up a development environment including choosing an editor, installing the JDK, and configuring a terminal emulator.
- Java language fundamentals such as variables, operators, control flow statements, and packages.
- Classes and objects including access modifiers, constructors, static vs instance members, and initializing fields.
- Other concepts like naming conventions, coding style, and annotations.
- Exercises are provided to practice each topic. Homework includes completing an online Java course and writing an address book program.
4. DEVELOPMENT SETUP
Editor
Choose anyone you prefer
But do not rush to IDE
Make sure you’ve killed “tab”
JDK
JDK not JRE
6 not 7
Terminal Emulator
Unix, Linux, Mac, but sorry M$
6. CODING STYLE
Similar with C++
Space instead of tab
Always make block with {}
Already defined for EMA development (Eclipse):
Get it here: /proj/EMA/env/formatter.xml
Or install the all-in-one shot
9. VARIABLES
Primitive Types
byte (0)
short (0)
int (0)
long (0L)
float (0.0f)
double (0.0d)
boolean (false)
char (‘u0000’)
Object
Array
is an object
must be initialized
primitive/object
Value or References
15. CONTROL FLOW
STATEMENTS
Enhanced for statement
Never use labeled break/continue
Avoid continue and break
Always use {} to clarify blocks
16. EXERCISES
Enhanced for statement
Write a small program to give number of days of a
specified month
Read from standard input
Input like “1”, “2”, “3”, “quit”; if “quit”, terminate
18. PACKAGE
Naming Conventions
java and javax are reserved by Java language itself
domain name + product name
Importing Package
import com.ericsson.ma.javatraining.HelloWorld;
import static com.ericsson.ma.javatraining.HelloWorld.say;
import com.ericsson.ma.javatraining.*;
Managing Source and Class Files
Not required to rely on file system to manage source and class files
As a universal convention, please rely on file system
19. EXERCISES
Refactor three classes into three different packages
com.ericsson.ma.javatraining.Server => c.e.m.j.s
com.ericsson.ma.javatraining.Utilities => c.e.m.j.u
com.ericsson.ma.javatraining.Client => c.e.m.j.c
25. EXERCISES
Write a few classes to represent Resource, Tester, Designer
Common fields like id, gender, name, unit
Test can test while designer can design, so the behaviors of working
are different
The following code creates one array and one string object. How many
references to those objects exist after the code executes? Is either
object eligible for garbage collection?
...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...
27. ANNOTATION
Information for the Compiler
Used by compiler to detect errors and suppress warnings
Compiler-time and Deployment-time Processing
Software tools can process annotation to generate XML,
files, and so on
Runtime Processing
Available to be examined at runtime
29. HOMEWORK
Finish all sections of http://t.cn/Ste59J
Strongly recommend http://t.cn/SteofW
Write an address book program
Store name, address, phone number
Possbile to search by phone number, either
complete or partial
Editor's Notes
\n
- everything from stratch\n- default package, HelloWorld.java\n > main\n > pure OO\n- named package\n\n
\n
\n
\n
\n
\n
try to explain these in HelloWorld.java\n\nfield concept\n
- show literals\n- show array usage\n- ArrayCopyDemo without print out for exercise\n
- name like 1test\n- field default values, local variable default values\n
\n
write examples to show equals and ==\n\nString a = “hello”;\nString b = “hello”;\n\na.equals(b);\na == b\n\nString a = new String(“hello”);\nString b = new String(“hello”);\n\na.equals(b);\na == b\n
\n
\n
for (int a : array) {\n}\n
\n
\n
describe when to use static import and not overuse it\n
\n
\n
there is no multi-inherits\n\nshape, rectangle, triangle to show class hierarchy\n