欢迎访问宙启技术站
智能推送

Java内置函数库:常用函数介绍和使用方法

发布时间:2023-06-17 12:25:04

Java是一种强大的编程语言,其内置函数库(Java API)是Java程序开发中不可或缺的一部分。Java API包含了众多的类、接口和方法,能够帮助开发者快速完成各种任务,例如字符串处理、I/O操作、网络编程、图形界面开发等等。本文将介绍Java内置函数库中常用的函数及其使用方法。

一、字符串处理函数

1. length(): 返回字符串的长度。

例如:

String str = "Hello World";

int length = str.length();  // 11

2. charAt(int index): 返回字符串中指定位置的字符。

例如:

String str = "Hello World";

char c = str.charAt(1);  // 'e'

3. equals(Object obj): 比较字符串是否相同,忽略大小写。

例如:

String str1 = "Hello World";

String str2 = "hello world";

if (str1.equalsIgnoreCase(str2)) {

   System.out.println("The strings are equal.");

}

4. indexOf(String str): 返回字符串中第一次出现子串的位置,如果没有找到则返回-1。

例如:

String str = "Hello World";

int index = str.indexOf("World");  // 6

5. substring(int beginIndex, int endIndex): 返回字符串中从beginIndex到endIndex-1的子串。

例如:

String str = "Hello World";

String subStr = str.substring(6, 11);  // "World"

二、数学函数

1. Math.sqrt(double a): 返回一个数字的平方根。

例如:

double a = 16;

double result = Math.sqrt(a);  // 4.0

2. Math.ceil(double a): 返回一个大于等于参数a的最小整数。

例如:

double a = 3.5;

double result = Math.ceil(a);  // 4.0

3. Math.floor(double a): 返回一个小于等于参数a的最大整数。

例如:

double a = 3.5;

double result = Math.floor(a);  // 3.0

4. Math.pow(double a, double b): 返回a的b次幂。

例如:

double a = 2;

double b = 3;

double result = Math.pow(a, b);  // 8.0

三、日期时间函数

1. System.currentTimeMillis(): 返回当前时间与1970年1月1日0点0分0秒之间的差值(毫秒数)。

例如:

long currentTime = System.currentTimeMillis();

2. Date(): 构造一个表示当前时间的Date对象。

例如:

Date date = new Date();

3. SimpleDateFormat(String pattern): 返回一个以指定格式来格式化日期和时间的SimpleDateFormat对象。

例如:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

4. Calendar.getInstance(): 返回一个表示当前时间的Calendar对象。

例如:

Calendar calendar = Calendar.getInstance();

四、I/O函数

1. FileInputStream(String fileName): 创建一个文件输入流,用于将数据读取到程序中。

例如:

FileInputStream fis = new FileInputStream("data.txt");

2. FileOutputStream(String fileName): 创建一个文件输出流,用于将数据写入到文件中。

例如:

FileOutputStream fos = new FileOutputStream("output.txt");

3. BufferedReader(InputStreamReader in): 构造一个带缓冲区的字符输入流。

例如:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

4. BufferedWriter(OutputStreamWriter out): 构造一个带缓冲区的字符输出流。

例如:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

五、网络编程函数

1. URL(String url): 构造一个URL对象,表示一个网络地址。

例如:

URL url = new URL("http://www.example.com");

2. HttpURLConnection.connect(): 与URL指定的资源建立连接。

例如:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.connect();

3. BufferedReader.readLine(): 读取一行数据。

例如:

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;

while ((line = reader.readLine()) != null) {

   System.out.println(line);

}

六、图形界面函数

1. JFrame(String title): 创建一个窗口并设置标题。

例如:

JFrame frame = new JFrame("My Window");

2. JButton(String text): 创建一个按钮并设置按钮上的文本。

例如:

JButton button = new JButton("Click Me!");

3. JLabel(String text): 创建一个标签并设置标签上的文本。

例如:

JLabel label = new JLabel("Hello World");

4. JTextField(int columns): 创建一个文本框并设置它的列数。

例如:

JTextField textField = new JTextField(20);

以上是Java内置函数库中常用的函数介绍及使用方法。使用这些函数可以大大减少开发工作量,提高程序开发的效率。