Teknik Kompilasi - Sekolah Tinggi Teknik Surabaya

advertisement
1
Lecture 1
Introduction to Java
Erick Pranata
© Sekolah Tinggi Teknik Surabaya
» Getting Started with Java
» Variables , Constants, Data Types and
Operators
» Arrays
» Control Flow Statements
» Misc.
2
© Sekolah Tinggi Teknik Surabaya
3
© Sekolah Tinggi Teknik Surabaya
» Java Standard Edition (Java SE)
˃ J2SE can be used to develop client-side
standalone applications or applets.
» Java Enterprise Edition (Java EE)
˃ J2EE can be used to develop server-side
applications such as Java Servlets and Java
Server Pages.
» Java Micro Edition (Java ME).
˃ J2ME can be used to develop applications
for mobile devices such as cell phones.
© Sekolah Tinggi Teknik Surabaya
4
» TextPad
» jEdit
» jCreator
» JBuilder
» Eclipse
» NetBeans
» Etc.
5
© Sekolah Tinggi Teknik Surabaya
» Java Development Kit
» Java Runtime Environment
˃ Java Virtual Machine
6
© Sekolah Tinggi Teknik Surabaya
Something.java
Something.class
7
© Sekolah Tinggi Teknik Surabaya
class Something {
public static void main(String[] args) {
System.out.println(“Hello World!");
}
}
8
© Sekolah Tinggi Teknik Surabaya
9
© Sekolah Tinggi Teknik Surabaya
» Started with a letter or dollar sign “$”
or underscore “_”
˃ line, _line, $line
» Subsequent characters may be letters,
digits, dollar signs, or underscore
characters
» Variable names must be in mixed case
starting with lowercase
˃ line, inputLine, i, j, currNode
10
© Sekolah Tinggi Teknik Surabaya
» Must be all uppercase using underscore
to separate words
˃ final int NUMBER_OF_HOURS_IN_A_DAY = 24;
11
© Sekolah Tinggi Teknik Surabaya
Data Types
Default
Value
Remark
byte
8-bit signed: -128 to 127 (inclusive)
0
short
16-bit signed: -32.768 to 32.767 (inclusive)
0
int
32-bit signed: -2,147,483,648 to
2,147,483,647 (inclusive)
0
long
64-bit signed: -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (inclusive)
0
float
32-bit floating point
0.0f
double
64-bit floating point
0.0d
boolean
true or false
false
char
16-bit unicode character
'\u0000'
String
-
null
12
© Sekolah Tinggi Teknik Surabaya
» The source code representation of a
fixed value; literals are represented
directly in your code without requiring
computation.
» Example
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;
13
© Sekolah Tinggi Teknik Surabaya
// The number 26, in decimal
int decVal = 26;
// The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;
14
© Sekolah Tinggi Teknik Surabaya
double d1 = 123.4;
/**
* same value as d1,
* but in scientific notation
**/
double d2 = 1.234e2;
float f1 = 123.4f;
15
© Sekolah Tinggi Teknik Surabaya
» The Java programming language also
supports a few special escape
sequences for char and String literals:
˃ \b (backspace)
˃ \t (tab)
˃ \n (line feed)
˃ \f (form feed)
˃ \r (carriage return)
˃ \" (double quote)
˃ \' (single quote)
˃ \\ (backslash).
© Sekolah Tinggi Teknik Surabaya
16
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
» Constraints:
˃ At the beginning or end of a number
˃ Adjacent to a decimal point in a floating point
literal
˃ Prior to an F or L suffix
˃ In positions where a string of digits is
expected
© Sekolah Tinggi Teknik Surabaya
17
Operators
Precedence
postfix
expr++ expr--
unary
++expr --expr +expr -expr ~ !
multiplicative
*/%
additive
+-
shift
<< >> >>>
relational
< > <= >= instanceof
equality
== !=
bitwise AND
&
bitwise exclusive OR
^
bitwise inclusive OR
|
logical AND
&&
logical OR
||
ternary
?:
assignment
= += -= *= /= %= &= ^= |= <<= >>=
>>>=
© Sekolah Tinggi Teknik Surabaya
18
19
© Sekolah Tinggi Teknik Surabaya
20
© Sekolah Tinggi Teknik Surabaya
// declares an array of integers
int[] anArray;
21
© Sekolah Tinggi Teknik Surabaya
// create an array of integers
anArray = new int[10];
// create + initializing
int[] anArray = {
100, 200, 300, 400,
500, 600, 700, 800,
900, 1000
};
// accessing
System.out.println
("Element 1 at index 0: " + anArray[0]);
© Sekolah Tinggi Teknik Surabaya
22
23
© Sekolah Tinggi Teknik Surabaya
» if-then Statement
if (isMoving){
currentSpeed--;
}
» if-then-else Statement
if (testscore >= 90)
grade = 'A';
} else if (testscore
grade = 'B';
} else if (testscore
grade = 'C';
} else if (testscore
grade = 'D';
} else {
grade = 'F';
}
© Sekolah Tinggi Teknik Surabaya
{
>= 80) {
>= 70) {
>= 60) {
24
switch (month) {
case 1:
monthString =
break;
case 2:
monthString =
break;
case 3:
monthString =
break;
default:
monthString =
break;
}
© Sekolah Tinggi Teknik Surabaya
"January";
"February";
"March";
"Invalid month";
25
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
26
© Sekolah Tinggi Teknik Surabaya
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
27
© Sekolah Tinggi Teknik Surabaya
28
© Sekolah Tinggi Teknik Surabaya
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
Default
do
double
else
Enum
extends
final
finally
Float
for
goto
if
Implements
import
instanceof
int
Interface
long
native
new
Package
private
protected
public
Return
short
static
strictfp
super
29
© Sekolah Tinggi Teknik Surabaya
30
This page is intentionally left blank
© Sekolah Tinggi Teknik Surabaya
» Language Basics,
http://docs.oracle.com/javase/tutorial/j
ava/nutsandbolts/index.html
31
© Sekolah Tinggi Teknik Surabaya
Download