วิชาภาษาจาวา (java)

การนิยามตัวแปร ภาษาจาวา

จากบทเรียนที่ผ่านมาเราได้เรียนรู้ในเรื่องของ ตัวแปร มาแล้ว บทเรียนนี้จะแสดงให้เห็นถึงการนิยามตัวแปร ซึ่งมีหลักการเขียนตัวแปร การตั้งชื่อตัวแปรนั้นควรตั้งชื่อให้มันสื่อความหมาย เพื่อให้ง่ายต่อความเข้าใจ และอย่าตั้งชื่อตัวแปรไปซ้ำกับคำสงวน

การนิยามตัวแปร

ชนิดข้อมูล ชื่อตัวแปร ;
ชนิดข้อมูล ชื่อตัวแปร = ค่าเริ่มต้น ;
แบบหลายตัวแปรในบรรทัดเดียว
ชนิดข้อมูล ชื่อตัวแปร = ค่าเริ่มต้น,ชื่อตัวแปร = ค่าเริ่มต้น ;


int คือตัวเลขจำนวนเต็ม

float คือเลขที่มีทศนิยน

char คือ ตัวอักษร

boolean คือ ค่าความจริง หรือเท็จ


ตัวอย่าง

class Ktbs 
{
	public static void main(String[] args) 
	{
		int a = 10 ;
		float b = 20.3f ;
		char c = 'a';
		boolean d = true;
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
	}
}

ผลที่ได้จากการ RUN

10
20.3
a
true

 

 

slot gacor hari ini slot qris slot gacor maxwin slot online slot gacor gampang menang slot gacor hari ini slot online slot slot slot guci777 guci777 fafatogel fafatogel fafatogel fafatogel agen138 slot agen138 slot data hk bola slot bolaslot99 bola slot agen138 link alternatif koin138 link alternatif agen138 slot ag138 slot slot gacor slot thailand