JAVA String Object(2)

           String is a class, and created String is a class object. A string variable name is a reference; that is , a String variable name refers to a location in memory, rather than to a particular value. But when you declare a variable of a basic, primitive type, such as int x=10; the memory address where x is located holds the value 10. If you later assign a new value to x, the new vale replaces the old one at the assigned memory address. For example, if you code x=45; then 45 replaces 10 at the address of x.

           In contrast, when you declare a String, such as String aGreeting=”hello”; aGreeting does not hold the characters “Hello”; instead it holds a memory address where the characters are stored. Strings and other objects that can’t be changed are immutable.

           When you compare Strings with the == operator, you are comparing their memory addresses, not their values. Furthermore, when you try to compare Strings using the less-than(<) or greater than(>) operator, the program will not even compile. The String class provides you with a number of useful methods. The equals() method evaluates the contents of two String objects to determine if they are equivalent.

           When the String class compareTo() method is used to compare two Strings, it provides additional information to the user in the form of an interger value. When you use compareTo() the compare two String objects, the method returns zero only if the two Strings refer to the same value. If there is any difference between the Strings, a negative number is returned if the calling object is “less than” the argument, and a positive number is returned if the calling object is “more than” the argument. Strings are considered “less than” or “more than” each other based on their Unicode values.

           When you use any of the methods to convert a String to a number, but the String does not represent a valid number(for example, if it contains letters), or the String represents the wrong kind of number(for example, it contains a decimal point but is being converted to an integer), an error called a NumberFormatException occurs.

          To convert a String to an integer, you use the Integer class, which is part of java.lang and is automatically imported into programs you write. The Integer class is an example of a wrapper. Aa wrapper is a class or object that is “wrapped around” a simpler element; the Integer wrapper class contains a simple integer and useful methods to manipulate it. You have already used the parseInt() Method, which is part of the Integer class; it takes a String argument and returns its integer value.

          String str=”123”;

          Integer integerNumber=Integer.valueOf(str);

          Int numbers=integerNumber.intValue();

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值