Crash Course in Java: - Network Programming in Java Is Very Different Than in C/C++
Crash Course in Java: - Network Programming in Java Is Very Different Than in C/C++
Why Java?
Network Programming in Java is very different than in C/C++
much more language support error handling no pointers! (garbage collection) Threads are part of the language. some support for common application level protocols (HTTP).
Netprog: Java Intro 2
To print to stdout:
System.out.println();
javac Simp.java
compile
source code
java Simp
Simp.class
bytecode
The Language
Data types Operators Control Structures Classes and Objects Packages
int!
boolean true or false char unicode! (16 bits) byte signed 8 bit integer short signed 16 bit integer int signed 32 bit integer long signed 64 bit integer float,double IEEE 754 floating point
Netprog: Java Intro 9
strings are supported by a built-in class named String string literals are supported by the language (as a special case).
Netprog: Java Intro 10
Operators
Assignment: =, +=, -=, *=, Numeric: +, -, *, /, %, ++, --, Relational: ==. !=, <, >, <=, >=, Boolean: &&, ||, ! Bitwise: &, |, ^, ~, <<, >>, Just like C/C++!
Netprog: Java Intro 11
Control Structures
More of what you expect: conditional: if, if else, switch loop: while, for, do break and continue (but a little different than with C/C++).
12
Exceptions
Terminology:
throw an exception: signal that some condition (possibly an error) has occurred. catch an exception: deal with the error (or whatever).
Try/Catch/Finally
try { // some code that can throw // an exception } catch (ExceptionType1 e1) { // code to handle the exception } catch (ExceptionType2 e2) { // code to handle the exception } finally { // code to run after the stuff in try // can handle other exception types } Netprog: Java Intro 14
A little hard to get used to, but forces the programmer to be aware of what errors can occur and to deal with them.
Netprog: Java Intro 15
synchronized as a modifier
You can also declare a method as synchronized:
synchronized int blah(String x) { // blah blah blah }
17
Defining a Class
One top level public class per .java file.
typically end up with many .java files for a single program. One (at least) has a static public main() method.
Sample Class
(from Java in a Nutshell)
public class Point { public double x,y; public Point(double x, double y) { this.x = x; this.y=y; } public double distanceFromOrigin(){ return Math.sqrt(x*x+y*y); } }
Netprog: Java Intro 20
but this doesnt create the object! You have to use new:
Point p = new Point(3.1,2.4);
Using objects
Just like C++:
object.method() object.field
Arrays
Arrays are supported as a second kind of reference type (objects are the other reference type). Although the way the language supports arrays is different than with C++, much of the syntax is compatible.
however, creating an array requires new
Netprog: Java Intro 24
Notes on Arrays
index starts at 0. arrays cant shrink or grow. each element is initialized. array bounds checking (no overflow!)
ArrayIndexOutOfBoundsException
Reference Types
Objects and Arrays are reference types Primitive types are stored as values. Reference type variables are stored as references (pointers that we cant mess with). There are significant differences!
26
27
Packages
You can organize a bunch of classes into a package.
defines a namespace that contains all the classes.
10