0% found this document useful (0 votes)
27 views2 pages

Java Cheatsheet String Final

This document is a Java cheatsheet covering Strings, StringBuilder, and formatting. It includes examples of string comparison, string methods, character and string addition, StringBuilder usage, and formatted output with printf format specifiers. Additionally, it lists common printf format specifiers with their meanings and example outputs.

Uploaded by

edu.kamesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Java Cheatsheet String Final

This document is a Java cheatsheet covering Strings, StringBuilder, and formatting. It includes examples of string comparison, string methods, character and string addition, StringBuilder usage, and formatted output with printf format specifiers. Additionally, it lists common printf format specifiers with their meanings and example outputs.

Uploaded by

edu.kamesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Cheatsheet: Strings, StringBuilder & Formatting

Java String, StringBuilder and Print Formatting


// String comparison
String a = "Kunal";
String b = "Kunal";
String c = a;
[Link](a == b); // true
[Link]([Link](b)); // true

String name1 = new String("Kunal");


String name2 = new String("Kunal");
[Link](name1 == name2); // false
[Link]([Link](name2)); // true

// String methods
String name = "Kunal Kushwaha Hello World";
[Link]([Link]([Link]()));
// [K, u, n, a, l, , K, u, s, h, w, a, h, a, , H, e, l, l, o, , W, o, r, l, d]
[Link]([Link]()); // kunal kushwaha hello world
[Link](name); // Kunal Kushwaha Hello World
[Link]([Link]('a')); // 3
[Link](" Kunal ".strip()); // Kunal
[Link]([Link]([Link](" ")));
// [Kunal, Kushwaha, Hello, World]

// Char and String addition


[Link]('a' + 'b'); // 195
[Link]("a" + "b"); // ab
[Link]((char)('a' + 3)); // d
[Link]("a" + 1); // a1
[Link]("a" + 'b'); // ab

// StringBuilder example
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 26; i++) {
char ch = (char) ('a' + i);
[Link](ch);
}
[Link]([Link]()); // abcdefghijklmnopqrstuvwxyz
[Link]();
[Link](builder); // zyxwvutsrqponmlkjihgfedcba

// Formatted output
float x = 453.1274f;
[Link]("Formatted number is %.2f%n", x); // 453.13
[Link]("Pie: %.3f%n", [Link]); // 3.142
[Link]("Hello my name is %s and I am %s%n", "Kunal", "Cool");
// Hello my name is Kunal and I am Cool
Java Cheatsheet: Strings, StringBuilder & Formatting

Common printf Format Specifiers

Specifier Meaning Example Output

%c Character A

%d Decimal/Integer 100

%e Exponential floating-point 1.200e+03

%f Floating-point number 3.142

%i Integer (same as %d) 42

%o Octal 12

%s String hi

%u Unsigned integer N/A in Java

%x Hexadecimal ff

%t Date/time (used with suffix) tD -> 12/30/23

%n Newline

You might also like