Dasar Pemrograman Java

of 32 /32
Dasar Pemrograman Java Pertemuan 2 Pemrograman Berbasis Obyek Oleh Tita Karlita

Embed Size (px)

description

Dasar Pemrograman Java. Pertemuan 2 Pemrograman Berbasis Obyek Oleh Tita Karlita. Topik:. Membedakan antara valid dan invalid identifiers. Mengetahui Java technology keywords. Mengetahui 8 tipe data primitif. Mendefinisikan literal value untuk tipe data numerik dan tekstual. - PowerPoint PPT Presentation

Transcript of Dasar Pemrograman Java

  • Dasar Pemrograman JavaPertemuan 2Pemrograman Berbasis ObyekOleh Tita Karlita

  • Topik:Membedakan antara valid dan invalid identifiers.Mengetahui Java technology keywords.Mengetahui 8 tipe data primitif.Mendefinisikan literal value untuk tipe data numerik dan tekstual.Mendefinisikan primitive dan reference variable.Mendeklarasikan variabel bertipe class.Mengetahui nilai inisialisasi default.Konversi dan casting tipe data primitif.

  • IdentifiersNama yang digunakan oleh programer untuk memberi nama pada variable, class, atau method.Can start with a Unicode letter, underscore (_), or dollar sign ($) Are case-sensitive and have no maximum length Examples:

    1. foobar // legal2. BIGinterface // legal: embedded keywords3. // are OK.4. $incomeAfterExpenses // legal5. 3_node5 // illegal: starts with a digit6. !theCase // illegal: must start with7. // letter, $, or _

  • Java Keywords and Reserved Wordsare considered as reserved keywordsmay not be used as identifiers.None of the reserved words have a capital letters2 keywords that are reserved in Java but which are not used : const dan goto

    abstract

    do

    implements

    private

    this

    boolean

    double

    import

    protected

    throw

    break

    else

    instanceof

    public

    throws

    byte

    extends

    int

    return

    transient

    case

    false

    interface

    short

    true

    catch

    final

    long

    static

    try

    char

    finally

    native

    strictfp

    void

    class

    float

    new

    super

    volatile

    continue

    for

    null

    switch

    while

    default

    if

    package

    synchronized

  • Primitive TypesThe Java programming language defines eight primitive types: Logical - boolean Textual - char Integral - byte, short, int, and long Floating - double and float

  • Primitive Types

  • Literalsis a valuecannot appear on the left side of assignments.

  • Logical literalsThe boolean data type has two literals, true and false. For example, the statement:

    1. boolean isBig = true;2. boolean isLittle = false;Note: boolean literal tidak boleh berharga 0 atau 1

  • Textual LiteralsThe range: 0 ~ 216 - 1. Java characters are in Unicode character (16-bit encoding).

  • char literalsExpressed by enclosing the desired character in single quotes ( ).Example:char c = w;Express as a Unicode value specified using four hexadecimal digits, preceded by \uExample:char c1 = \u4567;

  • char literalsSpecial Characters \n for new line \r for return \t for tab \b for backspace \f for formfeed \ for single quote \ for double quote \\ for backslash

  • String literalsIs not a primitive data type; it is a classRepresent sequences of charactersHas its literal enclosed in double quotes ( )Example:String s = Characters in strings are 16-bit Unicode.;String s = Good Morning !! \n;

  • Integral literals byte, short, int and longExpressed in decimal, octal, or hexadecimal.2 The decimal value is 2 077 The leading 0 indicates an octal value 0xBAAC The leading 0x indicates a hexadecimal value

    Has a default type of int

    Specify a long integer by putting an 'L' or 'l' after the number. 'L' is preferred as it cannot be confused with the digit '1'. Example:long x = 25L;

  • Integral

  • Floating-Point literalsFloating point literal includes either a decimal point or one of the following:E or e (add exponential value)F or f (float)D or d (double)3.14 a simple floating point value (a double) 6.02E23 a large floating point value 2.718F a simple float size value 123.4E306D a large double valueDefault is doubleSpecify a float by putting an F' or f' after the number. Example:float x = 2.5F;

  • Note:Semua tipe data primitif yang numerik (selain char dan boolean) adalah signed.

  • Member Variables Initialization

  • Reference variable

  • Conversion of primitivesTerjadi pada saat kompile.Conversion of a primitives bisa terjadi pada:AssignmentMethod callArithmetic promotion

  • Primitive Conversion: AssignmentTerjadi ketika suatu nilai kita berikan pada suatu variabel yang tipe datanya berbeda dari data aslinya.Tipe data yang baru harus mempunyai ukuran lebih besar dari tipe data yang lama.

    1. int i;2. double d;3. i = 10;4. d = i; // Assign an int value to a double variable

    Nilai d = 10.0

  • Primitive Conversion: AssignmentContoh konversi yang illegal

    1. double d;2. short s;3. d = 1.2345;4. s = d; // Assign a double to a short variable

    Muncul error: possible loss of precisionKarena tipe data short lebih kecil dari double.

  • Aturan untuk primitive assignment conversionBoolean tidak bisa di konversi ke tipe data lainNon-boolean dapat di konversi ke tipe data lain selain boolean, konversi yang dilakukan adalah widening conversion

    Note: widening conversion adalah merubah tipe data suatu variabel ke tipe data yang ukuran bit nya lebih besar dari aslinya.

  • Javas widening conversionsFrom a byte to a short, an int, a long, a float, or a doubleFrom a short to an int, a long, a float, or a doubleFrom a char to an int, a long, a float, or a doubleFrom an int to a long, a float, or a doubleFrom a long to a float or a doubleFrom a float to a double

    Note: Konversi antar primitive types yang tidak mengikuti arah panah disebut dengan narrowing conversion.

  • Javas narrowing conversionsFrom a byte to a charFrom a short to a byte or a charFrom a char to a byte or a shortFrom an int to a byte, a short, or a charFrom a long to a byte, a short, a char, or an intFrom a float to a byte, a short, a char, an int, or a longFrom a double to a byte, a short, a char an int, a long, or a floatNote: Ubah arah panah!!

  • Primitive Conversion: AssignmentAda yang istimewa tentang integral literal assignment

    Ilegal : 1.234 adalah literal untuk double sehingga tidak bisa di berikan pada float.float f = 1.234;

    Legal: khusus untuk integral literal aturan assignment conversion dibebaskan.byte b = 1;short s = 2;char c = 3;

    Illegal: Pembebasan assignment conversion untuk integral literal hanya untuk assigment terhadap nilai.int i = 12;byte b = i; i adalah bukan nilai

  • Primitive Conversion: Method CallTerjadi ketika kita berusaha melewatkan suatu nilai variabel sebagai argumen suatu method, dimana tipe data variabel method tersebut berbeda dengan yang diterima.

    1. float frads;2. double d;3. frads = 2.34567f;4. d = Math.cos(frads); // Pass float to method // that expects double

    Hint: Math.cos(double d);Pada contoh diatas frands yang bertipe float akan secara otomatis di konversi menjadi double.Pada contoh diatas terjadi widening conversions.

  • Primitive Conversion: Method CallNarrowing conversions tidak diperbolehkanContoh ilegal:

    double d = 12.0; Object ob = myVector.elementAt(d);

    Hint : myVector.elementAt(int i);Hasil kompile: Incompatible type for method.Harus dilakukan casting secara eksplisit untuk mengkonversi dari double ke int

  • Primitive Conversion: Arithmetic PromotionTerjadi pada operasi matematika.Kompiler berusaha mencari tipe data yang sesuai dengan tipe data operan yang berbeda-beda.

    1. short s = 9;2. int i = 10;3. float f = 11.1f;4. double d = 12.2;5. if ((s * i) >= (f/d))6. System.out.println(>>>>);7. else8. System.out.println(

  • Aturan: Arithmatic PromotionUnary operators: +, -, ++, --, ~

    Jika operan bertipe byte, short, atau char, maka dikonversikan ke int

  • Aturan: Arithmatic PromotionBinary operators: +, -, *, /, %, >>, >>>,
  • Primitives and CastingCasting means explicitly telling Java to make a conversion.Cara: tambahkan tipe data yang diinginkan dalam tanda kurung sebelum nilai.

    1. int i = 5;2. double d = (double)i;

    Sama dengan:

    1. int i = 5;2. double d = i;

  • Primitives and CastingAre required when you want to perform a narrowing conversion.

    1. short s = 259;2. byte b = s; // Compile error3. System.out.println(s = + s + , b = + b);Pesan error = Explicit cast needed to convert short to byte.

    Solusi: dengan menambahkan casting1. short s = 259;2. byte b = (byte)s; // Explicit cast3. System.out.println(b = + b);

    Hasil : b = 3Kenapa 259 = 1 0000 0011The cast tells the compiler Yes, I really want to do it

    true, false, null reserved wordconst, goto is keyword not in used