• Tidak ada hasil yang ditemukan

Java Fundamentals

N/A
N/A
Protected

Academic year: 2025

Membagikan "Java Fundamentals"

Copied!
13
0
0

Teks penuh

(1)

Java Fundamentals

CPIT 202

(2)

Week 2 Lecture 2

Java Primitive Data Types

(3)

Java’s Eight Primitive Data Types

DATA TYPE SIZE MIN VALUE MAX VALUE

byte 8 bits ‐128 127

short 16 bits ‐32768 32767

int 32 bits –2147483648 2147483647

long 64 bits –9223372036854775808 9223372036854775807 long 64 bits 9223372036854775808 9223372036854775807 float 32 bits ±1.40239846E‐45 ±3.40282347E+8

double 64 bits ±4.94065645841246544E 324

±1.79769313486231570E +308

‐324 +308

char 16 bits \u0000 \uFFFF

boolean n/a true or false

3 Java Programming

(4)

Variables

Variables are used to store data. In Java, a 

i bl d t b d l d

variable needs to be declared.

Declaring a variable involves two steps:

ƒ Giving the variable a name

ƒ Stating what type of data is to be stored in the  variable

short x;

short x;

int age;

float salary;

(5)

Declaring Variables

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

// integer variable;

d bl di // l di

double radius;       // Declare radius to

// be a double variable;

char a;       // Declare a to be a // character variable;

// character variable;

(6)

Assigning Variables

ƒ Use the assignment operator   = to assign a 

i bl t ti l l

variable to a particular value.

int x; // declare a variable 12 // i 12 t th

x = 12; // assign 12 to the var Or we can do the declaration and initialisation at 

f

the same time, like the following  int x = 12;;

(7)

Integral Types

ƒ From the eight primitive data types, 4 of them  diff l b th i i

are differ only by their size : 

byte, short, int, long int x = 250;

Example :

short a, b;

a = 21;

a 21;

b = 9;

Java Programming

long y = 12345678987654321L;

7

(8)

Floating‐Point Types

ƒ Two of the eight primitive data types are used 

f t i b

for storing numbers: 

float, double Example :

double pi = 3.14159;

float f = 2.7F;

(9)

Boolean Data Type

ƒ Only one from the eight primitive data types is 

d t t B l l

used to represent Boolean values: 

boolean Example :

char a = ‘A’;

char half = ‘\u00AB’;

char half \u00AB ;

Java Programming 9

(10)

Char Data Type

ƒ The char data type represents characters in  Java

Java.

ƒ Single quote ‘ ’ are used to denote a character  literal

ESCAPE  SEQUENCE

DEFINITION

\t tab

char a = ‘A’;

literal.

\t tab

\b backspace

\n newline

char half = ‘\u00AB’;

\r carriage return

\’ single quote

\” double quote

(11)

Strings

ƒ A string is a sequence of characters.

ƒ Treated as a single item.

ƒ Enclosed in double quote “ “.

“The quick brown fox jumps over the lazy cat”

string greeting = “Good Morning!!\n”;

Example :

string greeting = Good Morning!!\n ;

string errorMessage = “Record Not Found!”;

Java Programming 11

(12)

Constants

ƒ The final keyword is used, makes a constant.

ƒ Cannot be changed after it is assigned a value.

final Example :

final double pi = 3.14159;

final int x;

(13)

End of lecture 2

Referensi

Dokumen terkait