Вы находитесь на странице: 1из 59

Pengenalan Bahasa

Pemrograman Java
Popular High-Level
Languages
Language Description

Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ C++ is an object-oriented language, based on C.
C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
Python A simple general-purpose scripting language good for writing short programs.
Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
Basic graphical user interfaces.

7
Apa itu Java
• Java merupakan bahasa pemrograman berorientasi objek dan lintas
platform yang dikembangkan oleh SUN Microsystem
Keunggulan java
• Java sebagai bahasa pemrograman memiliki berbagai keunggulan
antara lain:
1. mempunyai struktur yang sederhana (mirip C++)
2. object oriented
3. Multiplatform
4. secure
5. mendukung Distributed computing
6. mendukung Mission critical software
Terminologi
• Java Developers Kit (JDK)
merupakan kumpulan utilitas yang diperlukan untuk mengkompilasi
program java
• Java Software Development Kit (J2SDK)
merupakan gabungan dari JDK dan utilitas untuk menjalankan
program java pada PC
• Java API (Application Programming interface)
Merupakan kumpulan library pada Java yang menyediakan modul -
modul generik yang dibutuhkan dalam pengembangan program
JDK EDITIONS
• Java Card for smartcards.
• Java Platform, Micro Edition (Java ME) — targeting environments with
limited resources.
• Java Platform, Standard Edition (Java SE) — targeting workstation
environments.
• Java Platform, Enterprise Edition (Java EE) — targeting large
distributed enterprise or Internet environments.
JAVA IDE TOOLS
• Forte by Sun MicroSystems
• Borland JBuilder
• Microsoft Visual J++
• Netbean
• IBM Visual Age for Java
A Simple Java Program
Listing 1.1
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

20
Creating, Compiling, and Running
Programs

21
animation

Trace a Program Execution


Enter main method

// This program prints Welcome to Java!


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

22
animation

Trace a Program Execution


Execute statement

// This program prints Welcome to Java!


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

23
animation

Trace a Program Execution

// This program prints Welcome to Java!


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

print a message to the


console

24
Anatomy program java
• Class name
• Main method
• Statements
• Statement terminator
• Reserved words
• Comments
• Blocks

25
Class Name
Setiap program java harus memiliki minimal sebuah class.
Setiap class memiliki nama, sesuai konvensi nama sebuah
class dimulai dengan huruf kapital

// This program prints Welcome to Java!


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

26
Main Method
Baris kedua menyatakan method main. Untuk menjalankan
class maka sebuah class harus mengandung method
bernama main. Method main merupakan titik awal
eksekusi program

// This program prints Welcome to Java!


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

27
Statement
Statement menyatakan aksi atau urutan aksi. statement
System.out.println("Welcome to Java!") dalam listing
program dibawah adalah untuk mencetak teks
"Welcome to Java!“.

// This program prints Welcome to Java!


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

28
Statement Terminator

setiap statement dalam java 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!");
}
}

29
Reserved words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the
word after class is the name for the class.

// This program prints Welcome to Java!


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

30
Blocks

Pasangan tanda kurung kurawal menandakan blok


program

public class Test {


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

31
Special Symbols

Character Name Description

{} Opening and closing Denotes a block to enclose statements.


braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.

32
{ …}

// This program prints Welcome to Java!


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

// This program prints Welcome to Java!


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

// This program prints Welcome to Java!


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

// This program prints Welcome to Java!


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

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
37
Cara menulis program dan
dokumentasi
• Comment yang bermanfaat
• Aturan penamaan
• Proper Indentation and Spacing Lines
• Block Styles

38
Comment bermanfaat
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 program.

39
Convensi nama
• Choose meaningful and descriptive names.
• Class names:
• Capitalize the first letter of each word in the name. For
example, the class name ComputeExpression.

40
Proper Indentation and
Spacing
• Indentation
• Indent two spaces.

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

41
Block Styles
Use end-of-line style for braces.

Next-line public class Test


style {
public static void main(String[] args)
{
System.out.println("Block Styles");
}
}

End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}

42
Programming Errors
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result

43
Syntax Errors
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}

44
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}

45
Logic Errors

public class ShowLogicErrors {


public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}

46
Identifier
• Identifier merupakan penamaan untuk entitas program seperti
variabel, method, konstan, class, dan package
• Aturan pemberian nama identifier antara lain :
- Dapat terdiri dari huruf, angka, underscore(_) dan (&)
- Harus diawali dengan huruf, angka atau tanda &
- Tidak dapat menggunakan keyword yang digunakan dalam java
Variabel
• Variabel digunakan untuk menyimpan data dalam program dan isinya dapat berubah
sepanjang program berjalan
contoh:
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is " + area + " for radius " + radius);

• Mendeklarasikan variabel
Syntax : datatype variableName;
contoh :
int x;
double radius;
char a;
Konstanta
• Konstanta mengandung data yang sifatnya permanen dan tidak dapat
diganti sepanjang program berjalan

• Syntax:
final datatype CONSTANTNAME = VALUE;

• Konstanta harus dideklarasikan dan dinisialisasi dalam satu statement


contoh:
final double PI = 3.14159;
Tipe Data Numeric
Operator aritmetik
• Operator aritmetika dalam java sama dengan yang terdapat dalam C

• Operator aritmetika mendukung penulisan singkat/shorthand


mis = i+=8 // sama dengan i= i+8
• Operator increment dan decremen sama dengan C
contoh : ++var , var++, --var, var --
Tipe data Character dan string
• Tipe data char digunakan untuk menyimpan karakter tunggal
contoh:
char letter = 'A';
char numChar = '4';

• Dalam java dikenal tipe string untuk menyatakan string karakter


contoh:
String message = "Welcome to Java";

• String bukan merupakan tipe data primitif tapi reference karena string merupakan sebuah class dalam java
• Untuk menyambung string(String concat) digunakan operator +
cth: String message = "Welcome" + "to" + "Java";
Masukan melalui input dialog

Untuk mengkonversi string ke integer value gunakan method parse Int dari class integer, seperti berikut
int intValue = Integer.parseInt(intString);

Konvert string ke double:


double doubleValue = Double.parseDouble(doubleString);
Masukan melalui console
• Java menggunakan System.out untuk mengacu ke perangkat output
standar (monitor) dan system.in untuk mengacu ke perangkat input
standar (keyboard)
• Untuk console output: printf(), println()
• Untuk console input : Class Scanner, Class BufferedReader
Class Scanner
• Membuat Objek dari class Scanner
Scanner scanner = new Scanner(System.in);

• Method dari objek Scanner


next(): Baca data String
nextByte(): Baca angka bertipe data byte
nextShort(): baca angka bertipe data short
nextInt(): baca angka bertipe data Integer
nextLong(): baca angka bertipe data Long
nextFloat(): baca angka bertipe float
nextDouble(): baca angka bertipe double

• Contoh meminta input bertipe double dari user via console


System.out.print(“Masukkan sebuah angka : ");
Scanner scanner = new Scanner(System.in);
int d = scanner.nextInt();
Output ke Message dialog
Konvensi penamaan
• Konvensi penamaan merupakan kesepakatan umum dalam melakukan penamaan terhadap
variabel,konstanta,method dan class
• Berikut penamaan konvensi:
- gunakan huruf kecil(lowercase) untuk variabel dan method .jika nama terdiri dari beberapa kata
maka disambung tanpa spasi ,dan untuk kata kedua dan seterusnya diawali dengan huruf kapital
contoh: radius,area,showMessageDialog
- gunakan huruf kapital pada awal kata untuk menamakan class
contoh: HitungLuas,Math,JOptionPane
- untuk konstanta gunakan huruf besar ditiap hurufnya,dan untuk tiap kata pisahkan dengan
underscore(_)
contoh: MAX_VALUE
Operator relasional dan
logical

Вам также может понравиться