slug
type
status
category
date
summary
tags
password
icon
 
  1. 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"
  1. int length():
      • The length() method returns the number of characters in a String.
      • Example:
        • String message = "Hello, Java"; int len = message.length(); // len will be 10
  1. String substring(int from, int to):
      • The substring(from, to) method extracts a portion of the original String from index from (inclusive) to index to (exclusive).
      • Example:
        • String original = "Programming"; String sub = original.substring(3, 7); // sub will be "gram"
  1. String substring(int from):
      • The substring(from) method extracts a portion of the original String from index from to the end.
      • Example:
        • String original = "Copilot"; String sub = original.substring(2); // sub will be "pilot"
  1. int indexOf(String str):
      • The indexOf(str) method returns the index of the first occurrence of str in the String, or -1 if not found.
      • Example:
        • String sentence = "This is a test sentence."; int index = sentence.indexOf("test"); // index will be 10
  1. boolean equals(String other):
      • The equals(other) method checks if the current String is equal to the other String.
      • Example:
        • String name1 = "Alice"; String name2 = "Alice"; boolean isEqual = name1.equals(name2); // isEqual will be true
           
  1. int compareTo(String other):
      • The compareTo(other) method compares two String lexicographically.
      • Example:
        • String word1 = "apple"; String word2 = "banana"; int result = word1.compareTo(word2); // result will be negative
  1. Integer(int value):
      • The Integer class wraps an int value into an object.
      • Example:
        • Integer num = new Integer(42);
  1. Integer.MIN_VALUE:
      • Represents the minimum value that an int can hold (-2147483648).
  1. Integer.MAX_VALUE:
      • Represents the maximum value that an int can hold (2147483647).
  1. int intValue():
      • Converts an Integer object to its corresponding int value.
      • Example:
        • Integer num = new Integer(123); int value = num.intValue(); // value will be 123
  1. Double(double value):
      • The Double class wraps a double value into an object.
      • Example:
        • Double pi = new Double(3.14159);
  1. double doubleValue():
      • Converts a Double object to its corresponding double value.
      • Example:
        • Double height = new Double(175.5); double heightValue = height.doubleValue(); // heightValue will be 175.5
  1. static int abs(int x):
      • Returns the absolute value of an integer x.
      • Example:
        • int result = Math.abs(-5); // result will be 5
  1. 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
  1. static double pow(double base, double exponent):
      • Computes base raised to the power of exponent.
      • Example:
        • double result = Math.pow(2, 3); // result will be 8.0
  1. static double sqrt(double x):
      • Computes the square root of a double x.
      • Example:
        • double result = Math.sqrt(25); // result will be 5.0
  1. static double random():
      • The random() method generates a random double value between 0.0 (inclusive) and 1.0 (exclusive). It is part of the Math class in Java.
      • Example:
        • double randomValue = Math.random(); // Generates a random value between 0.0 and 1.0
  1. 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
  1. boolean add(E obj):
      • The add(obj) method adds an element obj to a collection (e.g., a list or set). It returns true if the addition is successful.
      • Example (using a list):
        • List<Integer> numbers = new ArrayList<>(); boolean added = numbers.add(42); // added will be true
  1. void add(int index, E obj):
      • The add(index, obj) method inserts an element obj at the specified index in a list.
      • Example (using a list):
        • List<String> fruits = new ArrayList<>(); fruits.add("apple"); fruits.add(1, "banana"); // Inserts "banana" at index 1
  1. E get(int index):
      • The get(index) method retrieves the element at the specified index from a list.
      • Example (using a list):
        • List<String> colors = Arrays.asList("red", "green", "blue"); String thirdColor = colors.get(2); // thirdColor will be "blue"
  1. E set(int index, E obj):
      • The set(index, obj) method replaces the element at the specified index with the new element obj 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
  1. E remove(int index):
      • The remove(index) method removes the element at the specified index 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
  1. boolean equals(Object other):
      • The equals(other) method checks if the current object is equal to the other object. It is commonly used for comparing strings.
      • Example:
        • String name1 = "Alice"; String name2 = "Alice"; boolean isEqual = name1.equals(name2); // isEqual will be true
  1. String toString():
    1. 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:

  1. What does the String constructor new 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.
  1. What is the return value of message.length() if message = "Hello, Java"?
      • A) 9
      • B) 10
      • C) 11
      • D) 12
  1. What will original.substring(3, 7) return for original = "Programming"?
      • A) "gram"
      • B) "gramm"
      • C) "gramming"
      • D) "gramm"
  1. What is the result of original.substring(2) when original = "Copilot"?
      • A) "pi"
      • B) "pilot"
      • C) "Co"
      • D) "ilot"
  1. What does sentence.indexOf("test") return for sentence = "This is a test sentence."?
      • A) 8
      • B) 9
      • C) 10
      • D) -1
  1. What will name1.equals(name2) return when name1 = "Alice" and name2 = "Alice"?
      • A) true
      • B) false
      • C) null
      • D) runtime error
  1. What is the result of word1.compareTo(word2) when word1 = "apple" and word2 = "banana"?
      • A) Positive value
      • B) Negative value
      • C) 0
      • D) Compilation error
  1. What is the purpose of the Integer(int value) constructor?
      • A) It converts an int to a String.
      • B) It creates an Integer object from a String.
      • C) It creates an Integer object from an int value.
      • D) It returns the sum of two integers.
  1. 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.
  1. What is the output of num.intValue() for Integer num = new Integer(123)?
      • A) 123
      • B) "123"
      • C) null
      • D) 0
  1. What is the return value of height.doubleValue() for Double height = new Double(175.5)?
      • A) 175.5
      • B) 0
      • C) "175.5"
      • D) 175
  1. What does Math.abs(-3.14) return?
      • A) 3.14
      • B) -3.14
      • C) 0
      • D) null
  1. What will Math.pow(2, 3) return?
      • A) 6
      • B) 8
      • C) 9
      • D) 16
  1. What is the output of Math.sqrt(25)?
      • A) 5.0
      • B) 5
      • C) 25
      • D) 0
  1. 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.
  1. What does names.size() return when names = new ArrayList<>() with two elements added?
      • A) 0
      • B) 1
      • C) 2
      • D) 3
  1. What is the result of numbers.add(42) if numbers = new ArrayList<>()?
      • A) false
      • B) true
      • C) 42
      • D) null
  1. What does fruits.add(1, "banana") do for fruits = new ArrayList<>() where fruits.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.
  1. What will colors.get(2) return for colors = Arrays.asList("red", "green", "blue")?
      • A) "red"
      • B) "green"
      • C) "blue"
      • D) null
  1. What does animals.remove(1) do for animals = new ArrayList<>() with elements "cat", "cow", "dog"?
      • A) Removes "cat".
      • B) Removes "cow".
      • C) Removes "dog".
      • D) Removes all elements.

Answers:

Answers:

  1. B) It creates a new String object with the same characters as the given String s1.
  1. B) 10
  1. A) "gram"
  1. B) "pilot"
  1. C) 10
  1. A) true
  1. B) Negative value
  1. C) It creates an Integer object from an int value.
  1. B) The smallest possible value of an int.
  1. A) 123
  1. A) 175.5
  1. A) 3.14
  1. B) 8
  1. A) 5.0
  1. B) A random value between 0.0 and 1.0.
  1. C) 2
  1. B) true
  1. B) Adds "banana" at index 1.
  1. C) "blue"
  1. C) Removes "dog".
 

 
notion image
At the point marked /* End of method */, the values of s and n are:
  • s = "world"
  • n = 6

Explanation:

In Java, method parameters are passed by value:
  • For primitive types (like int), the actual value is copied into the method. Changes to the parameter inside the method do not affect the original variable.
  • For object references (like String, which is immutable), the reference is copied. Since String is immutable, any modification (like x = x + "peace") results in a new String object being created, not a change to the original object.
So, in this case:
  • x = x + "peace"; creates a new string "worldpeace" and assigns it to the local variable x. The original s remains "world".
  • y = y * 2; updates the local copy of y to 12, but n outside remains 6.

Final Answer:

  • s = "world"
  • n = 6
Would you like a follow-up question testing similar concepts?

 
notion image
 
Answer: (E) e2.doNothing(e2, e2);

Why the others don’t compile

Call
First-parameter type
Needs Example1 (or subclass)?
Second-parameter type
Needs Example2
Result
(A) e0.doNothing(e0, e0)
Example0
❌ (Example0 is not a subclass of Example1)
Example0
Fails
(B) e1.doNothing(el, el)
Example1
Example1
❌ (Example1 is not Example2)
Fails
(C) el.doNothing(e2, el)
Example2
✅ (subclass of Example1)
Example1
Fails
(D) e2.doNothing(e0, e0)
Example0
Example0
Fails
(E) e2.doNothing(e2, e2)
Example2
Example2
Compiles

Key points to remember

  1. Parameter types are checked at compile time.
    1. doNothing requires (Example1, Example2).
  1. Subtyping is allowed in method arguments.
    1. Any subclass of Example1 can serve as the first argument; any subclass of Example2 can serve as the second.
  1. Reference type, not variable name, matters.
    1. Even though e2 is declared as Example2, it can be passed where Example1 is expected because Example2 extends Example1.
Therefore, only choice (E) satisfies both parameter-type requirements simultaneously.
 
答案:选 E e2.doNothing(e2, e2);

解释(中文复述)

doNothing 方法的参数类型是 (Example1, Example2),编译器在编译期会检查传入实参的“引用类型”是否满足要求:
选项
作为第 1 个实参的引用类型
是否满足需要 Example1 或其子类?
第 2 个实参的引用类型
是否满足需要 Example2 或其子类?
结果
(A) e0.doNothing(e0, e0)
Example0
✖(不是 Example1 的子类)
Example0
不通过
(B) e1.doNothing(el, el)
Example1
Example1
✖(不是 Example2
不通过
(C) el.doNothing(e2, el)
Example2
✔(是 Example1 的子类)
Example1
不通过
(D) e2.doNothing(e0, e0)
Example0
Example0
不通过
(E) e2.doNothing(e2, e2)
Example2
Example2
通过
  • 第一个实参:只要是 Example1 或其子类即可;Example2 满足条件。
  • 第二个实参:必须是 Example2 或其子类;Example2 本身当然满足。
因此,只有 (E) 同时满足两个参数的类型要求,编译能够通过。
 

 
notion image
 
答案:D. getNumOfWatts 方法在 Bike 类中找不到。
原因解析
  • 在编译阶段,Java 只检查引用变量的声明类型是否拥有被调用的方法。
  • 声明 Bike b = new EBike(250); 后,变量 b 的静态类型是 Bike,即使它在运行时引用的是 EBike 对象。
  • Bike 类中没有 getNumOfWatts() 方法,因此编译器在看到
    • 时会报错:找不到该方法。
  • 另一方面,b.getNumOfWheels() 可以正常编译,因为 getNumOfWheels() 定义在 Bike 中。
其余选项逐一排除:
选项
说明
排除理由
A
Bike 没有构造器”
Java 会自动提供无参构造器;与错误无关。
B
EBike 构造器参数过多”
EBike 有一个接受 int 的构造器,调用正确。
C
“子类构造器第一行必须调用超类构造器”
若未显式写 super(),编译器默认插入;构造器本身合法。
E
EBike 中找不到 getNumOfWheels()
该方法继承自 Bike,调用没问题。
因此唯一导致编译失败的原因是 D。
Euclid Resources2025 CSA Quick Review
Loading...
现代数学启蒙
现代数学启蒙
推广现代数学🍚
最新发布
Key Concept Questions: AP Statistics
2025-6-9
Practice MAT 05
2025-6-5
Practice MAT 03
2025-6-5
Practice MAT 02
2025-6-5
Practice MAT 01
2025-6-5
Practice MAT 00
2025-6-5
公告
🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
-- 感谢您的支持 ---