Download presentation
Presentation is loading. Please wait.
Published byClemence Allison Modified over 9 years ago
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 and Electronic Engineering, University of Western Australia * The presenters of this course wish to disclaim responsibility this corny part of the title! John Morris Peter Jones
2
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
3
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
4
Class Example - Java code Rectangle.java class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = h; // changes the value of an } // attribute void setWidth( double w ) { width = w; }
5
Primitive Types Boolean boolean Values true false Not in C!
6
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)
7
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!
8
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
9
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
10
Primitive Types Arrays Rather different from C! int [] intArray;... intArray = new int[n]; double [] dlist = new double[n]; Initialised arrays int[] primes = { 2, 3, 5, 7, 11 }; Array attributes int n = primes.length; More later!
11
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];
12
Class Example - Java code Rectangle.java Block delimited by { } class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = h; // changes the value of an } // attribute void setWidth( double w ) { width = w; } 12
13
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
14
Class Example - Java code Rectangle.java Compilable program unit 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 ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = h; // changes the value of an } // attribute void setWidth( double w ) { width = w; }
15
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
16
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
17
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!
18
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 );
19
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)
20
Statements Assignment l_value = expression a = b; x = y = 0.0; z = f( x, a ) * g( y, b ); Statements are expressions
21
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
22
Expressions Operators > left, right shift a > 2 divide b by 4 x > k y / 2 k
23
Expressions Operators &, |, ^ arithmetic and, or, xor a & b bitwise and of a and b a | b bitwise or x ^ y x xor y Care a && b a & b Both legal, very different meaning!
24
Expressions Operators == relational equal if ( a == b ).. not if ( a = b ) assignment operator!
25
Expressions Operators &&, || boolean and, or if ( (a == b) && (x != y) )... again not if ( (a=b) & (x=y) ).. arithmetic operators!
26
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!
27
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
28
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];
29
Statements Loops while while( exp ) statement1; while( exp ) { statements; } while (a>b) a = x[i++]; while ( x < 0 ) { x = getX(... ); y = y + x; } while is zero-trip
30
Statements Loops do do 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
31
Statements - Loops for for( 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!
32
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; }
33
Strings Standard class Package : java.lang Literals “abcd” “123”
34
Objects for primitive data types Wrappers Make objects from standard data types java.lang Boolean Integer Long Character Float Double
35
Class Example - Java code Rectangle.java class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = h; // changes the value of an } // attribute void setWidth( double w ) { width = w; } class Rectangle { private double width, height; // attributes....... } Class name
36
Class Example - Java code Rectangle.java class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = h; // changes the value of an } // attribute void setWidth( double w ) { width = w; } class Rectangle { private double width, height; // attributes....... } Class name Block delimiters
37
Class Example - Java code Rectangle.java class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = h; // changes the value of an } // attribute void setWidth( double w ) { width = w; } class Rectangle { private double width, height; // attributes....... } Class name Attributes Block delimiters
38
Class Example - Java code Rectangle.java class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = 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 are black boxes!
39
Class Example - Java code Rectangle.java class Rectangle { private double width, height; // attributes public Rectangle( double w, double h ) { // constructor width = w; // set attributes from height = h; // parameters } double Height( ) { // projector return height; // simply returns value } // of an attribute double Width( ) { return width; } double getArea( ) { // projector return width*height; // returns the value of an attribute } // which it calculates double getPerimeter( ) { return 2.0*(width + height); } void setHeight( double h ) { // update method height = 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 are black boxes!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.