Anatomi program dalam Java

Anatomi program dalam Java

  Class name Main method Statements Statement terminator

  Class Name

  • Program dalam Java minimal memiliki 1 class
  • Nama kelas biasanya dimulai dengan huruf kapital, contoh pada potongan dibawah ini : Welcome

  // This program prints Welcome to Java!

  Main Method

  • Bagian dibawah ini adalah main method
  • Untuk menjalankan program sebuah kelas wajib memiliki main method (program pertama kali akan mengeksekusi yang ada di dalam main method)

  // This program prints Welcome to Java!

  

Statement

  merupakan urutan proses atau aktivitas

  Statement

  tertentu. Pada statement : System.out.println("Welcome to Java!") program akan mencetak teks

  "Welcome to Java!“.

  // This program prints Welcome to Java!

  Statement Terminator

  • Seluruh statement dalam Java harus diakhiri dengan tanda titik koma (;)

  // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

  // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Reserved words

  Reserved words or keywords adalah kata-kata yang

  memiliki makna tertentu yang dipahami oleh compiler yang tidak boleh digunakan untuk keperluan lainnya.

  Blocks Sepasang kurung kurawal yang menunjukkan sekelompok komponen program public class Test { Class block public static void main(String[] args) {

  

Special Symbols

Character Name Description {} braces Opening and closing Denotes a block to enclose statements. [] () parentheses Opening and closing Used with methods. brackets Opening and closing Denotes an array. " " Opening and closing // quotation marks Double slashes Enclosing a string (i.e., sequence of characters). Precedes a comment line.

Programming Style and Documentation

  Pemberian komentar yang sesuai Aturan penamaan Proper Indentation and Spacing Lines

Appropriate Comments

  Include a summary at the beginning of the

program to explain what the program does, its key

features, its supporting data structures, and any unique techniques it uses.

  Naming Conventions

Gunakan nama yang bermakna dan

deskriptif Class names:

  • – Capitalize the first letter of each word in the name. For example, the class name

Proper Indentation and Spacing

  Indentation – Indent two spaces.

  Spacing – Use blank line to separate segments of the code.

  Programming Errors Syntax Errors

  • – Detected by the compiler

  Runtime Errors

  • – Causes the program to abort

  Logic Errors

  • Introduce Programming with an Example • Identifiers, Variables, and Constants • Primitive Data Types
    • – byte, short, int, long, float, double, char, boolean

  • Expressions • Operators, Precedence, Associativity, Operand

Identifiers

  • An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($).
  • An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit.
  • An identifier cannot be a reserved word. (See Appendix A,

Variables

  // Compute the first area radius = 1.0; area = radius*radius*3.14159; System.out.println("The area is “ + area + " for radius "+radius);

Declaring Variables

  int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a

Assignment Statements

  x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;

Declaring and Initializing in One Step

  • int x = 1;
  • double d = 1.4;
  • float f = 1.4;

Constants

  final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;

  

Numerical Data Types

8 bits byte

  16 bits short

  32 bits int

  64 bits

  

Operators

  • , -, *, /, and % 5/2 yields an integer 2. 5.0/2 yields a double value 2.5

NOTE

  Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example,

  System.out.println(1 - 0.1 - 0.1 - 0.1

  • 0.1 - 0.1); displays 0.5000000000000001, not 0.5, and

  

Number Literals

  A

  literal

  is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements:

Integer Literals

  

An integer literal can be assigned to an

integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type.

Floating-Point Literals

  Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. You can make a number a float by appending the letter f or F, and

Scientific Notation

  Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as 1.23456e2, is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or

  e) represents an exponent and it can be either in Arithmetic Expressions

  3  4 x 10 ( y  5 )( abc )

  4 9  x

   9 (  )

  5

  x x y

  is translated to

  Shortcut Assignment Operators

Operator Example Equivalent

  • = i+=8 i = i+8
    • = f-=8.0 f = f-8.0
      • = i*=8 i = i*8

Increment and Decrement Operators

  x++; // Same as x = x + 1;

  • x; // Same as x = x + 1; suffix prefix suffix

Increment and Decrement Operators, cont

  int i=10; int newNum = 10* i++; int newNum = 10*i; Equivalent to i = i + 1;

Increment and Decrement Operators, cont

  Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple

Assignment Expressions and Assignment Statements

  Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements: variable op= expression; // Where op is +,

  • , *, /, or %

  Numeric Type Conversion

Consider the following statements:

  byte i = 100; long k = i*3+4; double d = i*3.1+k/2;


Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 785 7

OPTIMASI FORMULASI dan UJI EFEKTIVITAS ANTIOKSIDAN SEDIAAN KRIM EKSTRAK DAUN KEMANGI (Ocimum sanctum L) dalam BASIS VANISHING CREAM (Emulgator Asam Stearat, TEA, Tween 80, dan Span 20)

93 428 23

Analisis tentang saksi sebagai pertimbangan hakim dalam penjatuhan putusan dan tindak pidana pembunuhan berencana (Studi kasus Perkara No. 40/Pid/B/1988/PN.SAMPANG)

8 82 57

Diskriminasi Perempuan Muslim dalam Implementasi Civil Right Act 1964 di Amerika Serikat

3 37 15

Kekerasan rumah tangga terhadap anak dalam prespektif islam

7 68 74

Enriching students vocabulary by using word cards ( a classroom action research at second grade of marketing program class XI.2 SMK Nusantara, Ciputat South Tangerang

11 126 101

Kesesuaian konsep islam dalam praktik kerjasama bagi hasil petani desa Tenggulun Kecamatan Solokuro Kabupaten Lamongan Jawa Timur

0 80 111

Upaya guru PAI dalam mengembangkan kreativitas siswa pada mata pelajaran pendidikan agama islam Kelas VIII SMP Nusantara Plus Ciputat

39 235 84

Manajemen Sumber Daya Manusia dalam Peningkatan Produktivitas sekolah : penelitian di SMK al-Amanah Serpong

20 207 83

Konsep kecerdasan ruhani guru dalam pembentukan karakter peserta didik menurut kajian tafsir Qs. 3/Ali-‘Imran: 159

9 94 103

Dokumen baru

PENGARUH PENERAPAN MODEL DISKUSI TERHADAP KEMAMPUAN TES LISAN SISWA PADA MATA PELAJARAN ALQUR’AN HADIS DI MADRASAH TSANAWIYAH NEGERI TUNGGANGRI KALIDAWIR TULUNGAGUNG Institutional Repository of IAIN Tulungagung

64 1359 16

PENGARUH PENERAPAN MODEL DISKUSI TERHADAP KEMAMPUAN TES LISAN SISWA PADA MATA PELAJARAN ALQUR’AN HADIS DI MADRASAH TSANAWIYAH NEGERI TUNGGANGRI KALIDAWIR TULUNGAGUNG Institutional Repository of IAIN Tulungagung

23 368 43

PENGARUH PENERAPAN MODEL DISKUSI TERHADAP KEMAMPUAN TES LISAN SISWA PADA MATA PELAJARAN ALQUR’AN HADIS DI MADRASAH TSANAWIYAH NEGERI TUNGGANGRI KALIDAWIR TULUNGAGUNG Institutional Repository of IAIN Tulungagung

24 324 23

PENGARUH PENERAPAN MODEL DISKUSI TERHADAP KEMAMPUAN TES LISAN SISWA PADA MATA PELAJARAN ALQUR’AN HADIS DI MADRASAH TSANAWIYAH NEGERI TUNGGANGRI KALIDAWIR TULUNGAGUNG Institutional Repository of IAIN Tulungagung

6 210 24

PENGARUH PENERAPAN MODEL DISKUSI TERHADAP KEMAMPUAN TES LISAN SISWA PADA MATA PELAJARAN ALQUR’AN HADIS DI MADRASAH TSANAWIYAH NEGERI TUNGGANGRI KALIDAWIR TULUNGAGUNG Institutional Repository of IAIN Tulungagung

18 304 23

KREATIVITAS GURU DALAM MENGGUNAKAN SUMBER BELAJAR UNTUK MENINGKATKAN KUALITAS PEMBELAJARAN PENDIDIKAN AGAMA ISLAM DI SMPN 2 NGANTRU TULUNGAGUNG Institutional Repository of IAIN Tulungagung

27 405 14

KREATIVITAS GURU DALAM MENGGUNAKAN SUMBER BELAJAR UNTUK MENINGKATKAN KUALITAS PEMBELAJARAN PENDIDIKAN AGAMA ISLAM DI SMPN 2 NGANTRU TULUNGAGUNG Institutional Repository of IAIN Tulungagung

21 370 50

KREATIVITAS GURU DALAM MENGGUNAKAN SUMBER BELAJAR UNTUK MENINGKATKAN KUALITAS PEMBELAJARAN PENDIDIKAN AGAMA ISLAM DI SMPN 2 NGANTRU TULUNGAGUNG Institutional Repository of IAIN Tulungagung

8 223 17

KREATIVITAS GURU DALAM MENGGUNAKAN SUMBER BELAJAR UNTUK MENINGKATKAN KUALITAS PEMBELAJARAN PENDIDIKAN AGAMA ISLAM DI SMPN 2 NGANTRU TULUNGAGUNG Institutional Repository of IAIN Tulungagung

13 377 30

KREATIVITAS GURU DALAM MENGGUNAKAN SUMBER BELAJAR UNTUK MENINGKATKAN KUALITAS PEMBELAJARAN PENDIDIKAN AGAMA ISLAM DI SMPN 2 NGANTRU TULUNGAGUNG Institutional Repository of IAIN Tulungagung

21 428 23