← Home

Variable methods

String methods

I've told you previously that String is a class, not a primitive. Therefore it can, and does, have its own non-static methods.


The .length() method returns the length of a string as an int. Unlike (if you know JavaScript) in a JavaScript string, .length() is a method, that must be called with parentheses.

1String name = "George";
2System.out.println(name.length()); // Prints 6

toUpperCase, toLowerCase

There are also the .toUpperCase(), and .toLowerCase() methods, return an uppercase copy and lowercase copy of the original string respectively, e.g:

1String name = "George";
2System.out.println(name.toUpperCase()); // Returns "GEORGE"
3System.out.println(name.toLowerCase()); // Returns "george"


This takes in another string, and returns true/false depending on whether they are equal. Note .equals() is case sensitive. You can use this in if-statements:

1String personA = "George";
2String personB = "George";
3if(personA.equals(personB)) {
4    System.out.println("They're the same person!");


This takes the string you give it and two index numbers, and returns the chars in between them, excluding the value at the final index.

1String a = "last";
3//prints out "l"

As a note, you can also use substring where you only input one index and the string keeps everything after and including the value of that index.


This method searches a string for another string and returns the position at which it starts. If the string cannot be found, -1 is returned.

1String needle = "world";
2String haystack = "hello world";
3System.out.println(haystack.indexOf(needle)); // Prints 6

This is because world starts at the 7th character in hello world, and programmers count from zero, ergo 0 will refer to the 1st character, as 6 will refer to the 7th character.

N.B. There are many many many more of these methods and I would advise looking them up if you need them.

Integer methods

Now, if you care to remember, I once told you that integers are primitive variables. Therefore, theoretically, I shouldn't be able to assign methods to them. But, Java is case-sensitive and these are Integers which are class variables. I told you that variable types with a capital are usually burdens, but it is perfectly accceptable to use their non-static methods i.e. Integer.parseInt() etc, because they do not actually involve instantiating those classes.


You can convert an integer, or a boolean, or a double, to a string, by using Integer.toString(), or its equivalent:

1int age = 13;
2double pi = 3.14159265359;
3Boolean bool = true;
4String ageAsString = Integer.toString(age);
5//Prints out "13"
6String piAsString = Double.toString(pi);
7//Prints out "3.14159265359"
8String  boolAsString = Boolean.toString(bool);
9//Prints out "true"


You can convert a string to an integer by using Integer.parseInt():

1String ageAsString = "13";
2int age = Integer.parseInt(ageAsString);
3//Prints out 13

Boolean methods

Booleans work the same as integers, and the toString() method was shown above. But there is a method that turns a String into a boolean .parseBoolean():

1String aliveAsString = "true";
2alive = Boolean.parseBoolean(aliveAsString);
3//Prints out true

Double methods

Doubles have three major methods, one of which has been shown i.e. the toString() method. There is also the parseDouble() method, which I'm not going to even show because you know what it means. The third major one is the isNaN() method. This outputs true if the input is not a number, and false otherwise:

1String d = "3.1232";
2double myDouble = Double.parseDouble(d);
3System.out.println(Double.isNaN(myDouble)); // prints false
5String f  = "hrglbrgl";
6String myOtherDouble = Double.parseDouble(f);
7System.out.println(Double.isNaN(myOtherDouble)); // prints true

How to use the outputs of methods →