DSA Strings and StringBuilder in java

[[String revision in HINGLISH]]

any thing which start with capital letter is a class alt text alt text

String pool : separate memory structure inside a heap

alt text

alt text

make a program more optimise

Strings are immutable we cannot change it we cannot change object

alt text

alt text

alt text

comparison of string

alt text

Create diff objects of Same value

alt text

alt text

pretty printing in java

alt text output: 453.16f

‘%’ is called placeholder

System.out.printf("pie: %.3f", Math.PI);

output: 3.142

Here’s a tabular representation of the commonly used format specifiers in Java. These are used with methods like System.out.printf() or String.format() for formatted output:

SpecifierDescriptionExampleOutput
%dInteger (decimal)System.out.printf("%d", 42);42
%fFloating-point numberSystem.out.printf("%.2f", 3.14);3.14
%eScientific notation (lowercase)System.out.printf("%e", 12345.678);1.234568e+04
%EScientific notation (uppercase)System.out.printf("%E", 12345.678);1.234568E+04
%cCharacterSystem.out.printf("%c", 'A');A
%sStringSystem.out.printf("%s", "Java");Java
%bBooleanSystem.out.printf("%b", true);true
%%Literal % symbolSystem.out.printf("%%");%
%oOctal integerSystem.out.printf("%o", 10);12 (octal)
%xHexadecimal integer (lowercase)System.out.printf("%x", 255);ff (hex)
%XHexadecimal integer (uppercase)System.out.printf("%X", 255);FF (hex)
%tDate/Time specifier (see below)
%nNewline characterSystem.out.printf("Hello%nWorld");Multi-line

Additional Date/Time Specifiers (with %t)

SpecifierDescriptionExampleOutput
%tHHour (24-hour format)System.out.printf("%tH", new Date());14 (e.g., 2 PM)
%tIHour (12-hour format)System.out.printf("%tI", new Date());02
%tMMinuteSystem.out.printf("%tM", new Date());30
%tSSecondSystem.out.printf("%tS", new Date());45
%tpAM/PM marker (lowercase)System.out.printf("%tp", new Date());pm
%tBFull month nameSystem.out.printf("%tB", new Date());December
%tbAbbreviated month nameSystem.out.printf("%tb", new Date());Dec
%tAFull day nameSystem.out.printf("%tA", new Date());Friday
%taAbbreviated day nameSystem.out.printf("%ta", new Date());Fri
%tYYear (4-digit)System.out.printf("%tY", new Date());2024
%tyYear (last 2 digits)System.out.printf("%ty", new Date());24

Formatting Options

You can combine format specifiers with flags for more control:

FlagDescriptionExampleOutput
-Left-alignSystem.out.printf("%-10d", 123);123
+Print sign for numbersSystem.out.printf("%+d", 123);+123
0Pad with zeroesSystem.out.printf("%05d", 123);00123
,Use grouping separatorSystem.out.printf("%,d", 1234567);1,234,567
.Precision for floating-pointSystem.out.printf("%.2f", 3.14159);3.14

Example Program

import java.util.Date;

public class FormatSpecifierDemo {
    public static void main(String[] args) {
        System.out.printf("Integer: %d%n", 42);
        System.out.printf("Float: %.2f%n", 3.14159);
        System.out.printf("Character: %c%n", 'A');
        System.out.printf("String: %s%n", "Hello");
        System.out.printf("Hexadecimal: %x%n", 255);
        System.out.printf("Current Time: %tT%n", new Date());
    }
}

operators

alt text

alt text

String builder (only one obj is made and the changes is done in that obj only)\

we can’t name class as StringBuilder as it is already a class alt text

alt text

alt text

Reverse a string use

string builder reverse (“builder.reverse”)

String Methods

alt text

alt text


Palindrome of a string

algorithm:

  • if str == null || str.length() == 0 return true
  • i = 0; i = <= str.length()/2 ; i++

alt text