隨著Java語言的不斷發展,越來越多的程序員選擇在MacOS環境中進行開發。這裡我們就要介紹一下如何在MacOS下使用Java8進行開發,並通過代碼實例來展示其各種功能。
一、環境搭建
1、MacOS系統環境
$ sw_vers ProductName: Mac OS X ProductVersion: 10.15.4 BuildVersion: 19E266
2、JDK環境
$ java -version java version "1.8.0_281" Java(TM) SE Runtime Environment (build 1.8.0_281-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
3、Maven環境
$ mvn -v Apache Maven 3.6.3 Maven home: /usr/local/Cellar/maven/3.6.3/libexec Java version: 1.8.0_281, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "mac"
二、基本語法
1、Hello World實例代碼
public class HelloWorld { public static void main(String[] args) { // Prints "Hello, World" to the terminal window. System.out.println("Hello, World"); } }
2、變數和數據類型
public class VarTest { public static void main(String[] args) { // 聲明變數 int a = 1; float b = 1.2f; double c = 3.1415926; boolean d = true; char e = 'a'; String f = "Hello, World"; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(f); } }
3、循環和分支
public class LoopTest { public static void main(String[] args) { // if語句 int a = 10; if (a < 20) { System.out.println("小於20"); } // for循環 for (int i = 0; i < 10; i++) { System.out.println(i); } // while循環 int j = 0; while (j < 10) { System.out.println(j); j++; } } }
三、常用類和方法
1、String類
public class StringTest { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String str3 = str1 + ", " + str2 + "!"; System.out.println(str3); System.out.println(str3.toUpperCase()); System.out.println(str3.toLowerCase()); System.out.println(str3.substring(1,5)); System.out.println(str3.indexOf("World")); } }
2、List和Map集合
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ListMapTest { public static void main(String[] args) { // List集合 List list = new ArrayList(); list.add("apple"); list.add("banana"); list.add("orange"); for (String fruit : list) { System.out.println(fruit); } // Map集合 Map map = new HashMap(); map.put("name", "張三"); map.put("age", 20); map.put("address", "北京市海淀區"); for (String key : map.keySet()) { System.out.println(key + " -> " + map.get(key)); } } }
四、Web開發
1、Spring Boot實現RESTful API介面
@RestController public class HelloController { @RequestMapping("/") public String index() { return "Hello, World!"; } @RequestMapping("/hello") public String hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name) { return "Hello, " + name + "!"; } @RequestMapping("/user/{id}") public String user(@PathVariable int id) { return "User ID: " + id; } }
2、Thymeleaf模板引擎實現頁面渲染
@Controller public class UserController { @GetMapping("/users") public String userList(Model model) { List users = new ArrayList(); users.add(new User(1, "張三", 20)); users.add(new User(2, "李四", 25)); users.add(new User(3, "王五", 30)); model.addAttribute("users", users); return "users"; } }
五、總結
本文介紹了如何在MacOS環境中使用Java8進行開發,包括環境搭建、基本語法、常用類和方法以及Web開發。希望讀者能夠通過本文的介紹和實例,更好地了解Java8在MacOS上的應用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/236381.html