2 - dasar-dasar java.ppt - new line · pdf file02/09/2010 · kecildanprosesor yg...
TRANSCRIPT
Dasar Pemrograman Java
Nurochman
Sejarah Java
Pada 1991, sekelompok insinyur Sun dipimpin olehPatrick Naughton dan James Gosling menggarapProyek ”Green”Proyek Green Tujuan merancang bahasa komputer untukperangkat konsumer seperti cable TV Box: memoriperangkat konsumer seperti cable TV Box: memorikecil dan prosesor yg macam2Mulanya bahasa yang diciptakan diberi nama ”Oak”Mulanya bahasa yang diciptakan diberi nama Oak oleh James GoslingNama JAVA sendiri terinspirasi pada saat merekaNama JAVA sendiri terinspirasi pada saat merekasedang menikmati secangkir kopi di sebuah kedaikopi
Karakteristik
Sederhana : mirip C++, tanpa pointer, single Inheritance, automatic memory allocation dan memory garbage ll ticollection.
Berorientasi objek (Object Oriented)D didi ib i d d h lib i kiDapat didistribusi dengan mudah : libraries networking yang terintegrasi pada Java.I t t (JVM) > lti l tfInterpreter (JVM) ‐> multi platformRobust : runtime‐Exception handling
k k k l kAman: mekanisme keamanan untuk menjaga aplikasi tidak digunakan untuk merusak sistem komputer
Karakteristik
Architecture Neutral: platform independentPortabel: program Java dapat dengan mudah dibawa keplatform yang berbeda‐beda tanpa harus dikompilasiulangP f N f J dPerformance: Namun performance Java dapat ditingkatkan menggunakan kompilasi Java lain seperti buatan Inprise Microsoft ataupun Symantec yangbuatan Inprise, Microsoft ataupun Symantec yang menggunakan Just In Time Compilers (JIT).MultithreadedMultithreadedDinamis : penambahan fitur pd sebuah class tidakmengganggu program yg menggunakan class tsb.mengganggu program yg menggunakan class tsb.
Fitur Java
• Java Virtual Machine (JVM)
melakukan interpretasi terhadap bytecode ygmelakukan interpretasi terhadap bytecode ygdihasilkan dr proses kompilasi
• Garbage Collectionmencegah memory leaksmencegah memory leaks.
• Code Security
Elemen Bahasa Java
•Types•Literals•Literals•Variables•Operators•Control Structures•Control Structures•Exceptions•Arrays
Tipe Data
• Tipe data primitif
• Tipe data reference• Tipe data reference
Tipe data primitif
char: unicode (2 bytes each!)Integral typesb t 8 bit i d 2' l t i t◦ byte: 8 bit signed 2's complement integer◦ Short:16 bit signed 2's complement integer◦ int: 32 bit signed 2's complement integerg p g◦ long: 64 bit signed 2's complement integerFloating Point◦ float 32 bit IEEE 754
◦ double 64 bit IEEE 754 (double precision)
boolean: true or false only◦ not like C/C++: 0 doesn't mean false !
Numbers ‐ Integers
• Four integer types
– byte 8 bits ‐128 127– byte 8 bits ‐128 .. 127
– short 16 bits ‐32768 .. 32767
– int 32 bits ‐2147483648 .. 2147483647
long 64 bits 9223372036854775808– long 64 bits ‐9223372036854775808
.. 9223372036854775807
Numbers – Floating Points
• Two types float and double
Float Double Size 32 bits 64 bitsFloat Double Size 32 bits 64 bits
“Largest Value” ±3.4E+38 ±1.79E308
“Smallest Value” ±1.4E‐45 ±4.9E‐324
Precision 6 7 sig figs 14 15 sig figsPrecision 6‐7 sig. figs 14‐15 sig. figs.
Characters
• 16 bit Unicode values
• Equivalent to a short integer value• Equivalent to a short integer value
• May assign int to char
– char theChar = 48;
• May assign char to int• May assign char to int
– int number = 'a';
booleans
• Can be true or false
• Declared with the keyword boolean• Declared with the keyword booleanboolean flag = true;
Tipe Reference
• Everything that is not a primitive type is a reference type.reference type.
• Three kinds of reference types:– Classes
– Arrays
– Interfaces
Literal
Numbers– Integer• Normally 32 bits (‐2147483648 to 2147483647)• Can be long integer i.e. 64 bits• Can be expressed in hex or octal• Can be expressed in hex or octal
– Floating Point• any number with a decimal point any number with a decimal point• can be expressed in exponential form
Integers are normally 32 bits long but long integers are 64 bits long.Integers are normally 32 bits long but long integers are 64 bits long.
– a = 2; Here 2 is a 32 bit integer literal– a = 2L; Here 2 is a 64 bit integer literal
Literal
Hexadecimal values can be used by
prefixing a value 0x or 0X (zero‐x)prefixing a value 0x or 0X (zero‐x)
– a = 0xD; The variable a is set to 13 (Hexadecimal D)
Octal values are prefixed with an 0 (zero)Octal values are prefixed with an 0 (zero)
– b = 055; The variable b is set to 45 ((5*8)+5).
Exponential Format
Floating point values can also be expressed in exponential formexponential form
Append a trailing e or E and the exponent value.
– a = 2.0E1; sets a to 2*10 which is equal to 20
– a = 2 0E2; sets a to 2*100 which is equal to 200a 2.0E2; sets a to 2 100 which is equal to 200
Float VS Double
• Java supports two floating point types float and doubledouble
• Literal numerics with a decimal point are d t b d bl l 'f' ffi i dassumed to be double unless an 'f' suffix is used
– float vatRate = 17.5f
Literal
• Characters
– 16 bit values in single quotes– 16 bit values in single quotes
– Unicode values
– recognised escape sequences
• Booleans• Booleans
– true
– false
Literal
• Strings
– zero or more characters in " (double quote– zero or more characters in (double quote marks)
• a = ""; (empty string)
• b = "Hello World\n"; b Hello World\n ;
• c = "The string \"Hello World\"";
Idetifiers
• Names for classes, variables, methods in Java
• Must begin with an alphabetic character• Must begin with an alphabetic character
• Can contain upper and lower case letters, numbers, _ and $
• Some names are reservedSome names are reserved
Examples
• face is a valid identifier• 1face is invalid it begins with a number• 1face is invalid, it begins with a number
• face% is invalid because % is not allowed• face3 is valid• Face is valid but is not the same as face• Face is valid but is not the same as face
Naming Convention
• Variable and method names normally start with a lower case letterwith a lower case letter
• Words within the name are capitalised
– heightOfBox
• Class names begin with a capital letterClass names begin with a capital letter
– HelloWorldApplet
Declarations
byte age, month;short time;int x;int x;long hours;float phase;d bl di tdouble distance;char initial, input;boolean flag;g;int x=1,z=4,t;double phase = 4e3.6;float w 3e7 3f;float w=3e7.3f;char alpha='a';boolean flag=true,p;
Initialising
• Declarations can appear anywhere in code
• Variables must be initialised before being used• Variables must be initialised before being usedint i;i=i+1;
syntax error!syntax error!
Variables
• Storage location (chunk of memory) and an associated type.associated type.– type is either a primitive type or a reference type.
F i i i i bl i bl h ld h• For primitive type variables, a variable holds the actual value (the memory is used to store the value).
• For reference type variables a variable holds a• For reference type variables, a variable holds a reference to an object.
Variable Names
• Variable names are identifiers (so are class names, interface names, method names, ...)names, interface names, method names, ...)
• Identifiers are made of any length sequence of l tt di it d th d h t ' 'letters, digits and the underscore character '_'.– The character '$' is also supported, but is generally only used by code generators, not by humans.
• The first character of an identifier must not be aThe first character of an identifier must not be a digit.
Kinds of variables
• There are lots of kinds of variables, we will bump in to the various kinds as we go...bump in to the various kinds as we go...
class variables (static)
instance variables
parameters (method constructor exceptionparameters (method, constructor, exception handler)
l l bllocal variables
array componentsy p
Konstanta
• Kata kunci finalclass Calendar {{final int monthsInYear = 12;
}}
• monthsInYear can’t be changed
Java Comments
• Block comments/*..*/
• Single line comments
// This comment terminates at the end of the line
• Documentation comments/**..*/
Casting
• Allows the type of an expression to be explicitly changedchanged
• Follows C/C++ syntax:– (type) variable‐name
• e ge.g.float x = 3.5f;int a = (int) x;int a = (int) x;
Pertanyaan???