Java-Builtin-Types-Emoon
Java-Builtin-Types-Emoon
Java-Builtin-Types-Emoon
• OOP functions
• Data Abstraction
conditionals and loops
text I/O
1
Basic building blocks of
Java
Built-in data types
https://introcs.cs.princeton.edu/java/12types/
Built-in data types
A data type is a set of values and a set of operations defined on those values.
int a;
int b;
a = 1234;
b = 99;
int c = a + b;
Basic Definitions
A variable is a name that refers to a value.
A literal is a programming-language representation of a value.
A declaration statement associates a variable with a type.
An assignment statement associates a value with a variable.
variables
int a;
declaration statements
int b;
a = 1234;
assignment statements
b = 99;
int c = a + b;
combined declaration
and assignment statement
literals
A. I t doe s e xch ange (W e n eed out put t o con firm i t doe s e xch an g e). 8
standard
Output output
Bird's eye view of a Java program
• Java automatically converts numbers to strings for output.
You can put any base types, such as int and double.
Methods for output
• System.out.print(String s): Print the string s.
• System.out.println(String s): Print the given string s, followed by the newline character.
Robert
ThisSedgewick | Kevin method.CS
is a print Wayne is fun! See Goodrich et al’s text pp. 38 - 40
Input using Scanner class
• The input from the standard input device, which by default is
the computer keyboard echoing its characters in the Java
console at run time.
12
Methods in the Scanner class
• Q. How do we give an integer as an input at run time?
• A. Need to call the method nextInt() to convert the strings to integers.
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
Input and output warmup: exchange values
import java.util.*;
A. Reads two integers from the input device, then prints them out in the opposite order. 38
Robert Sedgewick | Kevin Wayne elice>Week 1-2> I/O warm up: Exchange
Data type for computing with integers: int
int data type
values integers between −2 3 1 and 2 3 1 −1
Important note:
typical literals 1234 99 0 1000000
Only 2 3 2 different intvalues.
operations add subtract multiply divide remainder
–1.0 –1.0
x2 — x -1
double b = input.nextDouble(); 1.618033988749895
double c = input.nextDouble(); -0.6180339887498949
1.0 1.0 x2 + x + 1
double discriminant = b*b - 4.0*c;
NaN
double d = Math.sqrt(discriminant);
//implement here.
double root1 = (-b+d)/2.0;
NaN
floatdata type
// divisible by 4 1900
isLeapYear = (year % 4 == 0); false
// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0); 1993
// divisible by 4 and not 100 unless divisible by 400 false
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear); 2019
} false
}
24
The Java compiler is your friend : it checks for type errors in your code.
When appropriate, we often convert a value from one type to another to make types match.
26
11 0.25
String
double
"x: 99"
2.75
• Make numeric types match if n o loss of precision. 11 is converted to a double and then, the result
of multiplying two doubles is a double.
Function call.
Integer.parseInt("123") int 123
Ex. Integer.parseInt(String s) Math.round(2.71828) long 3
https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
Explicit Cast for values that belong to multiple types. (int) 2.71828 int 2
• Ex: small integers can be short, int, or long. (int) Math.round(2.71828) int 3
• Ex: double values can be truncated to int values. 11 (int) 0.25 int 0
a. ( 7 / 2 ) 2.0
b. ( 7 / 2.0 ) 2
c. "3" + 2
d. 3.0 + "2"
e. Integer.parseInt("1010", 2) 28
import java.util.Scanner;
31
First launch of Ariane 5, 1996
A rocket exploded in midair because of
Robert Sedgewick | Kevin Wayne a type-conversion problem.
An instructive story about type conversion
Ariane 5 rocket. Ariane 5 rocket exploded 40
seconds after being launched by European Space
Agency. Maiden voyage after a decade and 7 billion
dollars of research and development. Sensor
reported acceleration that so was large that it
caused an overflow in the part of the program
responsible for recalibrating inertial guidance. 64-
bit floating point number was converted to a 16-bit
signed integer, but the number was larger than
32,767 and the conversion failed. Unanticipated
overflow was caught by a general systems
diagnostic and dumped debugging data into an
area of memory used for guiding the rocket's
motors. Control was switched to a backup
computer, but this had the same data. This resulted
in a drastic attempt to correct the nonexistent
problem, which separated the motors from their
mountings, leading to the end of Ariane 5. First launch of Ariane 5, 1996
A rocket exploded in midair because of
a type-conversion problem.
Summary
A data type is a set of values and a set of operations on those values.