Dasar Pemrograman Java Nurochman
Sejarah Java y Pada 1991, sekelompok insinyur Sun dipimpin oleh Patrick Naughton dan James Gosling menggarap Proyek ”Green” Green y Tujuan merancang bahasa komputer untuk perangkat konsumer seperti cable TV Box: memori cable TV Box: memori kecil dan prosesor yg macam2 y Mulanya bahasa yang diciptakan yang diciptakan diberi nama ”Oak” Oak oleh James Gosling y Nama JAVA sendiri JAVA sendiri terinspirasi pada saat mereka sedang menikmati secangkir kopi di sebuah kedai kopi
Karakteristik y Sederhana : mirip C++, tanpa pointer, single Inheritance, automatic memory allocation dan memory garbage collection. ll ti y Berorientasi objek (Object Oriented) y Dapat D didi ib i dengan didistribusi d mudah d h : libraries networking lib i ki yang terintegrasi pada Java. y Interpreter (JVM) ‐> multi platform I t t (JVM) > lti l tf y Robust : runtime‐Exception handling y Aman: mekanisme keamanan untuk menjaga aplikasi k k k lk tidak digunakan untuk merusak sistem komputer
Karakteristik y Architecture Neutral: platform independent y Portabel: program Java dapat dengan mudah dibawa ke platform yang berbeda‐beda tanpa harus dikompilasi ulang y Performance: Namun P f N performance Java dapat f J d ditingkatkan menggunakan kompilasi Java lain seperti buatan Inprise Microsoft ataupun Symantec yang buatan Inprise, Microsoft ataupun Symantec yang menggunakan Just In Time Compilers (JIT). y Multithreaded y Dinamis : penambahan fitur pd sebuah class tidak mengganggu program yg program yg menggunakan class tsb. class tsb.
Fitur Java • Java Virtual Machine (JVM) melakukan interpretasi terhadap bytecode yg dihasilkan dr proses kompilasi • Garbage Collection mencegah memory leaks. memory leaks • Code Security
Elemen Bahasa Java •Types •Literals •Variables •Operators •Control Structures •Control Structures •Exceptions •Arrays
Tipe Data • Tipe data primitif • Tipe data data reference reference
Tipe data primitif y char: unicode (2 bytes each!) y Integral types ◦ ◦ ◦ ◦
byte: b t 8 bit signed 2's complement integer 8 bit i d 2' l ti t Short:16 bit signed 2's complement integer int: 32 bit signed 2's complement integer g p g long: 64 bit signed 2's complement integer
y Floating Point
◦ float 32 bit IEEE 754 ◦ double 64 bit IEEE 754 (double precision)
y 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 bits Float 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 figs Precision 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 boolean boolean 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. 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) 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 form exponential 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 a = 2.0E2; sets a to 2 2 0E2; sets a to 2*100 100 which is equal to 200 which is equal to 200
Float VS Double • Java supports two floating point types float and double • Literal numerics with a decimal point are assumed to be double unless an 'f' suffix is used d t b d bl l 'f' ffi i d – 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 zero or more characters in " (double quote (double quote marks) • a = ""; (empty string) • b b = "Hello Hello World\n 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 reserved Some 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 letter with a lower case letter • Words within the name are capitalised – heightOfBox • Class names begin with a capital letter Class names begin with a capital letter – HelloWorldApplet
Declarations y y y y y y y y y y y y y
byte age, month; short time; int x; long hours; float phase; d bl di t double 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 used int 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.
• FFor primitive type variables, a variable holds the i ii i bl i bl h ld h 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 letters, digits and the underscore character '_'. di it d th d h t ' ' – The character '$' is also supported, but is generally only used by code generators, not by humans.
• The The first character of an identifier must not be a 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 exception parameters (method, constructor, exception handler) l l local variables bl array components y p
Konstanta • Kata kunci final class 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 changed changed • Follows C/C++ syntax: – (type) variable‐name • e.g. eg float x = 3.5f; int a = (int) x;
Pertanyaan???