Syntax
String variableName = "text";Examples
Creating and Combining Strings
Basic string creation and concatenation.
String firstName = "Fola";
String lastName = "A";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // Fola A
String greeting = String.format("Hello, %s!", firstName);
System.out.println(greeting);Common String Methods
Length, case conversion, and trimming whitespace.
String text = " Hello World ";
System.out.println(text.length()); // 15 (includes whitespace)
System.out.println(text.trim()); // "Hello World"
System.out.println(text.toUpperCase()); // " HELLO WORLD "
System.out.println(text.toLowerCase()); // " hello world "Searching and Extracting Substrings
Finding content within a string and pulling out parts of it.
String sentence = "The quick brown fox";
System.out.println(sentence.contains("quick")); // true
System.out.println(sentence.indexOf("brown")); // 10
System.out.println(sentence.substring(4, 9)); // "quick"
System.out.println(sentence.replace("fox", "dog")); // The quick brown dogSplitting and Joining
Converting between a string and an array of strings.
String csv = "Fola,Zain,Jamal";
String[] names = csv.split(",");
for (String name : names) {
System.out.println(name);
}
String joined = String.join(" & ", names);
System.out.println(joined); // Fola & Zain & JamalBest practices
- Remember String is immutable - every method like .trim() or .toUpperCase() returns a new String rather than modifying the original
- Use a StringBuilder instead of repeated + concatenation when building a string inside a loop, for much better performance
- Use .equals() (or .equalsIgnoreCase()) for content comparison, never == for comparing String values
- Use String.format() or printf-style formatting for building strings with embedded numbers or aligned output, rather than manual concatenation
At a glance
- Purpose
- General-purpose application development
- File extension
- .java
- Runs in
- Java Virtual Machine
- Usually used with
- JDK and Java libraries
Specifications & further reading
Related Java documentation
Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.