Strings and Functions
The String type
Unlike primitive types, String is an object that represents a sequence of characters.
Immutability
Strings in Java are immutable. If you change a String value, Java creates a new object in memory.
String saludo = "Hola";
saludo = saludo + " mundo"; // A new object "Hola mundo" is created
Useful String methods
length(): Returns the text length.toUpperCase()/toLowerCase(): Converts to uppercase/lowercase.contains("text"): Checks if it contains a substring.indexOf("a"): Searches for the position of a character.equals("other"): Compares contents (don’t use==!).
Functions (Methods)
A function is a reusable block of code that performs a specific task.
public static int suma(int a, int b) {
return a + b;
}
// Call:
int resultado = suma(5, 3);
Structure of a function:
- Return type: The data it returns (
int,String,voidif it returns nothing). - Name: To identify it.
- Parameters: Data it receives to work with.
- Body: The code inside
{ }.