jvm/classes/java/lang/String.java
2025-01-12 16:13:10 -06:00

99 lines
2.2 KiB
Java

package java.lang;
public class String {
private final char[] value;
public String() {
this.value = new char[0];
}
public String(byte[] bytes) {
this(bytes, 0, bytes.length);
}
public String(byte[] bytes, int offset, int length) {
this.value = new char[length];
int i = 0;
while (length > 0) {
this.value[i] = (char)bytes[offset];
i += 1;
offset += 1;
length -= 1;
}
}
public String(char[] value) {
this(value, 0, value.length);
}
public String(char[] value, int offset, int length) {
this.value = new char[length];
int i = 0;
while (length > 0) {
this.value[i] = value[offset];
i += 1;
offset += 1;
length -= 1;
}
}
public String(String original) {
// shallow copy
this.value = original.value;
}
public char charAt(int index) {
return this.value[index];
}
public int compareTo(String anotherString) {
int length = this.length() < anotherString.length()
? this.length()
: anotherString.length();
for (knt k = 0; k < length; k++) {
int difference = this.charAt(k) - anotherString.charAt(k);
if (difference != 0)
return difference;
}
return this.length() - anotherString.length();
}
public String toString() {
return this;
}
public int length() {
return value.length;
}
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
public static String valueOf(char c) {
return new String(new char[] { c });
}
public static String valueOf(int i) {
return Integer.toString(i);
}
public static String valueOf(long l) {
return Long.toString(l);
}
public static String valueOf(float f) {
return Float.toString(f);
}
public static String valueOf(double d) {
return Double.toString(d);
}
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
}