1주차 화요일

KH·2023년 5월 9일
0

사전캠프

목록 보기
2/5
post-thumbnail

https://nbcamp.gitbook.io/java-handbook/part-02.
Source: https://www.java67.com/2014/08/difference-between-string-literal-and-new-String-object-Java.html#ixzz81C2VSaTS
Source2: https://www.javatpoint.com/java-string-intern

Theme 1

Difference between String literal and New String object in Java

String strObject = new String("Java");

and

String strLiteral = "Java";

Both expressions give you a String object, but there is a subtle difference between them. When you create a String object using the new() operator, it always creates a new object in heap memory.

On the other hand, if you create an object using String literal syntax e.g. "Java", it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it already exists.

What is String literal and String Pool

Since String is one of the most used types in any application, the Java designer took a step further to optimize the uses of this class. They know that Strings will not be going to be cheap, and that's why they come up with an idea to cache all String instances created inside double quotes e.g. "Java". These double quoted literal is known as String literal and the cache which stored these String instances are known as String pool.

Difference between String literal and String object

At a high level both are String objects, but the main difference comes from the point that the new() operator always creates a new String object.

String a = "Java"; 
String b = "Java"; 
System.out.println(a == b); // True

String c = new String("Java"); 
String d = new String("Java"); 
System.out.println(c == d); // False

String e = "JDK"; 
String f = new String("JDK"); 
System.out.println(e == f); // False

== compares values of operands.
When operands are non-primitive(a.k.a reference) data, their values are memory addresses.

String interning using intern() method

Java by default doesn't put all String objects into the String pool, instead, it gives you the flexibility to explicitly store any arbitrary object in the String pool. You can put any object to the String pool by calling the intern() method of java.lang.String class.

String str0 = new String("newIntern").intern();
String str1 = new String("newIntern").intern();
System.out.println(str0 == str1); // True

String str2 = "literalAutoIntern"; //A string literal always invokes the intern() method
String str3 = "literalAutoIntern"; 
System.out.println(str2 == str3); // True  

String str4 = new String("newInternAndLiteral").intern(); 
String str5 = "newInternAndLiteral";
System.out.println(str4 == str5); // True

Conclusion

You should compare two String objects using the equals() method and avoid comparing them with the == operator, because you would not know which one is coming from pool and which one is created using new operator.

Theme 2

source: https://www.javatpoint.com/signed-and-unsigned-binary-numbers-in-digital-electronics

Signed and Unsigned Binary Numbers

Unsigned Numbers

There is no sign bit in unsigned binary numbers so it can only represent its magnitude. There is only one zero (0) in this representation, which is always positive. Because of one unique binary equivalent form, it is known as unambiguous representation technique. The range of the unsigned binary numbers starts from 0 to 2n2^n-1.

Signed Numbers

The signed numbers are represented in three ways. The signed bit makes two possible representations of zero (positive (0) and negative (1)), which is an ambiguous representation. The third representation is 2's complement representation in which no double representation of zero is possible, which makes it unambiguous representation. There are the following types of representation of signed binary numbers:

  • Sign-Magnitude form
    In this form, a binary number has a bit for a sign symbol. If this bit is set to 1, the number will be negative else the number will be positive if it is set to 0. Apart from this sign-bit, the n-1 bits represent the magnitude of the number.
  • 1's Complement
    By inverting each bit of a number, we can obtain the 1's complement of a number. The negative numbers can be represented in the form of 1's complement. In this form, the binary number also has an extra bit for sign representation as a sign-magnitude form.
  • 2's Complement
    By inverting each bit of a number and adding plus 1 to its least significant bit, we can obtain the 2's complement of a number. The negative numbers can also be represented in the form of 2's complement. In this form, the binary number also has an extra bit for sign representation as a sign-magnitude form.
profile
What, How, Why

0개의 댓글