Download presentation
Presentation is loading. Please wait.
Published byLeony Sudjarwadi Modified over 6 years ago
1
An overview of Java, Data types and variables
Lecture 4 from (Chapters 2 and 3) 2018/11/28
2
Elements of Java – lexical values, chapter 2 (pages 37-39)
White spaces: Java is a free-form language. There is no special rue for us to follow. Identifiers: Identifiers are used for class names, method names and variables names. They must not begin with a number. The following is correct: height, a2, $sign The following is incorrect: 2dco, yes/no Literals: A constant value is created by using literal such as 200 (integer), 12.3 (floating-point value), “DCO” (string) 2018/11/282018/11/28
3
Elements of Java – lexical values
Comments: // (single line) /*…… */ (block) /**….. */ (documentation for HTML format Separators: separators are used to terminate statements () parentheses {} braces [] brackets ; semicolon , comma . Period 2018/11/282018/11/28
4
Java Keywords – there are 49 words
abstract finally public assert3 float return boolean for short break goto1 static byte if strictfp2 case implements super catch import switch char instanceof synchronized class int this const1 interface throw continue long throws default native transient do new try double package void else private volatile extends protected while final It means that you cannot use it as a variable 2018/11/282018/11/28
5
Java Class The first program in last lecture you used print() and println() are from System class. There are several built-in class libraries that supports I/O, string handling, networking and graphics. 2018/11/282018/11/28
6
Data Types and Variables, chapter 3
Integers Floating point Characters Booleans Variables Type conversion (integer to floating) 2018/11/282018/11/28
7
Integers – 4 types Name Size (bits) Range byte 8 -128 to 127
short to 32767 int to long to 2018/11/282018/11/28
8
Example of byte, short, int and long
result 2018/11/282018/11/28
9
Example – out of range, (byte)
year > 127 2018/11/282018/11/28
10
Floating – 2 types Type size (bits) range
Double exp(-324) to 1.8 exp(308) float exp(-45) to 3.4 exp(38) 2018/11/282018/11/28
11
Example – long result 2018/11/282018/11/28
12
Example – out of range > 3.4e38 2018/11/282018/11/28
13
Characters Java uses two bytes to store characters (0 to 65536)
The standard of ASCII ranges from 0 to 127 For example, X in ASCII is 88 2018/11/282018/11/28
14
ASCII Table – in hexadecimal (16)
2018/11/282018/11/28
15
Example – character result 2018/11/282018/11/28
16
Character.isLetter(ch) – return true or false
2018/11/282018/11/28
17
Character.isDigit(ch) – return true or false
white space includes 0020, 0009, 000A, 000B and 000D 2018/11/282018/11/28
18
Booleans Java has a simple type called boolean.
It has two values, true or false For example, a = 4, b = 5, (a >b) is true For example, (4 = 4) is true For example, (3 > 4) is false 2018/11/282018/11/28
19
Example of boolean 2018/11/282018/11/28
20
Escape sequence Escape sequence uses “\u” to stand for white space. They are 16-bit values. Sequence name value \n newline \u000A \t tab \u0009 \b backspace \u0008 \r carriage return \u000D \f form feed \u000C \’ single quote 2018/11/282018/11/28
21
String Strings literals are similar to English. Examples are:
“hello how are you?” “three \blines” “\” This statement is quoted \”” 2018/11/282018/11/28
22
Example of String and Escape Sequence
newline 2018/11/282018/11/28
23
Variables In java, variables are basic storage. The basic format is
type identifier [= value], identifier [= value]; Examples are: int a =10, b = 20, c=30; int a, b, c; double a = 2.3, b = 4.5, d= 6.7; char x = ‘z’; 2018/11/282018/11/28
24
Type conversion Byte defines the value between -127 to 128. If you assign a value that is larger than 128, you have to use short or integer. This needs conversion. 2018/11/282018/11/28
25
Example type conversion overflow 2018/11/282018/11/28
26
Example type conversion truncated 2018/11/282018/11/28
27
Summary Integers – 4 types, byte, short, int and long
Floating point – 2 types, float and double Characters – ASCII, ‘X’ Booleans – true or false String – “How are you?” Variables – u67 (correct), 67u (incorrect) Type conversion (integer to floating) 2018/11/282018/11/28
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.