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();

Java 中,将字符串(String)转换为对象(Object)有多种方法,以下是一些常见的方式: ### 直接赋值 由于 `String` 类是 `Object` 类的子类,所以可以直接将 `String` 类型的变量赋值给 `Object` 类型的变量。 ```java public class StringToObjectDirect { public static void main(String[] args) { String str = "Hello, World!"; Object obj = str; System.out.println(obj); } } ``` ### 使用 `MessageBuilder` 构建 `Message` 对象 可以通过 `MessageBuilder` 构建 `Message` 对象,将字符串内容转换为 `Message` 对象的一部分。 ```java import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; public class StringToObjectMessage { public static void main(String[] args) { String content = "Hello, World!"; Message message = MessageBuilder.withBody(content.getBytes()) .setContentType(org.springframework.messaging.MessageProperties.CONTENT_TYPE_JSON) .setContentEncoding("utf-8") .build(); Object obj = message; System.out.println(obj); } } ``` ### 通过反序列化 如果字符串是序列化后的对象数据,可以通过反序列化将其转换为对象。以下是一个简单的示例: ```java import java.io.*; class MyClass implements Serializable { private String data; public MyClass(String data) { this.data = data; } @Override public String toString() { return "MyClass{" + "data='" + data + '\'' + '}'; } } public class StringToObjectDeserialization { public static void main(String[] args) throws IOException, ClassNotFoundException { MyClass myObject = new MyClass("Hello, World!"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(myObject); oos.flush(); byte[] bytes = bos.toByteArray(); String serializedString = new String(bytes); ByteArrayInputStream bis = new ByteArrayInputStream(serializedString.getBytes()); ObjectInputStream ois = new ObjectInputStream(bis); Object obj = ois.readObject(); System.out.println(obj); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值