slug
type
status
category
summary
date
tags
password
icon
- String (String str):
- Constructor: Creates a new String object with the same characters as the given String.
- Example:
String s1 = "Hello";
String s2 = new String(s1); // s2 now has the value "Hello"
- int length():
- The
length()
method returns the number of characters in aString
. - Example:
String message = "Hello, Java";
int len = message.length(); // len will be 10
- String substring(int from, int to):
- The
substring(from, to)
method extracts a portion of the originalString
from indexfrom
(inclusive) to indexto
(exclusive). - Example:
String original = "Programming";
String sub = original.substring(3, 7); // sub will be "gram"
- String substring(int from):
- The
substring(from)
method extracts a portion of the originalString
from indexfrom
to the end. - Example:
String original = "Copilot";
String sub = original.substring(2); // sub will be "pilot"
- int indexOf(String str):
- The
indexOf(str)
method returns the index of the first occurrence ofstr
in theString
, or -1 if not found. - Example:
String sentence = "This is a test sentence.";
int index = sentence.indexOf("test"); // index will be 10
- boolean equals(String other):
- The
equals(other)
method checks if the currentString
is equal to theother
String
. - Example:
String name1 = "Alice";
String name2 = "Alice";
boolean isEqual = name1.equals(name2); // isEqual will be true
- int compareTo(String other):
- The
compareTo(other)
method compares twoString
lexicographically. - Example:
String word1 = "apple";
String word2 = "banana";
int result = word1.compareTo(word2); // result will be negative
- Integer(int value):
- The
Integer
class wraps anint
value into an object. - Example:
Integer num = new Integer(42);
- Integer.MIN_VALUE:
- Represents the minimum value that an
int
can hold (-2147483648).
- Integer.MAX_VALUE:
- Represents the maximum value that an
int
can hold (2147483647).
- int intValue():
- Converts an
Integer
object to its correspondingint
value. - Example:
Integer num = new Integer(123);
int value = num.intValue(); // value will be 123
- Double(double value):
- The
Double
class wraps adouble
value into an object. - Example:
Double pi = new Double(3.14159);
- double doubleValue():
- Converts a
Double
object to its correspondingdouble
value. - Example:
Double height = new Double(175.5);
double heightValue = height.doubleValue(); // heightValue will be 175.5
- static int abs(int x):
- Returns the absolute value of an integer
x
. - Example:
int result = Math.abs(-5); // result will be 5
- static double abs(double x):
- Returns the absolute value of a double
x
. - Example:
double result = Math.abs(-3.14); // result will be 3.14
- static double pow(double base, double exponent):
- Computes
base
raised to the power ofexponent
. - Example:
double result = Math.pow(2, 3); // result will be 8.0
- static double sqrt(double x):
- Computes the square root of a double
x
. - Example:
double result = Math.sqrt(25); // result will be 5.0
- static double random():
- The
random()
method generates a random double value between 0.0 (inclusive) and 1.0 (exclusive). It is part of theMath
class in Java. - Example:
double randomValue = Math.random(); // Generates a random value between 0.0 and 1.0
- int size():
- The
size()
method is commonly used in collections (such as lists, sets, and maps) to determine the number of elements in the collection. - Example (using a list):
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
int listSize = names.size(); // listSize will be 2
- boolean add(E obj):
- The
add(obj)
method adds an elementobj
to a collection (e.g., a list or set). It returnstrue
if the addition is successful. - Example (using a list):
List<Integer> numbers = new ArrayList<>();
boolean added = numbers.add(42); // added will be true
- void add(int index, E obj):
- The
add(index, obj)
method inserts an elementobj
at the specifiedindex
in a list. - Example (using a list):
List<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add(1, "banana"); // Inserts "banana" at index 1
- E get(int index):
- The
get(index)
method retrieves the element at the specifiedindex
from a list. - Example (using a list):
List<String> colors = Arrays.asList("red", "green", "blue");
String thirdColor = colors.get(2); // thirdColor will be "blue"
- E set(int index, E obj):
- The
set(index, obj)
method replaces the element at the specifiedindex
with the new elementobj
in a list. - Example (using a list):
List<Integer> scores = new ArrayList<>();
scores.add(80);
scores.set(0, 90); // Replaces the score at index 0 with 90
- E remove(int index):
- The
remove(index)
method removes the element at the specifiedindex
from a list. - Example (using a list):
List<String> animals = new ArrayList<>();
animals.add("cat");
animals.add("dog");
String removedAnimal = animals.remove(1); // Removes "dog" from the list
- boolean equals(Object other):
- The
equals(other)
method checks if the current object is equal to theother
object. It is commonly used for comparing strings. - Example:
String name1 = "Alice";
String name2 = "Alice";
boolean isEqual = name1.equals(name2); // isEqual will be true
- String toString():
The
toString()
method in Java is a member of the Object
class. Since it is inherited by all Java classes, you can override it to provide a string representation of any Java object. This method is very useful when you need a human-readable description of an object, typically for debugging or logging purposes.Here's a breakdown of its uses and how to implement it:
Default Implementation
By default, the
toString()
method returns a string that consists of the class name followed by the "@" symbol and then the hexadecimal representation of the object's hash code. This default implementation is rarely useful in practical applications.Overriding toString()
You can override the
toString()
method in any class to return a more informative, readable string that describes an instance of that class. This is particularly useful for debugging, as you can print out objects directly and get meaningful information.Example
Here's a simple example of overriding the
toString()
method in a Person
class:In this example, the
toString()
method is overridden to return a string that includes the person's name and age. When you print an instance of Person
, the toString()
method is called automatically, resulting in a more informative output.Codes:
Quiz:
Here are 20 AP-style multiple-choice questions based on the content provided. I will list the answers at the end.
Questions:
- What does the
String
constructornew String(s1)
do? - A) It creates a new String object with the value "s1".
- B) It creates a new String object with the same characters as the given String
s1
. - C) It creates a String object by converting the character array
s1
into a String. - D) It appends "s1" to an existing String object.
- What is the return value of
message.length()
ifmessage = "Hello, Java"
? - A) 9
- B) 10
- C) 11
- D) 12
- What will
original.substring(3, 7)
return fororiginal = "Programming"
? - A) "gram"
- B) "gramm"
- C) "gramming"
- D) "gramm"
- What is the result of
original.substring(2)
whenoriginal = "Copilot"
? - A) "pi"
- B) "pilot"
- C) "Co"
- D) "ilot"
- What does
sentence.indexOf("test")
return forsentence = "This is a test sentence."
? - A) 8
- B) 9
- C) 10
- D) -1
- What will
name1.equals(name2)
return whenname1 = "Alice"
andname2 = "Alice"
? - A) true
- B) false
- C) null
- D) runtime error
- What is the result of
word1.compareTo(word2)
whenword1 = "apple"
andword2 = "banana"
? - A) Positive value
- B) Negative value
- C) 0
- D) Compilation error
- What is the purpose of the
Integer(int value)
constructor? - A) It converts an
int
to aString
. - B) It creates an
Integer
object from aString
. - C) It creates an
Integer
object from anint
value. - D) It returns the sum of two integers.
- What does
Integer.MIN_VALUE
represent? - A) The largest possible value of an
int
. - B) The smallest possible value of an
int
. - C) The default value of an
int
. - D) The minimum value of a
long
.
- What is the output of
num.intValue()
forInteger num = new Integer(123)
? - A) 123
- B) "123"
- C) null
- D) 0
- What is the return value of
height.doubleValue()
forDouble height = new Double(175.5)
? - A) 175.5
- B) 0
- C) "175.5"
- D) 175
- What does
Math.abs(-3.14)
return? - A) 3.14
- B) -3.14
- C) 0
- D) null
- What will
Math.pow(2, 3)
return? - A) 6
- B) 8
- C) 9
- D) 16
- What is the output of
Math.sqrt(25)
? - A) 5.0
- B) 5
- C) 25
- D) 0
- What does
Math.random()
return? - A) A random integer between 0 and 1.
- B) A random value between 0.0 and 1.0.
- C) A random float between 0 and 1.
- D) A random integer between 1 and 10.
- What does
names.size()
return whennames = new ArrayList<>()
with two elements added? - A) 0
- B) 1
- C) 2
- D) 3
- What is the result of
numbers.add(42)
ifnumbers = new ArrayList<>()
? - A) false
- B) true
- C) 42
- D) null
- What does
fruits.add(1, "banana")
do forfruits = new ArrayList<>()
wherefruits.add("apple")
has been called? - A) Adds "banana" at the end.
- B) Adds "banana" at index 1.
- C) Removes "apple" and adds "banana".
- D) Results in an IndexOutOfBoundsException.
- What will
colors.get(2)
return forcolors = Arrays.asList("red", "green", "blue")
? - A) "red"
- B) "green"
- C) "blue"
- D) null
- What does
animals.remove(1)
do foranimals = new ArrayList<>()
with elements "cat", "cow", "dog"? - A) Removes "cat".
- B) Removes "cow".
- C) Removes "dog".
- D) Removes all elements.
Answers:
Answers:
- B) It creates a new String object with the same characters as the given String
s1
.
- B) 10
- A) "gram"
- B) "pilot"
- C) 10
- A) true
- B) Negative value
- C) It creates an
Integer
object from anint
value.
- B) The smallest possible value of an
int
.
- A) 123
- A) 175.5
- A) 3.14
- B) 8
- A) 5.0
- B) A random value between 0.0 and 1.0.
- C) 2
- B) true
- B) Adds "banana" at index 1.
- C) "blue"
- C) Removes "dog".
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/javareference
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章