Objects and Primitive Data

03:55 ---

Variables


public class PianoKeys {
public static void main (String[] args) {
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
--------------------------------------------------------------------------------------------------------------------------
Compile:
A piano has 88 keys.
--------------------------------------------------------------------------------------------------------------------------
public class PianoKeysErr {
public static void main (String[] args) {
int keys; // declaration without initialization
System.out.println ("A piano has " + keys + " keys.");
}
}
 ก็คือไม่ประกาศint keysว่า=เท่าไหร่ runก็runไม่ได้
--------------------------------------------------------------------------------------------------------------------------
Compile:
C:\myDir\PianoKeysErr.java:6: variable keys might not have
been initialized
System.out.println ("A piano has " + keys + " keys.");
^
1 error
Tool completed with exit code 1
--------------------------------------------------------------------------------------------------------------------------

Assignment

กำหนดค่าตัวแปล ใช้เครื่องหมาย=

total = 5 * 8 - 3; // total is set to 37

ค่าก่อนหน้านี้จะถูกเขียนทับ

total = 10; // total is set to 10
total = 22; // now it is set to 22

สามารถกำหนดเพียงค่าให้กับตัวแปรที่เป็น สอดคล้องกับการประกาศชนิดของตัวแปร
int x = 10; // ok
int y = 10.5; // 10.5 is not an integer

Constants ค่าคงที่

ใช้finalในการประกาศค่าอย่างต่อเนื่อง
final int MIN_HEIGHT = 69;
ชื่อconvention:จะเป็นพิมพ์ใหญ่ทั้งหมด

Primitive Data

ชนิดข้อมูลในjava
แบบจำนวนเต็ม integers:
• byte, short, int, long
ทศนิยม
• float, double
ตัวอักษร
• char
เปรียบเทียบ
• boolean

Numeric Primitive Data

การเก็บข้อมูลตัวเลข
byte, short, int, long, float, double

int เก็บได้มากสุด 2,147,483,647.
floatเก็บได้เพียง7เลข 100,000.875ถ้ามากกว่านั้นใช้ double.

Characters

char เป็น String ได้ถ้ามี
'a'     'X'     '7'     '$'      ','    '\n‘

ความแตกต่างระหว่าง
int x = 1; // x is a number
char y = ‘1’; // y is a character

Boolean

จริงเท็จ
boolean done = false; // ok
boolean x = 1; // won’t compile

Arithmetic Expressions นิพจน์เลขคณิต

+ - * / %
จำนวนกับจำนวนเต็ม=จำนวนเต็ม
ทศนิยมกับทศนิยม=ทศนิยม
จำนวนเต็มกับทศนิยม=ทศนิยม
เช่น

3 * 5 = 15
3 * 5.0 = 15.0

Division and Remainder (Div and Mod)
If both operands to the division operator (/) are integers,
the result is an integer (the fractional part is discarded)
The remainder operator (%) returns the remainder after
dividing the second operand into the first
14 / 3 = 4
8 / 12 =0

14 % 3 =2
8 % 12 =8

Operator Precedence

x = 15 * 4 + 88 % 2 - 11;
result = total + count / max - offset;

• *, /, %
• +, -

What is the order of evaluation in the following expressions?
a + b + c + d + e
1 2 3 4
a + b * c - d / e
3 1 4 2
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1

Casting

int x = 10, y = 4;
float result;
result = (float) x / y; // result is 2.5
result = (float) (x / y); // result is 2.0

Creating Objects

สร้างวัตถุใหม่อ้างอิงวัตถุเก่า
public class Dog {
}
public void bark () {
...
}
...
public void wag () {
...
}

--------------------------------------------------------------------------------------------------------------------------
Dog myDog = new Dog ();

อ้างอิงและวัตถุยังสามารถสร้างแยกต่างหาก:
Dog myDog;
myDog = new Dog ();

Using Objects

public class Dog {
}
public void bark () {
...
}
...
public void wag () {
...
}

The null Reference

อ้างอิงวัตถุ ว่าง
Dog myDog = new Dog ();
myDog = null;
เมื่อตรวจสอบวัตถุไม่มี
if (myDog == null)
System.out.println ("Invalid");

Garbage Collection


0 ความคิดเห็น:

แสดงความคิดเห็น