10个常用的Java函数及其使用方法
1. String.length()
The String.length() function returns the length of a given string. This is a very useful function for checking whether a string is empty or not, or for validating that a string is of a certain length.
Example:
String myString = "Hello World!";
int length = myString.length();
System.out.println(length); // Output: 12
2. Arrays.sort()
The Arrays.sort() function is used to sort an array of objects. This function sorts the array in ascending order according to the natural order of the elements.
Example:
int[] numbers = { 5, 1, 8, 3 };
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [1, 3, 5, 8]
3. Math.min()
The Math.min() function takes two numeric parameters as input and returns the smaller of the two values.
Example:
int a = 5;
int b = 3;
int min = Math.min(a, b);
System.out.println(min); // Output: 3
4. String.trim()
The String.trim() function is used to remove any whitespace characters from the beginning and end of a string. This is useful for cleaning input data before processing it.
Example:
String myString = " Hello World! ";
myString = myString.trim();
System.out.println(myString); // Output: "Hello World!"
5. String.replace()
The String.replace() function takes two string parameters as input and replaces all occurrences of the first string in the original string with the second string. This is useful for modifying strings dynamically.
Example:
String myString = "Hello World!";
myString = myString.replace("World", "Java");
System.out.println(myString); // Output: "Hello Java!"
6. String.indexOf()
The String.indexOf() function returns the index of the first occurrence of the specified string or character in the given string. If the input string does not contain the specified value, it returns -1.
Example:
String myString = "Hello World!";
int index = myString.indexOf("World");
System.out.println(index); // Output: 6
7. StringBuilder.append()
The StringBuilder.append() function is used to concatenate a string to the end of an existing string. This function is more efficient than using the + operator to concatenate strings because it does not create a new object every time it is called.
Example:
StringBuilder sb = new StringBuilder();
sb.append("Hello ");
sb.append("World!");
String result = sb.toString();
System.out.println(result); // Output: "Hello World!"
8. ArrayList.add()
The ArrayList.add() function is used to add a new element to the end of an ArrayList. This function is very useful for dynamically adding or removing elements from a list.
Example:
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Orange");
list.add("Banana");
System.out.println(list); // Output: ["Apple", "Orange", "Banana"]
9. Calendar.getInstance()
The Calendar.getInstance() function returns a Calendar object representing the current date and time. This is useful for performing operations that require a timestamp, such as scheduling tasks.
Example:
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime()); // Output: "Sun Nov 28 17:18:56 EST 2021"
10. Scanner.next()
The Scanner.next() function is used to read the next token of input, which is typically a value entered by the user through the console. This is useful for building interactive applications that take user input.
Example:
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your name: ");
String name = scanner.next();
System.out.println("Hello, " + name + "!"); // Output: "Hello, [Name]!"
