转换和解析之间的区别

发布于 2024-11-07 20:48:09 字数 502 浏览 0 评论 0原文

我已经研究一些代码有一段时间了。现在我只是按原样使用关键字,而没有真正理解它们。所以这是我的问题

铸造和解析之间有什么区别?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String-->这是铸造还是解析?

(字符串)UserAdapter.GetIdAndUserTypeByEmailAndPassword(电子邮件,密码).Rows[0][“UserType”] --> 这是强制转换还是解析?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(电子邮件、密码).Rows[0]["UserType"].ToString()
x.ToString()(String) x 有什么区别?

I have been working on some code for a while. for now i have just used the keywords as-is without actually understanding them. So here is my question

What is the difference between Casting and Parsing?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String--> is this Casting or parsing?

(String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"]
-->is this Casting or parsing?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"].ToString()
What is the difference bewtween x.ToString() and (String) x?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2024-11-14 20:48:09

转换和解析有什么区别?

这些是无关的。

强制转换就是改变变量的类型。

解析是“检查”字符串并将其逻辑值分配给某个变量。

(补充:嗯,它们在某种意义上是相关的,因为从很远的地方来看,两者都可以用于“转换”数据,但是,数据只有在解析时才会真正转换)

UserAdapter.GetIdAndUserTypeByEmailAndPassword(电子邮件,密码).Rows[0]["UserType"] 作为字符串

这是强制转换还是解析?

这是一种特殊的转换,如果类型不可转换,它也不会失败(请参阅 这里),但会让你null

(字符串)UserAdapter.GetIdAndUserTypeByEmailAndPassword(电子邮件,密码).Rows[0][“UserType”]

这是强制转换还是解析?

这又是强制转换,但如果表达式不是 string 类型,则会抛出异常。

x.ToString() 和 (String) x 有什么区别?

x.ToString() 将尝试对对象 x 调用 ToString()。

(String) x 将尝试将 x 转换为字符串,如果 x 不是字符串,则会失败。

What is the difference between Casting and Parsing?

Those are unrelated.

Casting is changing the type of the variable.

Parsing is 'examining' the string and assigning its logical value to some variable.

(ADDITION: Well, they are related in a sense, because from far far away both can serve to 'convert' data, however, data is really converted ONLY in case of parsing)

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String

is this Casting or parsing?

This is special kind of casting that will not fail if types aren't convertible (look here), but will get you null.

(String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"]

is this Casting or parsing?

This again is casting, but will throw an exception if expression isn't of type string.

What is the difference bewtween x.ToString() and (String) x?

x.ToString() will try to call ToString() on the object x.

(String) x will try to cast x to string, and will fail if x isn't string.

与君绝 2024-11-14 20:48:09

区别如下:

char a = '3';
int number = 30;
if(number % (int) a == 0) System.out.println(true);
else System.out.println(false);

上面的代码将打印 false,因为我们正在进行类型转换(只是更改数据类型)。

if(number % Integer.parseInt(String.valueOf(a)) == 0) System.out.println(true);
else System.out.println(false);

现在上面的代码将打印 true,因为我们正在解析(逻辑设置)值。

那么有什么区别(请参阅此示例):

int value = 11;
char a = (char) value;
System.out.println(a);  // result is -> 

正确方法:

int value = 11;
char a = String.valueOf(value).toCharArray()[0];
System.out.println(a);

为什么,因为 char 只能存储 1 个字符(当然!)。

摘要-类型转换只是更改数据类型,而解析意味着逻辑地设置值并生成预期结果,而不是这样。

HERE IS THE DIFFERENCE:

char a = '3';
int number = 30;
if(number % (int) a == 0) System.out.println(true);
else System.out.println(false);

Above code will print false, because we are typecasting (just changing the data type).

if(number % Integer.parseInt(String.valueOf(a)) == 0) System.out.println(true);
else System.out.println(false);

Now above code will print true because we are parsing (logically setting) value.

SO WHAT IS THE DIFFERENCE (see this example):

int value = 11;
char a = (char) value;
System.out.println(a);  // result is -> 

Correct way :

int value = 11;
char a = String.valueOf(value).toCharArray()[0];
System.out.println(a);

Why because char can store only 1 character (ofcourse!).

SUMMARY - typecasting is just changing the datatype WHILE parsing means logically setting the values and generate expected result not like this .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文