class Person {
private int regiNum; // 주민등록번호
private int passNum; // 여권번호
Person(){
regiNum = 0;
passNum = 0;
}
Person(int regiNum, int passNum){
this.regiNum = regiNum;
this.passNum = passNum;
}
Person(int rnum){
this(rnum, 0);
}
}
String str1 = new String("Simple String");
String str2 = "The Best String";
public class ImmutableString {
public static void main(String[] args) {
String str1 = "Simple String";
String str2 = "Simple String";
String str3 = new String("Simple String");
String str4 = new String("Simple String");
if (str1 == str2) {
System.out.println("같은 인스턴스 참조");
} else {
System.out.println("다른 인스턴스 참조");
}
if (str3 == str4) {
System.out.println("같은 인스턴스 참조");
} else {
System.out.println("다른 인스턴스 참조");
}
}
}
[Console]
같은 인스턴스 참조
다른 인스턴스 참조
private final byte[] value;
public class JavaPractice {
public static void main(String[] args) {
String str = "two";
switch(str) {
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
default :
System.out.println("default");
}
}
}
public class JavaPractice {
public static void main(String[] args) {
String str1 = "Simple String";
String str2 = "Simple String";
String str3 = new String("Simple String");
String str4 = new String("Simple String");
if(str1.equals(str3)) {
System.out.println("같은 문자열");
}else {
System.out.println("다른 문자열");
}
}
}
[Console]
같은 문자열
String 클래스 > equals 함수 (boolean)
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
public class StringConcat {
public static void main(String[] args) {
String str1 = "Coffee";
String str2 = "Bread";
String str3 = str1.concat(str2);
System.out.println(str3);
String str4 = "Fresh".concat(str3);
System.out.println(str4);
}
}
[Console]
CoffeeBread
FreshCoffeeBread
String 클래스 > concat 함수 (String)
public String concat(String str) {
if (str.isEmpty()) {
return this;
}
if (coder() == str.coder()) {
byte[] val = this.value;
byte[] oval = str.value;
int len = val.length + oval.length;
byte[] buf = Arrays.copyOf(val, len);
System.arraycopy(oval, 0, buf, val.length, oval.length);
return new String(buf, coder);
}
int len = length();
int olen = str.length();
byte[] buf = StringUTF16.newBytesFor(len + olen);
getBytes(buf, 0, UTF16);
str.getBytes(buf, len, UTF16);
return new String(buf, UTF16);
}
String 클래스 > length 함수 (int)
public int length() {
return value.length >> coder();
}
String 클래스 > charAt 함수 (char, 매개변수는 int)
public char charAt(int index) {
if (isLatin1()) {
return StringLatin1.charAt(value, index);
} else {
return StringUTF16.charAt(value, index);
}
}
public static void main(String[] args) {
String str = "abcde";
for(int i=0; i<str.length(); i++) {
System.out.print(str.charAt(i));
}
}
[Console]
abcde
public static void main(String[] args) {
String str = "abcde";
for (int i = str.length() - 1; i >= 0; i--) {
System.out.print(str.charAt(i));
}
}
[Console]
edcba