39
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical and Electronic Engineering, University of Western Australia rs of this course wish to disclaim responsibility this corny part o John Morris Peter Jones

Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Embed Size (px)

Citation preview

Page 1: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Java ProgrammingThe Language of Now & the Future*

Lecture 0An Introduction to Java Syntax for Non-C

Programmers

John MorrisDepartment of Electrical and Electronic Engineering,

University of Western Australia

*The presenters of this course wish to disclaim responsibility this corny part of the title!

JohnMorris

PeterJones

Page 2: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Program

• Object Oriented Design• Objects and Classes• Inheritance• Interfaces

• Java Programs and Applets• Graphical User Interfaces• Swing• Windows, Frames, Menus, ...

• Practical Session

Week 2

Week 1

Week 3

Page 3: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Program

• Object Oriented Design• Objects and Classes• Inheritance• Interfaces

• Java Programs and Applets• Graphical User Interfaces• Swing• Windows, Frames, Menus, ...

• Practical Session

Week 5

Week 4

Week 6

Page 4: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

Page 5: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Boolean • boolean• Values• true• false

• Not in C!

Page 6: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Character• char• Unicode• Uses two bytes• Designed for “internationalisation”• More later!

• Literals• Single quotes• ‘Ordinary’ (printing) characters

• ‘a’, ‘A’, ‘!’, ‘1’, ...

• Special• Hexadecimal form

• ‘\u0008’ (Unicode backspace)

Page 7: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Character• char• Literals• Non-printing characters

• Use \ as escape character

• ‘\b’ backspace• ‘\t’ tab• ‘\n’ linefeed• ‘\r’ return• ‘\”’ double quote• ‘\’’ single quote• ‘\\’ backslash itself!

Page 8: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Integers• Many flavours (just like C )

• int 4 bytes• short 2 bytes• long 8 bytes• byte 1 bytes (not in C)

(Use in low level file handling

char is Unicode!) • Literals

• [ sign ] { decimal digits }: 1, +1, -1, 9999, ....• L suffix for long: 123456789122L• 0x prefix for hexadecimal: 0x234ab

Page 9: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Floating point• Two flavours (as C)

• float 4 bytes• double 8 bytes • Literals

• [+|-] { dec digit }.[ { dec digit } ] [e|E] [+|-] { dec digit } • 1. 1.0 1.E+6 1.2e-6• -1.3e-06 1.896

Page 10: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Arrays• Rather different from C!• int [] intArray;...intArray = new int[n];

• double [] dlist = new double[n];• Initialised arraysint[] primes = { 2, 3, 5, 7, 11 };

• Array attributes int n = primes.length;

• More later!

Page 11: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Primitive Types

• Constants• No preprocessor (as C)!

• Use final keyword

• static final int n = 100;int [] intArray;...intArray = new int[n];double [] dlist = new double[n];

Page 12: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

• Block delimited by{ }

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

1 2

Page 13: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Declarations

• type_name variable_name_list ;• boolean ok, finished;• char tab, separator;• int k, j;• byte c, d;• float x, y;• double a, b;• May occur anywhere in a block

• C: must be at head of block

• Scope• Valid within the block

Page 14: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

• Compilable programunit

• class• one or more classes

per file

• package• several classes• used for making libraries

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

Page 15: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Methods (functions)

• [ public | private ] return_type name ( parameter_list ) block

• public int count( double a ) { some_statements; }

• public - accessible outside the class

• private - accessible inside the class only

• Some other qualifiers (later!)• static, protected, synchronized

Page 16: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Methods (functions)

• [ public | private ] return_type name ( parameter_list ) block

• public int count( double a ) { some_statements; }

• return_type - any Java type or class

• void - method doesn’t return a value but may update an object

Page 17: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Methods (functions)

• [ public | private ] return_type name ( parameter_list ) block

• public int count( double a ) { some_statements; }

• name - any legal name• must begin with a character• letters, numbers, _

• Convention• setValue, raiseSalary, printTable• common, but not mandatory!

Page 18: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Methods (functions)

• [ public | private ] return_type name ( parameter_list ) block• public int count( double a ) { some_statements; }

• parameter_list - • comma separated list

• type name pairs• void setPoint( double from, double to );• void update( Graphics g );• void setNewValues( int[] a );

Page 19: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Methods (functions)

• [ public | private ] return_type name ( parameter_list ) block• public int count( double a ) { some_statements; }

• block - • delimited by { }

• contains sequences of declarations & statements

• ; is terminator• not separator (as some languages)

Page 20: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Statements

• Assignment• l_value = expression

• a = b;x = y = 0.0;z = f( x, a ) * g( y, b );

• Statements are expressions

Page 21: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators• = assignment operator+, -, *, / arithmetic operators% modulus (remainder)! complement<<, >> left, right shift&, |, ^ arithmetic and, or, xor<, <=, >, >= relational== relational eq&&, || boolean and, or++, -- increment, decrement?: if(x) a else b

Page 22: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators•<<, >> left, right shifta << 1 multiplies a by 2b >> 2 divide b by 4x << n x * 2n

y >> k y / 2k

Page 23: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators• &, |, ^ arithmetic and, or, xora & b bitwise and of a and ba | b bitwise orx ^ y x xor y

• Care• a && b• a & b

• Both legal, very different meaning!

Page 24: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators•== relational equalif ( a == b ) .. notif ( a = b ) assignment operator!

Page 25: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators• &&, || boolean and, orif ( (a == b) && (x != y) ) ... again notif ( (a=b) & (x=y) ) ..

arithmetic operators!

Page 26: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators• ++, -- increment, decrement

x++ use x, then increment y-- use y, then decrement --x decrement x, then use it ++y increment y, then use it

• Postfix and prefix operators are quite different!

Page 27: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Expressions

• Operators•?: if(x) a else b

(a==b)?x:y if( a== b) x else y(a>b)?a:b max of a and b

Page 28: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Statements

• IF•if( exp ) statement1;else statement2;

if (a>b) x = a; else x = b;

else is optional if ( x[i] > max ) max = x[i];

Page 29: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Statements

• Loops• whilewhile( exp ) statement1;while( exp ) { statements; }

while (a>b) a = x[i++]; while ( x < 0 ) { x = getX( ... ); y = y + x; }

• while is zero-trip

Page 30: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Statements

• Loops• dodo statement; while( exp ); do { statements; } while( exp );

do a = x[i++]; while( a>z );• do { x = getX( ... ); y = y + x; } while ( x > 0 );

• do is one-trip

Page 31: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Statements - Loops

• forfor( exp1; exp2; exp3 ) { s; }

• equivalent to: exp1; while ( exp2 ) { s; exp3; }

• for( k=0; k<n; k++ ) { s; }

k=0;while( k<n ) { s; k++; }

• Standard pattern for n iterations!

Page 32: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Statements - switch

• switch( exp1 ) { case x1: s1; break; case x2: s2; break; default: s3; }

• switch( x ) { case 1: y = a; break; case 2: y = b; break; default: y = c; }

Page 33: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Strings

• Standard class• Package: java.lang

• Literals• “abcd”• “123”

Page 34: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Objects for primitive data types

• Wrappers• Make objects from standard data types• java.lang

•Boolean•Integer•Long•Character•Float•Double

Page 35: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

class Rectangle {

private double width, height; // attributes

....... }

Class name

Page 36: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

class Rectangle {

private double width, height; // attributes

....... }

Class name

Block delimiters

Page 37: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

class Rectangle {

private double width, height; // attributes

....... }

Class name

AttributesBlock delimiters

Page 38: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

class Rectangle {

private double width, height; // attributes

....... }

Class name

Attributes

Note that in all our examples,we mark the attributes private!

In good designs, classes areblack boxes!

Page 39: Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical

Class Example - Java code

• Rectangle.java

class Rectangle { private double width, height; // attributes

public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}

double Height( ) { // projectorreturn height; // simply returns value} // of an attribute

double Width( ) {return width;}

double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates

double getPerimeter( ) {

return 2.0*(width + height);}

void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute

void setWidth( double w ) {width = w;}

}

class Rectangle {

private double width, height; // attributes

....... }

Class name

Attributes

Note that in all our examples,we mark the attributes private!

In good designs, classes areblack boxes!