วันอาทิตย์, มิถุนายน 1, 2025
Latest:
วิชาภาษาจาวา (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 wortel21 guci777 guci777 wortel21 wortel21 wortel21