随着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/n/236381.html