简体   繁体   English

铸造和解析的区别

[英]Difference between Casting and Parsing

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? UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String --> 这是转换还是解析?

(String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] -->is this Casting or parsing? (String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] -->这是转换还是解析?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"].ToString()
What is the difference bewtween x.ToString() and (String) x ? x.ToString()(String) 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 .这是一种特殊的类型转换,如果类型不可转换,它不会失败(看这里),但会让你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 .这又是强制转换,但如果表达式不是string类型,则会抛出异常。

What is the difference bewtween x.ToString() and (String) x? x.ToString() 和 (String) x 有什么区别?

x.ToString() will try to call ToString() on the object x. x.ToString()将尝试在对象 x 上调用 ToString()。

(String) x will try to cast x to string, and will fail if x isn't string. (String) x将尝试将 x 转换为字符串,如果 x 不是字符串,则会失败。

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).上面的代码将打印 false,因为我们正在类型转换(只是更改数据类型)。

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.现在上面的代码将打印 true,因为我们正在解析(逻辑设置)值。

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!).为什么因为 char 只能存储 1 个字符(当然!)。

SUMMARY - typecasting is just changing the datatype WHILE parsing means logically setting the values and generate expected result not like this ?.总结 - 类型转换只是改变数据类型,而解析意味着逻辑地设置值并生成预期的结果,而不是这样?。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM