2 Dasar Pemrograman Java

31
Dasar Pemrograman Java Nurochman

description

Dasar Pemrograman Java

Transcript of 2 Dasar Pemrograman Java

Microsoft PowerPoint - 2 - Dasar-dasar Java.ppt [Compatibility Mode]Sejarah Java
Pada 1991, sekelompok insinyur Sun dipimpin oleh Patrick Naughton dan James Gosling menggarap Proyek ”Green”Proyek Green   Tujuan merancang bahasa komputer untuk perangkat konsumer seperti cable TV Box: memoriperangkat konsumer seperti cable TV Box: memori kecil dan prosesor yg macam2 Mulanya bahasa yang diciptakan diberi nama ”Oak”Mulanya bahasa yang diciptakan diberi nama Oak   oleh James Gosling Nama JAVA sendiri terinspirasi pada saat merekaNama JAVA sendiri terinspirasi pada saat mereka sedang menikmati secangkir kopi di sebuah kedai kopi
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 platform Robust : runtimeException handling
k k k l kAman: mekanisme keamanan untuk menjaga aplikasi  tidak digunakan untuk merusak sistem komputer
Karakteristik
Architecture Neutral: platform independent Portabel: program Java dapat dengan mudah dibawa ke platform yang berbedabeda tanpa harus dikompilasi ulang P 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). MultithreadedMultithreaded Dinamis : penambahan fitur pd sebuah class tidak mengganggu program yg menggunakan class tsb.mengganggu program yg menggunakan class tsb.
Fitur Java
• Code Security
Tipe Data
Tipe data primitif
char: unicode (2 bytes each!) Integral types b 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 integer Floating 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
– 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
• 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.4E45 ±4.9E324
Precision 6 7 sig figs 14 15 sig figsPrecision 67 sig. figs 1415 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';
• 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.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 (zerox)prefixing a value 0x or 0X (zerox)
– 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
– true
– false
Literal
• Strings
– zero or more characters in " (double quote– zero or more characters in   (double quote  marks)
• a = ""; (empty string)
• 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 used int i; i=i+1;
syntax error!syntax error!
• 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)
l l bllocal variables
}}
Java Comments
• Block comments /* . . */
• Documentation comments /** . . */
• Allows the type of an expression to be explicitly  changedchanged 
• Follows C/C++ syntax: – (type) variablename
• e ge.g. float x = 3.5f; int a = (int) x;int a = (int) x;
Pertanyaan???