© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to...
-
Upload
avery-harvell -
Category
Documents
-
view
212 -
download
0
Transcript of © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to...
![Page 1: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/1.jpg)
© Vinny Cahill 1
Writing a Program in Java
![Page 2: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/2.jpg)
© Vinny Cahill 2
The “Hello World” Program
Want to write a program to print a message on the screen.
![Page 3: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/3.jpg)
© Vinny Cahill 3
“Hello World” – Objects and Classes
An instance of class TerminalAn instance of class String
Another instance of class String
Class String isbuilt into Java Class Terminal is part of a
collection of pre-existing classescalled tcdIO
![Page 4: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/4.jpg)
© Vinny Cahill 4
Functions and Parameters
Given f(x) = x2 + 4x + 13
What is f(10)?
What is f(20)?
What is f(30)?
x is the “formal parameter” of function f
10 is an “actual parameter” of function f
![Page 5: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/5.jpg)
© Vinny Cahill 5
Variables 1
A variable is a container for information
In our program
Terminal window; // Used to store object representing the
window
“window” is the name of a container used to store a Terminal object
Alternatively:
Terminal box;
![Page 6: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/6.jpg)
© Vinny Cahill 6
Variables 2
Every variable has a name, a type, and a single (current) value
window
type is Terminal
namevalue
Think of a variable as a container for a value of the specified type
![Page 7: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/7.jpg)
© Vinny Cahill 7
Variables 3
window is a local variablebecause it is defined within a method
public static void main (String args[]) { Terminal window; // store object representing terminal
. . . .
}
![Page 8: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/8.jpg)
© Vinny Cahill 8
Variables 4
The value stored in a variable can be changed using assignment
window
that why it’s called a variable!
window
window = new Terminal("Hello Window");
Assignment operator
![Page 9: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/9.jpg)
© Vinny Cahill 9
Variables 5
The value stored in a variable can be changed using assignment as often as we want
window
window
window = new Terminal("Another Window");
![Page 10: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/10.jpg)
© Vinny Cahill 10
Type int
Type int represents positive and negative whole numbers
four bytes of memory are used to store a value of type int thus, an int can contain a number in the range
-2,147,483,648 to 2,147,483,647 values of type int can be written in decimal
-123 23000 0 1000000 -7456 3990276
![Page 11: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/11.jpg)
© Vinny Cahill 11
Integer operators 1
There are five integer operatorsOperator Meaning ExamplesUse
+ op1 + op2 add values of op1 and op2
newSalary = oldSalary + rise;count = count +1;
- op1 - op2 subtract op2from op1
newSalary = oldSalary - 100;
* op1 * op2 multiply op1by op2
area = length * breath;minutes = hours * 60;
/ op1 / op2 divide op1by op2
length = area / breath;
% op1 % op2 remainder ondividing op1by op2
pounds = pence / 100pence = pence % 100;
![Page 12: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/12.jpg)
© Vinny Cahill 12
Integer operators 2
The int operators are all binary operators– they all take two operands
The int operators all return a single result of type int
For example:
int result, number1, number2;number1 = 7; number2 = 2;
result = number1 / number2;
result = number1 % number2;
![Page 13: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/13.jpg)
© Vinny Cahill 13
Integer expressions
We can have expressions that involve multiple operators
pay = salary + bonus - tax;
monthlyPay = salary + bonus - tax / 12;
Note that the order in which operators are evaluated is important!
*, /, % are always evaluated before +, - i.e. *, /, % have higher precedence than +, -
monthlyPay = (salary + bonus - tax) / 12;
Operators of equal precedenceare evaluated left to righti.e. they are left associative
![Page 14: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/14.jpg)
© Vinny Cahill 14
Average
/* A program to get the average of five numbers entered by the user */import tcdIO.*;
public class Average { public static void main(String[] args) {
Terminal window; // Used to store object representing the windowint runningTotal, average; // used to store total
window = new Terminal("Average"); runningTotal = window.readInt("Enter first number: "); runningTotal = runningTotal + window.readInt("Enter second number: "); runningTotal = runningTotal + window.readInt("Enter third number: "); runningTotal = runningTotal + window.readInt("Enter fourth number: "); runningTotal = runningTotal + window.readInt("Enter fifth number: "); average = runningTotal / 5; window.println("The average is: " + average); }
}
integer value integer value
integer value
![Page 15: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/15.jpg)
© Vinny Cahill 15
![Page 16: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/16.jpg)
© Vinny Cahill 16
Compilation
The text of any program needs to be translated into an executable form
The process of doing this translation is called “compilation”
The program that performs the process is called a “compiler”
Different programming languages use different compilers
Eclipse includes a Java compiler
![Page 17: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/17.jpg)
© Vinny Cahill 17
Compilation cntd.
HelloWorld.java
Java program as text
Java compiler HelloWorld.class
Executable Java program
![Page 18: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/18.jpg)
© Vinny Cahill 18
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
The program always starts executing the main method
![Page 19: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/19.jpg)
© Vinny Cahill 19
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
On the screen
“Address Window”
In the computer
object
We execute a single statementat a time in sequence
point of control
![Page 20: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/20.jpg)
© Vinny Cahill 20
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
On the screen
“Address Window”
In the computer On the screen
![Page 21: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/21.jpg)
© Vinny Cahill 21
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
In the computer On the screenOn the screen
![Page 22: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/22.jpg)
© Vinny Cahill 22
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
In the computer On the screenOn the screen
![Page 23: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/23.jpg)
© Vinny Cahill 23
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
In the computer On the screenOn the screen
![Page 24: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/24.jpg)
© Vinny Cahill 24
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
In the computer On the screenOn the screen
![Page 25: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/25.jpg)
© Vinny Cahill 25
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
On the screen
“Address Window”
In the computer
![Page 26: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/26.jpg)
© Vinny Cahill 26
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
On the screen
“Address Window”
On the screenprintln! - "Prof. Vinny Cahill"
![Page 27: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/27.jpg)
© Vinny Cahill 27
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
On the screenOn the screenprintln! - "Discipline of Computer Systems"
![Page 28: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/28.jpg)
© Vinny Cahill 28
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
On the screenOn the screen
println! - "School of Computer Science and Statistics"
![Page 29: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/29.jpg)
© Vinny Cahill 29
Flow of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
On the screenOn the screenprintln! - “Trinity College Dublin"
![Page 30: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/30.jpg)
© Vinny Cahill 30
Transfer of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
On the screen
“Address Window”
In the computer
![Page 31: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/31.jpg)
© Vinny Cahill 31
Transfer of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
On the screen
“Address Window”
On the screenprintln! - "Prof. Vinny Cahill"
void println (String text) { // step 1 // step 2 // step 3}
![Page 32: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/32.jpg)
© Vinny Cahill 32
Transfer of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
println! - "Discipline of Computer Systems"
void println (String text) { // step 1 // step 2 // step 3}
On the screenOn the screen
![Page 33: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/33.jpg)
© Vinny Cahill 33
Transfer of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
void println (String text) { // step 1 // step 2 // step 3}
On the screenOn the screen
println! - "School of Computer Science and Statistics"
![Page 34: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/34.jpg)
© Vinny Cahill 34
Transfer of control
public static void main(String[] args) { Terminal window;
window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin");}
“Address Window”
void println (String text) { // step 1 // step 2 // step 3}
println! - “Trinity College Dublin"
On the screenOn the screen
![Page 35: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/35.jpg)
© Vinny Cahill 35
![Page 36: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/36.jpg)
© Vinny Cahill 36
Transfer of control
public static void main(String[] args) { Terminal window; Square shape; int area;
window = new Terminal(“Square”); shape = new Square(10); area = shape.calculateArea(); window.println(“Area is: ” + area);}
![Page 37: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/37.jpg)
© Vinny Cahill 37
Transfer of control
public static void main(String[] args) { Terminal window; Square shape; int area;
window = new Terminal(“Square”); shape = new Square(10); area = shape.calculateArea(); window.println(“Area is: “ + area);}
“Square”
On the screen
![Page 38: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/38.jpg)
© Vinny Cahill 38
Transfer of control
public static void main(String[] args) { Terminal window; Square shape; int area;
window = new Terminal(“Square”); shape = new Square(10); area = shape.calculateArea(); window.println(“Area is: “ + area);}
“Square”
On the screen
10
![Page 39: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/39.jpg)
© Vinny Cahill 39
Transfer of control
public static void main(String[] args) { Terminal window; Square shape; int area;
window = new Terminal(“Square”); shape = new Square(10); area = shape.calculateArea(); window.println(“Area is: “ + area);}
10
calculateArea!
int calculateArea() { // step 1 // step 2 // step 3}
“Square”
On the screen100
![Page 40: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/40.jpg)
© Vinny Cahill 40
Transfer of control
public static void main(String[] args) { Terminal window; Square shape; int area;
window = new Terminal(“Square”); shape = new Square(10); area = shape.calculateArea(); window.println(“Area is: ” + area);}
“Square”
void println (String text) { // step 1 // step 2 // step 3}
println! - “Area is: 100”
On the screen
10
![Page 41: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/41.jpg)
© Vinny Cahill 41
Transfer of control
public static void main(String[] args) { Terminal window; Square shape; int area;
window = new Terminal(“Square”); shape = new Square(10); area = shape.calculateArea(); window.println(“Area is: “ + area);}
“Square”
On the screen
10
![Page 42: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/42.jpg)
© Vinny Cahill 42
Type double
Type double represents real numbers
A value of type double occupies 8 bytes of memory A value of type double can store a number in the range
± 1.79769313486231570E+308 with 15 significant decimal digits
Values of type double can be followed by the letter ‘D’
230.6 0.0 -1.0E8 -7.4E-5 -123D -399.02E+7F and usually have either a decimal point or an exponent
![Page 43: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/43.jpg)
© Vinny Cahill 43
double operators
There are four double operators
Operator Meaning ExamplesUse
+ op1 + op2 add values of op1 and op2
newSalary = oldSalary + rise;distance = distance + 10.2;
- op1 - op2 subtract op2from op1
time = hours - 0.6;
* op1 * op2 multiply op1by op2
area = radius * PI;area = radius * 3.14D;
/ op1 / op2 divide op1by op2
length = area / breath;
![Page 44: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/44.jpg)
© Vinny Cahill 44
Real expressions
The same rules apply to writing real expressions as to integer expressions
The double operators are all binary operators that take two operands
The double operators all return a single result of type double * and / have higher precedence than + and – Operators of equal precedence are left associative Can use brackets to change the order of evaluation
![Page 45: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/45.jpg)
© Vinny Cahill 45
Other integer types
Java actually provides four primitive types that represent integers They differ only in their size
(i.e., the range of values that they can store)
int 32 bits -2,147,483,648 to 2,147,483,647
short 16 bits -32,768 to 32,7687
long 64 bits -9,233,372,036,854,775,808 to .....
byte 8 bits -128 to 127
Use int unless you have a really good reason not to!
![Page 46: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/46.jpg)
© Vinny Cahill 46
Floating-point types
Java provides two primitive types that represent real numbers Again they differ only in their size
(i.e., the range of values that they can store and their precision)
float 32 bits ± 3.40282347E+38 with 7 significant decimal digits
In general, use double rather than float
double 64 bits ± 1.79769313486231570E+308 with 15 significant decimal digits
Values of type float are followed by an ‘F’
![Page 47: © Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.](https://reader038.fdocuments.in/reader038/viewer/2022110116/55163ed0550346a2308b65a5/html5/thumbnails/47.jpg)
© Vinny Cahill 47
Assignment compatibility
The type of an expression must be the same as the type of the variable to which its value is being
assigned
“You can’t put a square peg in a round hole!”