官术网_书友最值得收藏!

Strings

Let's talk about strings in Java. To begin, create a new NetBeans project, name it StringsInJava, and enter the main() function. Then, declare two variables: a character called c and String called s. Right away, it becomes clear to us that String is a little different. You'll notice that NetBeans did not choose to color code our String keyword with blue, as it would have done if we were declaring a variable of a primitive type:

This is because String, unlike char, is not a primitive type. String is what we call a class. Classes are the backbone of object-oriented programming. Just as we can declare variables of a primitive type, we can also declare variables of a class, which are called instances. In our program, the variable s is an instance of the String class. Unlike variables of primitive types, instances of a class can contain their own special methodologies and functions declared by the class of which they are an instance. In this section, we'll use some of these string-specific methods and functions to manipulate text.

But first, let's take a look at what makes the String class so special. As we know, we can pretty much use our character variables and our character literals interchangeably, as we can with just about any other primitive type. The String class also maps interchangeably with the string literal, which is like a character literal but uses double quotation marks and can contain many or no characters at all. Most Java classes do not map to any sort of literal, and our ability to manipulate string literals through the String class is what makes it so valuable.

主站蜘蛛池模板: 玛沁县| 平和县| 泸溪县| 顺昌县| 施甸县| 黄大仙区| 阿拉善左旗| 苍溪县| 宁南县| 兴宁市| 元阳县| 禹州市| 鄱阳县| 饶平县| 饶平县| 虎林市| 太湖县| 菏泽市| 广德县| 铁岭县| 天镇县| 康乐县| 江北区| 耿马| 沁水县| 蓝田县| 马鞍山市| 淄博市| 邢台县| 长垣县| 贵定县| 鹤岗市| 广安市| 塔城市| 汤阴县| 洪江市| 乌鲁木齐县| 策勒县| 西乌珠穆沁旗| 铜梁县| 曲水县|