Post

Java) lang/ Object/ StringBuffer/ equals

Java) lang/ Object/ StringBuffer/ equals

This post was migrated from Tistory. You can find the original here.

java.lang package

Object

The Object class is the ultimate ancestor of every class.
By default, Object.equals() compares instances by their address value.
String.equals(), on the other hand, is overridden by the String class, so instead of comparing addresses it compares the object’s member values.
The comparison operator (==) always compares address values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Main {
    public static void main(String[] args) {
        String a = new String("hello");
        String b = new String("hello");
        System.out.println(a.equals(b));
        System.out.println(a == b);

        Value a2 = new Value(10);
        Value b2 = new Value(10);
        System.out.println(a2.equals(b2));
    }
}

class Value{
    int value;

    Value(int value){
        this.value = value;
    }
}

Result
true
false
false

String

String is an immutable class.
String + String creates a brand new String instance, so if you need to do a lot of string manipulation, use the StringBuffer class instead.

1
2
3
4
5
6
7
8
9
10
11
public class Main {
    public static void main(String[] args) {
        String a = new String("h");
        String b = new String("h");
        System.out.println(a == b);

        String a2 = "h";
        String b2 = "h";
        System.out.println(a2 == b2);
    }
}

new String creates a brand new instance, while a literal reuses one that already exists.

1
2
3
4
5
6
7
8
9
public class Main {
    public static void main(String[] args) {
        long a = 100L;
        String b = "100L";

        String a2 = String.valueOf(a);
        long b2 = Long.parseLong(b);
    }
}

String.valueOf(): primitive type -> String
{Wrapper}.parse{Wrapper}(): String -> primitive type

StringBuffer

If you don’t specify a length, bufferSize = 16.
If you create it from a string, bufferSize = string.length() + 16.

Since equals() isn’t overridden, comparing with it gives you the same result as using the comparison operator (==).

Its methods include append(), capacity(), charAt(), delete(), insert(), replace(), toString(), substring(), and more.
It covers pretty much everything String can do.

StringBuffer is synchronized to be thread-safe.
Since synchronization hurts StringBuffer’s performance, there’s a StringBuilder class that’s identical except without the synchronization.
If you’re not using multiple threads, StringBuilder performs better, but StringBuffer is plenty fast too, so there’s no need to worry too much about it.

Math

round(), ceil(), and floor() all round at the first decimal place.
To round at a different digit, multiply by 10^n up to the digit you want, round, and then divide back by the same amount.

1
2
3
4
5
6
7
8
9
10
11
12
public class Main {
    public static void main(String[] args) {
        float a = 123.51262f;
        a *= Math.pow(10, 2);
        a = Math.round(a);
        a /= Math.pow(10, 2);
        System.out.println(a);
    }
}

Result
123.51

Pay close attention to int, long, float, and double.
ceil() and floor() return a double, while round() returns a long.

Wrapper

Every wrapper class overrides equals(), so it compares values rather than addresses. (In recent versions of Java, the comparison operator (==) also compares values.)
toString() is overridden as well, converting the value the object holds into a string and returning it.

{Wrapper}.parse{Wrapper}(“string”): String -> primitive type
{Wrapper}.valueOf(“string”): String -> wrapper class

This post is licensed under CC BY-NC 4.0 by the author.