DEV Community

Pavithra C
Pavithra C

Posted on

Day-47:String

** String Constant Pool in Java**

The String Constant Pool (also called the String Intern Pool) is a special area in the heap memory where Java stores string literals to optimize memory usage and improve performance.

String is Immutable:
Immutable: Non-Changeable

public class StringImmutableDemo {
    public static void main(String[] args) {
        String str = "Hello";

        System.out.println("Original String: " + str);

        // Try to change the string
        str.concat(" World");

        System.out.println("After concat without assignment: " + str);

        // Now assign the result to the string
        str = str.concat(" World");

        System.out.println("After concat with assignment: " + str);
    }
}

Enter fullscreen mode Exit fullscreen mode

output:
Original String: Hello
After concat without assignment: Hello
After concat with assignment: Hello World


String citizen1 = "Singapore Citizen";
String citizen2 = "Indian";
String citizen3 = "Indian";
String citizen4 = "Indian";
String citizen5 = "Indian";

System.out.println(citizen1.hashCode());
System.out.println(citizen2.hashCode());
System.out.println(citizen3.hashCode());

citizen1 = "Singapore Citizen"; // Reassigning the same string again

System.out.println(citizen1.hashCode());
System.out.println(citizen2.hashCode());
System.out.println(citizen3.hashCode());

Enter fullscreen mode Exit fullscreen mode

output:
-1143432508
-2081375228
-2081375228
-1143432508
-2081375228

-2081375228

Hash code:[TBD]
In Java, every object has a method called hashCode() which returns an integer. This integer is called the hash code of the object and is used to efficiently store and retrieve objects in data structures like HashMap, HashSet, and Hashtable.

What is a Hashing Algorithm?
A hashing algorithm is a function that takes input data (like a string, number, file, etc.) and converts it into a fixed-size value (usually an integer or a hexadecimal string), called a hash code or hash value.

The Luhn Algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate identification numbers, such as:

  • Credit card numbers
  • Debit card numbers
  • IMSI numbers (used in mobile phones)
  • National identification numbers

Purpose of the Luhn Algorithm
It helps detect simple errors like:

  • Typing a wrong digit
  • Swapping digits

How It Works (Step-by-Step)
Let's take an example: 4539 1488 0343 6467 (a credit card number)
Step 1: Start from the right, double every second digit

Card Number:        4 5 3 9  1 4 8 8  0 3 4 3  6 4 6 7
Index (L → R):      0 1 2 3  4 5 6 7  8 9 10 11 12 13 14 15
Double every 2nd:      ↑   ↑   ↑   ↑   ↑   ↑   ↑   ↑
Doubled Values:     4 10 3 18 1 8 8 16 0 6 4 6 6 8 6 14

Enter fullscreen mode Exit fullscreen mode

Step 2: If any doubled value is greater than 9, subtract 9 from it

Adjusted:           4 1 3 9 1 8 8 7 0 6 4 3 6 8 6 5

Enter fullscreen mode Exit fullscreen mode

Step 3: Sum all the digits

Total sum = 4 + 1 + 3 + 9 + 1 + 8 + 8 + 7 + 0 + 6 + 4 + 3 + 6 + 8 + 6 + 5 = 89

Enter fullscreen mode Exit fullscreen mode

Step 4: If total sum mod 10 == 0, the number is valid

89 % 10 = 9 → ❌ Invalid

Enter fullscreen mode Exit fullscreen mode

Top comments (0)