slug
type
status
category
summary
date
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:
IGCSE 0580 浓缩笔记(考前必看)Learning IGCSE Mathematics with Pictures (1/n)
Loading...