• Tidak ada hasil yang ditemukan

Anatomi program dalam Java

N/A
N/A
Protected

Academic year: 2018

Membagikan "Anatomi program dalam Java"

Copied!
34
0
0

Teks penuh

(1)

1

Anatomi program dalam Java

Class name

Main method

Statements

Statement terminator

Reserved words

Comments

(2)

// This program prints Welcome to Java! public class Welcome {

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

Class Name

Program dalam Java minimal memiliki 1

class

(3)

3

// This program prints Welcome to Java! public class Welcome {

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

}

Main Method

Bagian dibawah ini adalah

main method

(4)

// This program prints Welcome to Java! public class Welcome {

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

Statement

Statement

merupakan urutan proses atau aktivitas

tertentu. Pada

statement

:

System.out.println("Welcome to Java!")

(5)

5

// This program prints Welcome to Java! public class Welcome {

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

}

Statement Terminator

(6)

// 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

(7)

7

Blocks

Sepasang kurung kurawal yang

menunjukkan sekelompok komponen

program

public class Test {

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

}

(8)

Special Symbols

Character Name Description {} () [] // " " ;

Opening and closing braces

Opening and closing parentheses

Opening and closing brackets

Double slashes

Opening and closing quotation marks Semicolon

Denotes a block to enclose statements. Used with methods.

Denotes an array.

Precedes a comment line.

(9)

9

Programming Style and

Documentation

Pemberian komentar yang sesuai

Aturan penamaan

(10)

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.

Include your name, class section, instructor, date,

and a brief description at the beginning of the

(11)

11

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

(12)

Proper Indentation and Spacing

Indentation

Indent two spaces.

Spacing

(13)

13

Programming Errors

Syntax Errors

Detected by the compiler

Runtime Errors

Causes the program to abort

Logic Errors

(14)

Chapter 2 Primitive Data Types and

Operations

• 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

Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -,

(15)

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,

“Java Keywords,” for a list of reserved words).

• An identifier cannot be true, false, or

null.

(16)

Variables

// Compute the first area radius = 1.0;

area = radius*radius*3.14159;

System.out.println("The area is “ + area + " for radius "+radius);

// Compute the second area radius = 2.0;

(17)

Declaring Variables

int x; // Declare x to be an // integer variable;

double radius; // Declare radius to

// be a double variable;

(18)

Assignment Statements

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

(19)

Declaring and Initializing

in One Step

int x = 1;

double d = 1.4;

float f = 1.4;

(20)

Constants

final datatype CONSTANTNAME = VALUE;

(21)

Numerical Data Types

byte

8 bits

short

16 bits

int

32 bits

long

64 bits

float

32 bits

(22)

Operators

+, -, *, /, and %

5/2 yields an integer 2.

5.0/2 yields a double value 2.5

(23)

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

System.out.println(1.0 - 0.9);

displays 0.09999999999999998, not 0.1.

Integers

are

stored

precisely.

(24)

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:

int i = 34;

long l = 1000000;

(25)

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.

An integer literal is assumed to be of the int

type, whose value is between -231

(-2147483648) to 231–1 (2147483647). To denote

an integer literal of the long type, append it with the letter L or l. L is preferred

because l (lowercase L) can easily be

(26)

Floating-Point Literals

Floating-point

literals

are

written

(27)

Scientific Notation

(28)

Arithmetic Expressions

)

9

4

(

9

)

)(

5

(

10

5

4

3

y

x

x

x

c

b

a

y

x

is translated to

(3+4*x)/5

10*(y-5)*(a+b+c)/x +

(29)

Shortcut Assignment Operators

Operator

Example

Equivalent

+=

i+=8

i = i+8

-=

f-=8.0

f = f-8.0

*=

i*=8

i = i*8

/=

i/=8

i = i/8

(30)

Increment and

Decrement Operators

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

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

x

––

;

// Same as x = x - 1;

––

x;

// Same as x = x - 1;

suffix

prefix

suffix

(31)

Increment and

Decrement Operators, cont.

int i=10;

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

Equivalent to

int i=10;

int newNum = 10*(++i); i = i + 1;

int newNum = 10*i;

(32)

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

variables, or the same variable for

(33)

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 %

(34)

Numeric Type Conversion

Consider the following statements:

byte i = 100;

long k = i*3+4;

double d = i*3.1+k/2;

int x = k; //(Wrong)

Referensi

Dokumen terkait

Dari tabel di atas, menunjukkan bahwa Ha diterima yang berarti ada hubungan positif assurance dengan kepuasan.. kunjungan wisatawan mancanegara. Koefisien yang dihasilkan yaitu

Pada tugas akhir ini akan di analisa model Brusselator, dengan acuan jurnal internasional 2015 yang digunakan sebelumnya berjudul Dynamic and Control of Brusselator

Kegempaan masa lampau terjadi pada Miosen Akhir – Pliosen Awal dengan ditandai adanya sand boil dan karbon di sekitar sesar naik serta adanya zona fracture

Dari kedua tinjauan perpustakaan di atas maka dapat diambil beberapa hal yang nantinya akan diterapkan dalam perancangan Cengkareng Park Library (CPL) ini antara lain:

Salah satu jenis jagung fungsional yang diteliti dan dikembangkan di Balai Penelitian Tanaman Serealia (Balitsereal) adalah jagung kaya betakaroten dengan kandungan vitamin A

bahkan Tergugat sudah menikah lagi dengan perempuan lain dan kini Penggugat tetap bersikeras untuk bercerai;--- Menimbang, bahwa dalam suatu rumah tangga jika suami istri tidak

Dengan mengamati guru bernyanyi, siswa dapat mengidentifikasi bentuk pola irama sederhana dalam lagu dengan tepat.. Setelah mengidentifikasi pola irama sederhana,

Atlet berpikir bahwa latihan yang mereka ikuti tidak dapat membawa atlet untuk mencapai cita-cita sebagai atlet Bulutangkis Nasional, menunjukan komitmen latihan