STRING HANDLING ================ what is string? =============== Collection/group of characters which are enclosed with double quotes (""). Difference between strings in C and Java: ========================================= Array: Collection of similar data items char a[] = {'a', 'e', 'i', 'o', 'u'}; "Hello" = {'H', 'e', 'l', 'l', 'o'}; In C": "Hello" = {'H', 'e', 'l', 'l', 'o', '\0'}; ==> In C programming language, the string can always end with null character (\0). Whereas in Java, the string never end with null character because the string definition in java act as an object. Why Strings? ============ int a = Scanner_Object.nextInt(); Scanner class import java.util.Scanner; main() { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int a = sc.nextInt(); } Internally: String a = sc.nextLine(); int b = Integer.parseInt(a); int a = sc.nextInt(); a = variable if type "int" sc = Scanner object nextInt() = method to convert the string data into integer int ==> Integer float ==> Float double ==> Double char ==> Character Wrapper classes Wrapping ======== Converting the string data into primitive data String => int String ==> Float etc Type casting ============ Converting of primitive data to another primitive data int ==> float Boxing ====== Converting the primitive data to reference type Unboxing ======= Converting the reference type to primitive type Note: Java ==> automatic boxing 9/7 ==> 1 1.222 ====================== How to define the strings in Java? ================================== Strings can be defined in two ways: 1) String literal 2) String Object String Literal: =============== Syntax: String variable_name = "String_value"; String Object: ============== Syntax: String variable_name = new String("String-value"); class Strings{ public static void main(String[] args) { // string literal form String s1 = "Hello"; // string object String s2 = new String("Ashok IT"); System.out.println("String1 = "+s1); System.out.println("String2 = "+s2); } } ============================== What is the difference between String literal and String object? ================================================================ ==> If the string can define in string literal format: the string object value can store in SCP (String Constant Pool) of JVM Memory. ==> If the string can define in string object format: the string object value can store in heap memory. ================================= ==> String is a built-in class in java ==> String class in java have defined in a built-in and automatic imported package: "java.lang"