Lesson B - Basic Java Elements
Lesson B - Basic Java Elements
Lecture B
Slide 1 of 69.
Lecture B
Slide 2 of 69.
Identifiers: Syntax
Identifiers are the words a programmer uses in a program Identifier syntactic rules:
Cannot begin with a digit User and user are completely different identifiers
Lecture B
Slide 3 of 69.
Identifiers: Semantics
Identifiers names can come from the following sources
Lecture B
Slide 4 of 69.
Naming style
The correctness of the program is not affected by the
names used
public class X7_65Tx { }
Names play a central role in the readability of the program They are part of its documentation They should thus be chosen carefully
Lecture B
Slide 5 of 69.
White Space
Spaces, blank lines, and tabs are collectively called white
space White space is used to separate words and symbols in a program Extra white space is ignored A valid Java program can be formatted many different ways Programs should be formatted to enhance readability, using consistent indentation
Lecture B
Slide 6 of 69.
Lecture B
Slide 7 of 69.
Comments
Comments are ignored and are treated as white space They should be written to enhance readability
Explain what a piece of code does (its interface) Explain any special tricks, limitations, // comment to end of line /* comment until closing */ /** API specification comment */
Lecture B
Slide 8 of 69.
Lecture B
Slide 9 of 69.
Variables
A variable is a location in memory that can hold values of
a certain data type Each variable must be declared before it is used The declaration allocates a location in memory to hold values of this type Variable types can be
Lecture B
Slide 10 of 69.
VariableExample Program
public class VariableExample { public static void main(String[] args){ int x; x = 3; System.out.println(x); x = 4; System.out.println(x); } }
Lecture B
Slide 11 of 69.
Lecture B
Slide 12 of 69.
3
x
Lecture B
Slide 13 of 69.
3
x
Lecture B
Slide 14 of 69.
4
x
Lecture B
Slide 15 of 69.
4
x
Lecture B
Slide 16 of 69.
operators you can perform on them The Java language has several predefined types, called primitive data types The following reserved words represent the eight different primitive data types:
Lecture B
Slide 17 of 69.
Integers
There are four integer data types. They differ by the
Lecture B
Slide 18 of 69.
Floating Point
There are two floating point types
Bits 32 64
Lecture B
Slide 19 of 69.
Characters
A char value stores a single character from the Unicode
A, B, C, , a, b, ,0, 1, , $,
allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages The ASCII character set is a subset of Unicode ASCII is the current standard (outside of Java)
Lecture B
Slide 20 of 69.
Boolean
A boolean value represents a true/false condition. It can also be used to represent any two states, such as a
light bulb being on or off The reserved words true and false are the only valid values for a boolean type
Lecture B
Slide 21 of 69.
Variable Declarations
The syntax of a variable declaration is
data-type variable-name;
For example
int total;
declaration
int total = 0, count = 20; double unitPrice = 57.25;
Lecture B
Slide 22 of 69.
Lecture B
Slide 23 of 69.
Lecture B
Slide 24 of 69.
Constants
We may declare that a variable is a constant and its value
Advantages:
Lecture B
Slide 25 of 69.
Lecture B
Slide 26 of 69.
Assignment Statements
An assignment statement takes the following form
variable-name = expression;
Lecture B
Slide 27 of 69.
Arithmetic Operators
An operator is a mapping that maps one or more values
Unary Operator:
Lecture B
Slide 28 of 69.
Pounds to Kg conversion
public class PoundsToKg { public static void main(String[] args){ double weightInPounds = 200.0; final double KILOS_IN_POUND = 0.455; double weightInKg; weightInKg = weightInPounds * KILOS_IN_POUND ; System.out.println(weightInKg); } }
Lecture B
Slide 29 of 69.
Pounds to Kg conversion 2
public class PoundsToKg2 { public static void main(String[] args){ final double KILOS_IN_POUND = 0.455; System.out.println(200.0 * KILOS_IN_POUND); } }
Lecture B
Slide 30 of 69.
Integer Division
When division is performed on integers (byte, short,
int j = 5; double x = 5.0, y; System.out.println(j / 2); // 2 System.out.println(x / 2.0); // 2.5 System.out.println(5 / 2); // 2 y = j / 2 ; // 2
Lecture B
Slide 31 of 69.
Complex Expressions
Expressions can combine many operators and operands Examples:
x -34 weight * 2.73 2 * PI * r a - (7 b) 1 + 2 + 3 + 4 (x + y) * (2 - z + (5 - q)) * -(1-x)
Lecture B
Slide 32 of 69.
Operator Precedence
Multiplication, division, and remainder (%) have a higher
precedence than addition and subtraction. Operators with same precedence evaluate from left to right. Parenthesis can be used to force order of evaluation.
Lecture B
Slide 33 of 69.
Expression 10 - 7 - 1 10 - (7 - 1) 1 + 2 * 3 (1 + 2) * 3 1 - 2 * 3 + 4 * 5 2 4 7 9 15
Result
Lecture B
Slide 34 of 69.
Conversions
Data types can be mixed in an expression When the expression is evaluated one type is converted
Can be converted to a narrower type only by casting List of types from narrowest to widest:
Lecture B
Slide 35 of 69.
Conversion Examples
double f, x; int j; f = 5; f = 5.0 / 2; f = x * j; f = 5 / 2; f = (float) j / 5; j = (int) f; j = (int) 5.0 / 2.0;
Lecture B
Slide 36 of 69.
Lecture B
Slide 37 of 69.
Reference Types
Variables can be declared to be of an object type. In this
myName
today
Memory
Lecture B
Slide 38 of 69.
Creating Objects
Objects are created by invoking a constructor of the class.
Lecture B
Slide 39 of 69.
today
Lecture B
Slide 40 of 69.
today
12345
Lecture B
Slide 41 of 69.
today
12345
Lecture B
Slide 42 of 69.
Strings
Strings are objects that are treated by the compiler in special ways:
String myName = John Jones; String hello; hello = Hello World; hello = hello + !!!!; int year = 2008; String s = See you in China in + year;
Lecture B
Slide 43 of 69.
Method invocations
You may invoke methods on an object. Methods may
Lecture B
Slide 44 of 69.
APIs
To use an object you only need to know its application
programmer interface (API). The API of an object class includes a description of:
all available constructors and methods and what they do the parameters they take and the values that they return
javadoc.
Lecture B
Slide 45 of 69.
Javadoc example
Lecture B
Slide 46 of 69.
Lecture B
Slide 47 of 69.
Class Libraries
A class library is a collection of classes that we can use
when developing programs There is a standard class library that comes with every Java environment. Class Libraries are organized into packages
java.net, java.lang, java.io, ... Import the package import java.io.*; File f = new File(John);
Or, use a fully qualified class name java.io.File f = new java.io.File (John);
Lecture B
Slide 48 of 69.
RandomNumbers.java
import java.util.Random; public class RandomNumbers { public static void main (String[] args){ Random generator = new Random(); int num = generator.nextInt(); System.out.println ("A random int: " + num); num = generator.nextInt(); System.out.print(Another one: + num); } }
Lecture B
Slide 49 of 69.
Lecture B
Slide 50 of 69.
Lecture B
Slide 51 of 69.
object:
InputRequestor inp = new InputRequestor();
Lecture B
Slide 52 of 69.
InputRequestor Behaviour
A window will pop up every time you use the
requestXXX() method:
Lecture B
Slide 53 of 69.
MS-DOS window, you may display output in your own window. Create an OutputWindow object:
OutputWindow outwin = new OutputWindow();
println() - just as regular println(). clear() - clears the output window. showMessage() - pop up a message on the desktop.
Lecture B
Slide 54 of 69.
Lecture B
Slide 55 of 69.
Lecture B
Slide 56 of 69.
Lecture B
Slide 57 of 69.
Lecture B
Slide 58 of 69.
The Web
Lecture B
Slide 59 of 69.
Lecture B
Slide 60 of 69.
Lecture B
Slide 61 of 69.
embedded in web-pages Such Java programs are called Applets This had to solve some problems
Platform independence
browsers have a built-in Java byte-code interpreter
Security
the program runs in the browser in a sandbox
Lecture B
Slide 62 of 69.
Lecture B
Slide 63 of 69.
Writing Applets
An Applet does not have a main method. Instead, it has a set of methods that control the interaction
with the user. An Applet inherits this interface from the java.applet.Applet class The paint method has to paint the screen
It receives a Graphics object, and uses it to paint The Graphics class has various drawXXX() methods
Lecture B
Slide 64 of 69.
Smiley.java
import java.applet.Applet; import java.awt.*; public class Smiley extends Applet{ public void paint (Graphics page){ page.setColor(Color.yellow); page.fillOval(50,50,300,300); page.setColor(Color.black); page.fillOval(125,125,50,50); page.fillOval(225,125,50,50); page.drawLine(150,275,250,275); } }
Lecture B
Slide 65 of 69.
Smiley.HTML
<H3>My Smiley Applet</H3> <applet code=Smiley.class" width=400 height=400> </applet> <p> Above this text you should see Smiley!!
Lecture B
Slide 66 of 69.
Smiley
Lecture B
Slide 67 of 69.
Graphics Coordinates
10 150 X
20
45
Lecture B
Slide 68 of 69.
Drawing an Oval
175 X
20
80
50
Lecture B
Slide 69 of 69.