Wednesday, June 8, 2016

String Introduction

So far already explained simple data type, variable, and array. But why the string type not yet explained? The answer is, in Java, string is not a simple type, and not also an array of character (such as the concept of string in C/C++). In Java, a string is defined as an object. For that reason, it will be explained later in next post. Java has defined String object to represent string type. As an introduction, the following is an example of declaring a variable of string type in Java:

String str;
str = "Sample string in Java";

Here, str is an object of String type, instead of variable of simple type. Therefore, str has special method to manipulate its value. Thus, we can call those methods from String type through str object, as shown in the following code sample:

int jPosition = str.charAt('J');
System.out.println(str.toUppercase());
...

The string type will be explained more detail in next post. Nevertheless, we will often use it in our program examples. The only you need to know for now, is just how you declare object of String type.

1 comment: