String
is a Java class for representing a
fixed collection of characters. You can represent a String
in your code by using double quotes as follows:
String greeting = "Hello World!";There are some useful methods that you can use:
length()
substring(int beginIndex,int endIndex)
beginIndex
and ending with position endIndex
.
Remeber that position 0 is the first character in the String
valueOf(anything)
String
that represents the argument. For
example, it would convert the integer 64 to "64".
These aren't all the methods, but they will get you started. Also, there
exists the concatination operator, +
, and
a function to print strings, System.out.println
.
String greeting = "Hello World!"; System.out.println(greeting);outputs:
Hello World!
String greeting = "Hello World!"; System.out.println(greeting.substring(0,4));outputs:
Hello
String greeting = "Hello World!"; System.out.println(String.valueOf(greeting.length()));outputs:
12
String greeting = "Hello World!"; int greeting_length = greeting.length(); System.out.println("The length of "+greeting+" is "+greeting_length);outputs:
The length of Hello World! is 12
![]() |
6.4 Arrays |