In the actual development and application of java projects, it is often necessary to use the basic function of converting objects to String. This article will provide a summary of commonly used conversion methods. Commonly used methods include Object.toString(), (String) the object to be converted, String.valueOf(Object), etc. These methods are analyzed one by one below.
Method 1: Use the Object.toString() method please see below Example:
Object object = getObject();
System.out.println(object.toString());
In this usage method, because there is a public method .toString() in the java.lang.Object class, this method can be called on any java object in the strict sense. But be careful when using it, you must ensure that the object is not a null value, otherwise a NullPointerException will be thrown. When using this method, usually the derived class will override the toString() method in Object.
Method 2: Use the type conversion (String) object method. This is a standard type conversion. , convert the object into a String type value. When using this method, it should be noted that the type must be convertible to String type. Therefore, it is best to use instanceof to do a type check to determine whether it can be converted. Otherwise, it is easy to throw CalssCastException. In addition, special care is required because it is defined as Object
The syntax check will not report an error when an object of type is converted to String, which may lead to potential errors. Be extra careful at this time. For example:
Object obj = new Integer(100);
String strVal = (String) obj;
There will be an error at runtime because the Integer type is forced to be converted to the String type and cannot be passed. However,
Integer obj = new Integer(100);
String strVal = (String)obj;
If this is the format code, A syntax error will be reported.
In addition, because the null value can be cast to any java class type, (String)null is also legal.
Method 3: Using String.valueOf(Object) The basis of String.valueOf(Object) is Object.toString(). But it is different from Object#toString(). As mentioned in the previous analysis of method 1, when using the latter, it must be ensured that it is not null. But when using the third method, you don't have to worry about whether the object is a null value. In order to facilitate the explanation of the problem, let's analyze the relevant source code. The source code of String.valueOf(Object) in Jdk is as follows:
public static String valueOf(Object obj) {
return (obj == null) ? " null" : obj.toString(); }
From the above source code, we can clearly see the reason why there is no need to worry about null values. However, this also gives us hidden dangers. We should note that when object is null, the value of String.valueOf(object) is the string "null", not null! ! ! Remember to pay attention during use. Just imagine what problems may occur if we use statements like if(String.valueOf(object)==null){System.out.println("The value passed in is null!");}. Think again, when outputting to the console, what is the visual difference in the execution results of the following statements:
System.out.println(String.valueOf(null ));//It is the string "null"
System.out.println(null);//It is the empty value null
The output we see will be exactly the same thing: null, but do they mean the same thing?
Related recommendations】
1. Detailed explanation of valueOf method examples in java
2. valueOf, parseInt, toString in Java The difference between the three
3. In-depth understanding of the valueOf function and toString method
4. Introduction to the object conversion function toString() and valueOf()
5. Use toString() method to return the time as a string
The above is the detailed content of Difference between valueOf and toString,(String) in Java. For more information, please follow other related articles on the PHP Chinese website!

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

我们知道在C语言中,'while'关键字用于定义一个循环,该循环根据传递给循环的条件来工作。现在,由于条件可以有两个值,即真或假,所以如果条件为真,则while块内的代码将被重复执行,如果条件为假,则代码将不会被执行。现在,通过将参数传递给while循环,我们可以区分while(1)和while(0),因为while(1)是一个条件始终被视为真的循环,因此块内的代码将开始重复执行。此外,我们可以说明,传递给循环并使条件为真的不是1,而是如果任何非零整数传递给while循环,则它将被视为真条件,因

使用Java的String.valueOf()函数将基本数据类型转换为字符串在Java开发中,当我们需要将基本数据类型转换为字符串时,一种常见的方法是使用String类的valueOf()函数。这个函数可以接受基本数据类型的参数,并返回对应的字符串表示。在本文中,我们将探讨如何使用String.valueOf()函数进行基本数据类型转换,并提供一些代码示例来

win32和win64的区别是:1、win32是指Microsoft Windows操作系统的32位环境,win64是指Microsoft Windows操作系统的64位版本,比32位版本更加稳定快速;2、win32最高支持2G的内存,win64必须是4G以上内存;3、win64支持基于64位的处理器,而win32却不能完全支持;4、win32追求简洁,win64追求性能。

使用Java的Boolean.valueOf()函数将字符串转换为布尔值在Java编程中,经常会遇到需要将字符串转换为布尔值的情况。而Java提供了一个便捷的方法来实现这一需求,即使用Boolean.valueOf()函数。该函数可以将字符串表示的布尔值转换为对应的布尔类型。下面我们来详细了解一下Boolean.valueOf()的用法。给定一个字符串,我们

在C中,结构体和数组都用作数据类型的容器,即在结构体和数组中我们都可以存储数据,也可以对它们执行不同的操作。基于内部实现,以下是两者之间存在一些基本差异。Sr.编号键结构数组1定义结构体可以定义为一种数据结构,用作容器,可以容纳不同类型的变量。另一方面,数组是一种用作容器的数据结构,可以容纳相同类型的变量,但不支持多种数据类型变量。2内存分配输入数据的内存分配结构不必位于连续的内存位置。而在数组的情况下,输入数据存储在连续的内存分配中,这意味着数组将数据存储在分配连续内存块的内存模型中(即,具有

Vue3和Vue2的区别:更丰富的生命周期钩子Vue是一种流行的JavaScript框架,用于构建交互式的Web应用程序。Vue2是Vue.js的稳定版本,而Vue3是Vue.js的最新版本。Vue3带来了许多改进,其中之一是更丰富的生命周期钩子。本文将介绍Vue3和Vue2生命周期钩子的区别,并通过代码示例进行演示。Vue2的生命周期钩子在Vue2中,我们

JavaScriptCookie使用JavaScriptcookie是记住和跟踪偏好、购买、佣金和其他信息的最有效方法。更好的访问者体验或网站统计所需的信息。PHPCookieCookie是存储在客户端计算机上的文本文件并保留它们用于跟踪目的。PHP透明地支持HTTPcookie。JavaScriptcookie如何工作?您的服务器将一些数据发送到访问者的浏览器cookie的形式。浏览器可以接受cookie。如果存在,它将作为纯文本记录存储在访问者的硬盘上。现在,当访问者到达站点上的另一个页面时


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
