Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java Simple Arithmetic
Last Week
Lecture 2
1
CP4044 – Lecture 2
2
CP4044 – Lecture 2
Hello World Program
Platform
Specific
Platform Independent
Programmer types the
program into the computer
in the high level Java language.
/* The "hello world" program in Java.
Date written 2/10/7
*/
The Java program is translated
into a form that is “understood”
by the computer.
1
2
Text
Editor
Source Code
Compiler
ClassName.java
notepad, eclipse,
bluej, kate, etc
3
CP4044 – Lecture 2
3
Hardware and
Operating
System
Programmer
public class MyProg
{
static public void main(String args[])
{
System.out.println("Hello world");
System.out.println("Goodbye");
}
}
The program executes
Byte Code
Interpreter
ClassName.class
javac
java, appletviewer,
netscape etc.
4
CP4044 – Lecture 2
The int primitive data type
• Primitive data type
– Not a class – no methods
– Java is not a fully OO language
This week
• Stores a single integer value
– A whole number
• Platform independent
– 32 bits on all architectures and O/S
• Other integer types are available
– long – 64 bits
– byte – 8 bits
CP4044 – Lecture 2
5
CP4044 – Lecture 2
6
Kevan Buckley, Peter Burden, CP4044, 2004
1
The int primitive data type
Sum of two numbers
public class Sum1 {
• All variables must be declared before use
public static void main(String[] args) {
int x=4, y;
int x = 3;
y=7;
int y;
Variable
declaration
Initialisation
before use
y = 4;
• Integer literals
System.out.println("Sum of " +
– Leading zeros indicate octal
Output to
stdio
x +
" and " +
09 is invalid
String
concatenation
y +
– 0x indicates hexadecimal
" is " +
0xff equals 255
Type-casting/
conversion
(x + y));
– Trailing L indicates a long value
}
9987689756985L is a long value
Operator
precedence
}
Sum of 3 and 4 is 7
7
CP4044 – Lecture 2
8
CP4044 – Lecture 2
Sum of two numbers?
Sum of two numbers?
public class Sum2 {
public class Sum3 {
public static void main(String[] args) {
public static void main(String[] args) {
int x = 3, y = 4;
int x = 3, y = 4;
System.out.print("Sum of ");
System.out.println("Sum of " + x + " and " + y +
System.out.print(x);
" is " + x + y);
System.out.print(" and ");
}
System.out.print(y);
}
System.out.print(" is ");
System.out.println(x + y);
}
}
Sum of 3 and 4 is 7
What is happening
here?
Sum of 3 and 4 is 34
9
CP4044 – Lecture 2
Primitive data types
Primitive data types
• boolean
• byte
– Value is either true or false
– Holds an 8 bit integer
– Values range from –128 to 127
• true and false keywords are used for literals
– Occupies one bit of memory
• short
• char
• int
– Holds a 32 bit integer
– Values range from –2147483648 to 2147483647
• long
'a' - letter a
'\n' – escape code for new line character
'\u12cf' - hexadecimal
'\066' – octal – maximum value is '\377' i.e. '\u00ff'
CP4044 – Lecture 2
java.math.BigDecimal
provides arbitrary
precision arithmetic
at a price in
performance
– Holds a 16 bit integer
– Values range from –32768 to 32767
– Stores a single character
– Uses unicode, not ASCII
– Occupies 16 bits
– Literals enclosed in single quotes
•
•
•
•
10
CP4044 – Lecture 2
– Holds a 64 bit integer
– Values range from –9223372036854775808 to
9223372036854775807
11
CP4044 – Lecture 2
12
Kevan Buckley, Peter Burden, CP4044, 2004
2
Primitive data types
Operators
• See Workshop notes for the full list of Java
operators
• float
– Holds a 32 bit floating points number
– Values range from ±1.4E-45 to ±3.4028235E+38
– literal required to end in 'f'.
• Some operators are overloaded
– i.e. behavior depends on the context
• float pi = 3.14f;
• Example +
• double
25 + 6
“25” + “6”
– Holds a 64 bit floating point number
– Values range from ±4.9E-324 to
±1.7976931348623157E+308
Arithmetic addition
String concatenation
produces 31
produces “256”
• Example /
2.5 / 2.0
7/3
Floating point division produces 1.25
Integer division
produces 2
Floating point numbers conform to IEEE 754-1985
13
CP4044 – Lecture 2
14
CP4044 – Lecture 2
Arithmetic Example
public class Arithex1 {
public static void main(String[] args) {
double x = 23.4, y = -41.7;
Formatting Output
Addition
import java.text.*;
Import
packages
public class Arithex2 {
Number
Format
Multiplication
int i = 4, j = 7;
(Integer)
division
System.out.println("x + y = " + (x + y));
Modulus
NumberFormat layout =
System.out.println("x * i = " + (x * i));
Rounding
errors
layout.setMaximumFractionDigits(4);
System.out.println("j / i = " + (j / i));
public static void main(String[] args) {
NumberFormat.getNumberInstance();
double x = 23.4, y = 41.7;
System.out.println("x % i = " + (x % i));
System.out.println("x/y = " + (x / y));
}
String outstr = layout.format(x / y);
}
x + y = -18.300000000000004
System.out.println("x/y = " + outstr);
x * i = 93.6
}
j / i = 1
CP4044 – Lecture 2
x/y = 0.5611510791366906
}
x % i = 3.3999999999999986
15
CP4044 – Lecture 2
x/y = 0.5612
Floating Point Arithmetic Errors
public class Divzer1 {
public static void main(String[] args[]) {
double x, y, quotient;
x = 1;
y = 0;
Factory
method
Define format:
fraction &
integer parts
Build output
string
See also
Decimal
Format &
Font Metrics
16
Arithmetic Errors
Division by
zero
public class Divzer2 {
public static void main(String[] args) {
IEEE-754
int x, y, quotient;
Infinity
x = 1;
NaN
y = 0;
quotient = x / y;
quotient = x / y;
Division by
zero
Exceptions
Integers and
simple!
Code
defensively
System.out.println("Quotient = " + quotient);
System.out.println("Quotient = " + quotient);
}
}
}
}
Quotient = Infinity
Exception in thread "main" java.lang.ArithmeticException:
/ by zero
at Divzer2.main(Divzer2.java:6)
CP4044 – Lecture 2
17
CP4044 – Lecture 2
18
Kevan Buckley, Peter Burden, CP4044, 2004
3
Identifying Problems
public class Divzer3 {
public static void main(String[] args) {
double x, y, quotient;
x = 1;
y = 0;
quotient = x / y;
if (quotient == Double.POSITIVE_INFINITY)
System.out.println("Problem");
else
Trapping Exceptions
public class Divzer4 {
Conditional
statement
public static void main(String[] args) {
Equality vs.
assignment
int x, y, quotient;
Wrapper
classes
y = 0;
x = 9;
Exception
handlers
Exception
classes
try {
quotient = x / y;
POSITIVE_
INFINITY
Throwing
exceptions
} catch (ArithmeticException ae) {
Finding out
more detail
System.out.println("Problem");
Java is not
fully OO
return;
}
System.out.println("Quotient = " + quotient);
System.out.println("Quotient = " + quotient);
}
}
}
}
19
CP4044 – Lecture 2
20
CP4044 – Lecture 2
Don't Rely on Exception Handlers
public class Intof {
public static void main(String[] args) {
int x, y, product;
x = 1000000;
y = 2000000;
try {
product = x * y;
} catch (ArithmeticException ae) {
Packages
Overflow
• Java includes many Standard Classes
Integers are
simple
– E.g. NumberFormat
Code to
defend
against
problems, not
react to them.
System.out.println("Problem");
return;
System.out.println("Product = " + product);
}
}
Product = -1454759936
– E.g. NumberFormat is in the java.text package
• The names of packages have a hierarchical
structure (similar to DNS names)
• Most of the Java classes you use are in the
java.lang package
}
CP4044 – Lecture 2
• Classes are organized into Packages
• Use the Java Documentation to find out what
package a class is in
21
Packages
Example from the Workshop
• When you use a Java standard class, you must
tell the compiler which package it is in. There
are three ways to do this:
java.text.NumberFormat
import java.text.NumberFormat;
import java.text.*;
import java.text.*;
public class Arithex2
{
static public void main(String args[])
{
NumberFormat layout = NumberFormat.getNumberInstance();
layout.setMaximumFractionDigits(4);
double x = 23.4, y = 41.7;
System.out.println("x/y = " + (x/y));
• The java.lang package is imported by default
• Classes you define are stored in the default
package
CP4044 – Lecture 2
22
CP4044 – Lecture 2
String outstr = layout.format(x/y);
System.out.println("x/y = " + outstr);
}
}
23
CP4044 – Lecture 2
24
Kevan Buckley, Peter Burden, CP4044, 2004
4