Professional Documents
Culture Documents
• Textbooks
• Y. Daniel Liang. (2020). Introduction to Java Programming and
Data Structures, Comprehensive Version. 12. Pearson. New
York. ISBN: 978-0136520238.
Outline
• Data Types
• Type Casting (Conversion)
• Input/Output (I/O)
• Output Format
• ASCII
• Constant
Bina Nusantara 3
Data Type
• An attribut that has range value and type.
• To be used as a value storage from execution process.
• Based on its data type :
– Boolean (boolean)
– Numeric (byte, short, int, long, float, double)
– Character (char)
– String (String)
Bina Nusantara 4
Data Type Categories
• Based on Complexity :
– Atomic DT (boolean, byte, char, short, int, long, float,
double)
– Composite DT (Array, Struct, List, Queue, Stack, String,
Tree)
• Based on Source:
– Native / primitive / basic DT
– Abstract DT
• Based on Customization:
– Built-in DT
– User-defined DT
Bina Nusantara 5
Numeric Data Type
Name Range Storage size
byte -27 (-128) to 27-1 (127) 8-bit signed
short -215 (-32768) to 215-1 (32767) 16-bit signed
int -231 (-2147483648) to 231-1 (2147483647) 32-bit signed
long -263 to 263-1 64-bit signed
Negative range: -3.4028235E + 38 to -1.4E-45
float 32-bit IEEE 754
Positive range: 1.4E-45 to 3.4028235E+38
Negative range: -1.776931348623157E + 308 to -
4.9E-324
double 64-bit IEEE 754
Positive range: 4.9E-324 to
1.7976931348623157E+308
Bina Nusantara 6
Sample of Numeric Data Type
• Filename : DataType.java
• Output:
There are 4 Circles with radius 4.5
Bina Nusantara 7
Type Casting (Conversion)
• Convert a value to different type.
• Type casting conversion :
– Widening a type: convert a value from smaller data
to bigger data. (Automatically done by Java).
– Narrowing a type: convert a value from bigger data
to smaller data.
Bina Nusantara 8
Type Casting (Conversion)
• Parentheses is use as a syntax for narrowing.
• Example 1:
float f = (float) 10.1;
int i = (int) f;
• Example 2 :
double d = 4.5;
int i = (int) d;
Bina Nusantara 9
Numeric Casting
Example of storing numeric value to a numeric data
type.
Output
Bina Nusantara 10
Type Casting (Conversion)
Bina Nusantara 11
Type Casting (Conversion)
Type Casting (Conversion)
Bina Nusantara 12
Type Casting (Conversion)
Bina Nusantara 13
Type Casting (Conversion)
Type Casting (Conversion)
Bina Nusantara 14
Type Casting (Conversion)
Bina Nusantara 15
Character Data Type
Bina Nusantara 16
Example of Character Data Type
– Output: B
Bina Nusantara 17
String Types &
Boolean Variables
Output
– Note: String will be explained more in the Tutorial Class
Bina Nusantara 19
Input in Java Programming
• Get input from console using Scanner.
• Library java.util.Scanner.
• It needs to import declaration :
import java.util.Scanner;
• Afterward declare an object creation :
Scanner input = new Scanner(System.in);
Bina Nusantara 20
Common used Scanner’s methods
Method Description
next(); Input String (word)
nextLine(); Input String (sentence)
nextByte(); Input number (byte)
nextShort(); Input number (short)
nextInt(); Input number (int)
nextLong(); Input number (long)
nextFloat(); Input number (float)
nextDouble(); Input number (double)
Bina Nusantara 21
Output
• System.out.print
– print into console without linefeed (newline).
• System.out.println
– print into console with linefeed (newline)
• System.out.printf
– same as System.out.print, supports format output
Bina Nusantara 22
Input / Output (I/O) Example
Bina Nusantara 23
Input / Output (I/O)
Sample Output Program :
Bina Nusantara 24
Output Format
Specifier Description Example 1 Output 1 Example 2 Output 2
˽false false˽
%b boolean %6b %-6b
˽˽true true˽˽
%c character %5c ˽˽˽˽a %-5c a˽˽˽˽
˽˽˽69 69˽˽˽
%d integer %5d %-5d
1234567 1E+06
˽˽3.14 3.14˽˽
%f Floating-point %5.2f %-5.2f
˽20.60 20.60˽
%e scientific %10.2e ˽˽3.14e+02 %-10.2e 3.14e+02˽˽
%s string %10s ˽˽˽˽˽hello %-10s hello˽˽˽˽˽
Note : ˽ space
Bina Nusantara 25
Input / Output (I/O)
Bina Nusantara 26
ASCII
• American Standard Code for Information Interchange
• 7-bit, 128 characters (000 s/d 127)
• uppercase/lowercase letters, digits, punctuation
marks, dan control characters
• Next development of ASCII Unicode has 1,112,064
characters.
Bina Nusantara 27
ASCII
Bina Nusantara 28
ASCII
Bina Nusantara 29
ASCII
Sample Output :
Bina Nusantara 30
Constant
• a constant variable is a variable that is always constant
• Example :
– π (PHI) = 3.14159 26535 89793 23846 26433 83279
50288 41971 69399 37510
– g (gravitation) = 9.8
• Constant should have been declared and initialized.
• Final is a keyword in Java to declare constant.
• Declaration :
– final dataType CONSTANTNAME = VALUE;
– Example: final double PHI = 3.1415;
Bina Nusantara 31
Constant
Bina Nusantara 32
Exercise
• Create a program that can count the area of 2
geometries : square and rectangle. The following UI is
the one you have to create.
Outline
• Relational & Comparator operations
• Logic & Boolean operations
• Truth table
Bina Nusantara 34
Relational procedure
• Relational procedure == Comparator procedure.
• Comparing two values.
• Using 6 relational/comparator operator.
• The result has Boolean type.
• The values have number, ASCII, or String data type.
Bina Nusantara 35
Comparator operator
Operator Description Example Result
< less than 1<2 true
<= less than or equal to 1 <= 2 true
> greater than 1>2 false
>= greater than or equal to 1 >= 2 false
== equal to 1 == 2 false
Bina Nusantara 36
Comparator operator
• Character can be compared by refering to its ASCII
number.
– Example: ‘a’ is more bigger than ‘A’ because ASCII
number for ‘a’ (97) is bigger than ASCII number for
‘A’ (65).
• Word can be compared by String helper.
• Comparator operator is different than asssignment
operator.
– X = 14 store 14 to X.
– X == 14 compare if X is equal to 14.
Bina Nusantara 37
Comparator Operation
Bina Nusantara
Logic/Boolean Procedure
• Logical procedure == Boolean procedure.
• Execute 1 or 2 logic values.
• It has 4 types Logic/Boolean operator.
• The result has Boolean data type.
• Truth table
Bina Nusantara 39
Boolean Operator
Bina Nusantara 40
NOT (!)
p !p Example
true false !(1>2) is true, because (1>2) is false
false true !(1>0) is false, because (1>0) is true
Bina Nusantara 41
AND (&&)
p1 p2 p1 && p2 Example
false false false (2>3) && (5>5) is false
Because both (2>3) and (5>5) are false
false true false (2>3) && (6>5) is false
Because (2>3) is false
true false false (6>5) && (2>3) is false
Because (2>3) is false
true true true (3>2) && (5>=5) is true
Because both (3>2) and (5>=5) are true
p1 p2 p1 || Example
p2
false false false (2>3) || (5>5) is false
Because both (2>3) and (5>5) are false
false true true (2>3) || (6>5) is true
Because (6>5) is true
true false true (6>5) || (2>3) is true
Because (6>5) is true
true true true (3>2) || (5>=5) is true
Because both (3>2) and (5>=5) are true
p1 p2 p1 ^ p2 Example
false false false (2>3) ^ (5>5) is false
Because both (2>3) and (5>5) are false
false true true (2>3) ^ (6>5) is true
Because (2>3) is false and (6>5) is true
true false true (6>5) ^ (2>3) is true
Because (6>5) is true and (2>3) is false
true true false (3>2) ^ (5>=5) is true
Because both (3>2) and (5>=5) are true
• Operator XOR (^) is true when both if its operands has different condition.
• When its operands has same condition then XOR is false.
Bina Nusantara 44
Tabel Kebenaran: Demo
Bina Nusantara
Truth Table: Demo
Bina Nusantara
Thruth table: Leap Year
Bina Nusantara
Did You Know?
• When evaluating p1&&p2, Java evaluates p1 first.
– If p1 is true then Java evaluates p2.
– If p2 is false then Java doesn’t evaluate p2.
• When evaluating p1||p2, Java evaluates p1 first
– If p1 is true then Java doesn’t evaluate p2.
– If p1 is false then Java evaluates p2.
Bina Nusantara
Exercise
1. Assuming that x is 1, show the result of the following
Boolean expressions.
– (true) && (3 > 4)
– !(x > 0) && (x > 0)
– (x != 1) == !(x == 1)
– (x >= 0) || (x < 0)
2. List the precedence order of the Boolean operators.
Evaluate the following expressions:
– 2 * 2 – 3 > 2 && 4 – 2 > 5
– 2 * 2 – 3 > 2 || 4 – 2 > 5
Bina Nusantara 50