Pemrograman Dasar

Post on 22-Feb-2016

92 views 1 download

description

Pemrograman Dasar. Basic Elements Of Java. Compiling and Running a Java Application . Portable Java App. Java Platform. Platform: hardware or software environment in which a program runs. Oracle has two products that implement Java Platform Standard Edition (Java SE) 7: - PowerPoint PPT Presentation

transcript

Pemrograman Dasar

Basic Elements Of Java

Compiling and Runninga Java Application

2

3

Portable Java App

Java Platform

Platform: hardware or software environment in which a program runs.

Oracle has two products that implement Java Platform Standard Edition (Java SE) 7: – Java SE Development Kit (JDK) 7 – Java SE Runtime Environment (JRE) 7.

4

Java Platform SE 7

5

Typical Java Development Environment

Basic Lexical Elements

Character set – 16-bit Unicode– Legal characters

Keywords– Reserved words, special meaning, illegal for identifiers

Identifiers– Names of declared entities, e.g. variables, constants

Variables– A variable is a storage location, something that can hold a value

to which a value can be assigned Literals

– Constants or values, e.g. 12, 17.9, “Hello”

7

Basic Lexical Elements

Other notations– Operators, e.g. +, -, *, /, etc.– Block symbols, e.g. pair of {}

Comments– Help developers, ignored by compiler– e.g.

/* Program 1 */// Function to count Circle area

8

Character Set

Java programs are written using Unicode character set (16 bit), which include:

Capital letters: A .. Z Small letters: a .. z Numbers: 0 .. 9 Punctuation marks, e.g. ‘!’ , ‘,’,’?’, etc. Other characters or symbols, e.g.

– Arithmetic symbols, e.g. ‘+’, ‘-’, ‘/’, etc. – from many natural languages

Pengantar Bahasa C - TIF UB 2010 9

Keywords

Keywords cannot be used as identifiers (reserved) because they have special meaning within the language.

Pengantar Bahasa C - TIF UB 2010 10

abstract continue for new switchassert default goto package synchronizedboolean do if private thisbreak double implements protected throwbyte else import public throwscase enum instanceof return transientcatch extends int short trychar final interface static voidclass finally long strictfp volatileconst float native super while

Identifiers Used for names of declared entities such as variables,

constants, and labels Must start with a letter, followed by letters, digits, or

both. Identifiers are case-sensitive The terms letter and digit are broad in Unicode: if

something is considered a letter or digit in a human language, you can probably use it in identifiers.

"Letters" can come from Chinese, Korean, Gurmukhi, Georgian, Devanagari, and almost any other script written in the world today.

Pengantar Bahasa C - TIF UB 2010 11

Identifiers Letters also include any currency symbol (such as $, ¥,

and £) and connecting punctuation (such as _). Identifiers can be as long as you like, but use some taste. Identifiers that are too long are hard to use correctly and

actually obscure your code. Cannot use keywords (e.g. for, if, while, etc) Valid identifiers, e.g.:

– name, x1, _total, cubic

Invalid identifiers, e.g.: – 1kali, int

Pengantar Bahasa C - TIF UB 2010 12

(Data) Types

Every expression has a type that determines what values the expression can produce.

The type of an expression is determined by the types of values and variables used within that expression.

Types are divided into the primitive types and the reference types.

13

Primitive Data Types

14

Unicode character

16 bits or 0 to 216-1 or

unsigned 2 bytes '\u0000' (0) to '\uFFFF' (65535)

8 bit or -27 to 27-1 or1 byte -128 to 12716 bit or -215 to 215-1 or2 bytes -32768 to 3276732 bit or -231 to 231-1 or4 bytes -2147483648 to 2147483647

64 bit or -263 to 263-1 or8 bytes -9223372036854775808 to

9223372036854775807IEEE 754 floating point

32 bit or 1.4E-45 to 3.4028235E+38

single-precision

4 bytes

IEEE 754 floating point

64 bit or 439E-324 to

double-precision

8 bytes 1.7976931348623157E+308

short Signed integer

0

char '\u0000'

byte Signed integer

0

float 0.0f

double 0

int Signed integer

0

long Signed integer

0

Literals Also known as “values” or “constants” Each (data) type has literals, which are the way that

constant values of that type are written. – Boolean literals– Character literals– Integer literals– Floating-point literals– String literals– Reference literals– Class literals

15

Literals Boolean literals

– Only true and false Character literals

– Appear with single quotes, e.g. ‘Z’, ‘a’, ‘2’, ‘\u004e’

– Certain special characters can be represented by an escape sequence, e.g.:

16

\n newline (\u000A)\t tab (\u0009)\b backspace (\u0008)\r return (\u000D)\f form feed (\u000C)\\ backslash itself (\u005C)\' single quote (\u0027)\" double quote (\u0022)

Literals Integer literals

– Integer constants are a sequence of octal, decimal, or hexadecimal digits.

– The start of a constant declares the number's base: A 0 (zero) starts an octal number (base 8); a 0x or 0X starts a hexadecimal number (base 16); and any other digit starts a decimal number (base 10). – E.g. all the following numbers have the same value

29 035 0x1D 0X1d

17

Literals Floating-point literals

– Expressed in either decimal or hexadecimal – The decimal form consists of a string of decimal digits

with an optional decimal point, optionally followed by an exponent the letter e or E, followed by an optionally signed integer.

– e.g. all these literals denote the same floating-point number:

18. 1.8e1 .18E+2 180.0e-1

18

Literals Floating-point literals (continued)

– The hexadecimal form consists of 0x (or 0X), a string of hexadecimal digits with an optional hexadecimal point, followed by a mandatory binary exponent the letter p or P, followed by an optionally signed integer.

– The binary exponent represents scaling by two raised to a power.

– e.g. all these literals denote the same floating-point number (decimal 18.0):

0x12p0 0x1.2p4 0x.12P+8 0x120p-4

19

Literals Floating-point literals (continued)

– Floating-point constants are of type double unless they are specified with a trailing f or F, which makes them float constants, such as 18.0f.

– A trailing d or D specifies a double constant. – There are two zeros: positive (0.0) and negative (-

0.0). – Positive and negative zero are considered equal

when you use == but produce different results when used in some calculations.

20

Literals String literals (continued)

– String literals appear with double quotes, e.g. “Welcome”, “salam”, "\u0633\u0644\u0672\u0645".

– Any character can be included in string literals, with the exception of newline and " (double quote).

– Newlines are not allowed in the middle of strings. – If you want to embed a newline character in the

string, use the escape sequence \n. – To embed a double quote use the escape

sequence \". – A string literal references an object of type String.

21

Variables A variable is a storage location, something that can hold

a value to which a value can be assigned. A variable declaration states the identifier (name), type,

and other attributes of a variable. e.g.

float x, y; // is the same as

float x; float y;

float x = 3.14f, y = 2.81f; // is the same as

float x = 3.14f, y = 2.81f; // is the same as

float x = 3.14f; float y = 2.81f; 22

Variables

Instance variables (non-static fields) Class variables (static fields) Local variables Parameters

23

Variablespublic class Bicycle { int cadence = 0; // instance variable

static int wheels = 2; // static variable

…// formal parameter: decrementvoid applyBrakes(int decrement) {

speed = speed - decrement; } // local variable: states

void printStates() { String states = "cadence: "+cadence+

",speed: "+speed+", gear: "+gear; System.out.println(states); }} 24