|
Network Programming ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Lecture /
Introduction to Java ProgrammingA Network Programming Lecture by Steven Choy Lecture Overview: The Java Virtual machine - Different editions of Java - Install Java SE Development Kit (JDK) - A Simple Java Program - Primitive Data Types - Program Flow Controls - Object-oriented programming with Java
The Java virtual machine (JVM)
(Java bytecode can be executed on any platform with a Java virtual machine)
(A functional block diagram of JVM)
Different editions of JavaJava is split into three different editions, each targeting a different platform:
It includes the tools necessary to develop Java applications for desktop platforms.
It includes the tools necessary to develop robust, secure and scalable middleware applications for enterprises.
It includes the tools necessary to develop Java applications on portable or embedded devices such as mobile phones. A special version of virtual machine has been developed for Java ME due to restrictions usually found on the target platforms.
Install Java SE Development Kit (JDK)
A simple Java program/** * Hello World Demo */ class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
C:\>c:
C:\>cd temp
C:\temp>javac HelloWorld.java
C:\temp>java HelloWorld
C:\temp>java –classpath . HelloWorld
Hello World Primitive data types
Program flow controls
if (boolean expression)
statement or block of statements
else
statement or block of statements
switch (integer expression) {
case integer expression:
statement or block of statements
break;
...
default:
statement or block of statements
break;
}
// The if-then Statement
if (isMoving) {
currentSpeed--;
}
// The if-then-else Statement
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
// The switch Statement
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Invalid month.");break;
}
do {
statement or block of statements
} while (boolean expression);
while (boolean expression) {
statement or block of statements
}
for (initialization; boolean expression; step) {
statement or block of statements
}
for (type variable : array or collection) {
statement or block of statements
}
// The do-while Statement
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count <= 11);
// The while and do-while Statements
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
// The for Statement
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
The for-each loop, also called enhanced for loop, is a new addition to the flow controls since Java 5. It provides a convenient way to iterate through the elements of an array or a collection. For example, the following code calculates the total of integers in an array:
int[] arr = { 12, 4, 23, 5 };
int total = 0;
for (int i : arr) {
total += i;
}
System.out.println("Total = " + total);
Object-oriented programming with Java
Java and Network ProgrammingWe will use Java as one of programming languages to write network programs.
We will learn more about Java programming in the course of studying network programming.
So, don't worry if you can't understand all the topics about Java programming.
Some basic topics that are important to you are:
An Example Java Network Programimport java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; public class Post2WebTest { public static void main(String[] args) { String postUrl = ""; String postData = ""; try { URL url = new URL(postUrl); URLConnection conn = url.openConnection(); conn.setDoOutput(true); //conn.setRequestProperty("Referer", ""); // write POST data to server OutputStream output = conn.getOutputStream(); output.write(postData.getBytes()); output.close(); // read response from server BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) System.out.println(line); } catch (IOException ex) { ex.printStackTrace(); } } } Homework for youReadings: The Java Tutorial (http://download.oracle.com/javase/tutorial/)
This online reading contains a fair amount of material. Prepare to spend an additional four hours on the examples and exercises. Note that the reading covers some concepts that will be discussed again later. Use the reading as a preparation for this.
Thanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. If you find any problem in this lecture note, please feel free to tell Steven via steven@findaway.hk. |