![](https://www.glurgeek.com/wp-content/uploads/2022/01/Java-Cover-1024x576.jpg)
เตรียมเครื่องมือก่อนการเขียนโปรแกรมภาษา Java
1. ติดตั้งโปรแกรม Visual Studio Code
ดาวน์โหลดและติดตั้งโปรแกรม Visual Studio Code (VSCode) ทาง https://code.visualstudio.com/ รองรับทั้ง macOS, Windows และ Linux
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-21.24.25-1024x548.png)
2. ติดตั้ง Environment สำหรับเขียนโปรแกรมภาษา Java ด้วย Visual Studio Code
2.1 Getting Started with Java in VS Code ทาง https://code.visualstudio.com/docs/java/java-tutorial
2.2 Install the Coding Pack for Java – Windows หรือ macOS
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-21.31.20-1024x532.png)
2.3 Install the Extension Pack for Java
เปิดโปรแกรม VS Code –> Extensions พิมพ์คำว่า extension pack for java
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-21.39.36-1-1024x575.png)
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-21.33.45-1024x552.png)
3. ติดตั้ง Extensions ต่าง ๆ ที่ช่วยให้การเขียนโปรแกรมบน VS Code สะดวกและง่ายขึ้น
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-21.35.34.png)
4. เริ่มต้นเขียนโปรแกรมภาษา Java ด้วย VS Code
สร้าง Folder สำหรับเก็บไฟล์เขียนโปรแกรม แล้วลากมาใส่ในโปรแกรม VS Code
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-21.47.13-1024x576.png)
ฝึกปฏิบัติการเขียนโปรแกรมภาษา Java
แบบฝึกหัดที่ 1 Java Syntax
สร้างไฟล์ .java
New File –> ตั้งชื่อ Ex1_JavaSyntax.java –> ทำการเขียนโปรแกรมภาษา Java ตาม Code –> กด Run แสดงผลลัพธ์ออกทาง Terminal
//Aj.NesT the Series /* Java Programming Java Syntax Hello Java Programming */ public class Ex1_JavaSyntax { public static void main(String[] args) { System.out.println("Hello Java Programming"); } }
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-22.15.19-1024x576.png)
ทำการ Config ค่า Setting สำหรับแสดง Output
Code –> Preferences –> Settings –> พิมพ์ java>debug>settings –> ตั้งค่า Console เป็น internalConsole
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-22.18.07-1024x330.png)
ทำการกด Run ผลลัพธ์อีกครั้ง และไปดู Output ที่ Debug Console
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-22.19.01-1024x577.png)
แบบฝึกหัดที่ 2 Java Variables
//Aj.NesT the Series /** * Ex2_JavaVariables */ public class Ex2_JavaVariables { public static void main(String[] args) { String name = "Aj.NesT the Series"; System.out.println(name); int score1 = 89; System.out.println(score1); int score2; score2 = 93; System.out.println(score2); int score3 = 58; score3 = 73; System.out.println(score3); final int score4 = 83; //final variable value //scoreD = 30; //error System.out.println(score4); System.out.println("Mean: "+((score1+score2+score3+score4)/4)); int score = (score1+score2+score3+score4)/4; String firstName = "Aj.NesT "; String lastName = "the Series"; String fullName = firstName + lastName; System.out.println(fullName + "'s mean score: "+ score); } }
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-23.35.18-1024x577.png)
แบบฝึกหัดที่ 3 Java Data Types
//Aj.NesT the Series /** * Ex3_JavaDataTypes */ public class Ex3_JavaDataTypes { public static void main(String[] args) { byte numByte = 127; //-128 to 127 short numShort = 32767; int score = 90; //-2147483648 to 2147483647 long numLong = 9223372036854775807L; //-9223372036854775808 to 9223372036854775807 float gravity = 9.8f, numFloat = 26e3f; //6 to 7 decimal digits double golden_ratio = 1.618033988d, numDouble = 18e4d; //15 decimal digits char grade = 'A', gradeB = 66; //Character or ASCII values boolean answer = true; //true or false String name = "Aj.NesT"; String text = "\"I love Java Programming\""; //Escape character in Java String file = "My work files are in D:\\Work Projects\\java"; System.out.println(score); System.out.println(gravity); System.out.println(golden_ratio); System.out.println("byte: "+numByte+ " short: "+numShort+" long: "+numLong); System.out.println("float: "+numFloat+ " double: "+numDouble); System.out.println(grade+" "+gradeB); System.out.println(answer); System.out.println(name+" say "+text); System.out.println("Files: "+file); } }
![](https://www.glurgeek.com/wp-content/uploads/2022/01/Screen-Shot-2565-01-13-at-23.35.18-1-1024x577.png)