隐式转换和显式转换的区别

发布于 2024-12-06 12:07:33 字数 211 浏览 0 评论 0 原文

可能的重复:
隐式与显式转换

“隐式转换”和“显式转换”有什么区别? Java和C++有什么不同吗?

Possible Duplicate:
Implicit VS Explicit Conversion

What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++?

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

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

发布评论

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

评论(3

秋千易 2024-12-13 12:07:33

显式转换是指您使用某种语法来告诉程序进行转换。例如(在 Java 中):

int i = 999999999;
byte b = (byte) i;  // The type cast causes an explicit conversion
b = i;              // Compilation error!!  No implicit conversion here.

隐式转换是指在没有任何语法的情况下进行转换。例如(在 Java 中):

int i = 999999999;
float f = i;    // An implicit conversion is performed here

应该注意的是(在 Java 中)涉及基本类型的转换通常涉及表示形式的某些更改,这可能会导致精度损失或信息丢失。相比之下,(仅)涉及引用类型的转换不会改变基本表示形式。


Java 和 C++ 之间有什么不同吗?

我不这么认为。显然,可用的转换会有所不同,但“隐式”和“显式”之间的区别是相同的。 (注:我不是 C++ 语言专家……但这些词在英语中具有自然含义,我无法想象 C++ 规范会以矛盾的方式使用它们。)

An explicit conversion is where you use some syntax to tell the program to do a conversion. For example (in Java):

int i = 999999999;
byte b = (byte) i;  // The type cast causes an explicit conversion
b = i;              // Compilation error!!  No implicit conversion here.

An implicit conversion is where the conversion happens without any syntax. For example (in Java):

int i = 999999999;
float f = i;    // An implicit conversion is performed here

It should be noted that (in Java) conversions involving primitive types generally involve some change of representation, and that may result in loss of precision or loss of information. By contrast, conversions that involve reference types (only) don't change the fundamental representation.


Is the difference different in Java and C++?

I don't imagine so. Obviously the conversions available will be different, but the distinction between "implicit" and "explicit" will be the same. (Note: I'm not an expert on the C++ language ... but these words have a natural meaning in English and I can't imagine the C++ specifications use them in a contradictory sense.)

请远离我 2024-12-13 12:07:33

你是说选角吗?
隐式意味着你传递一个类型的实例,比如 B,它继承自一个类型,比如 A 作为 A。

例如:

Class A;
Class B extends A;

function f(A a) {...};

main() {
  B b = new B;
  f(b); // <-- b will be implicitly upcast to A.
}

实际上还有其他类型的隐式转换 - 在基元之间,使用默认构造函数。您的问题必须更加具体。

带有默认构造函数的隐式:

class A { 
  A (B b) { ... };
}

class B {};

main() {
  B b = new B();
  A a = b; // Implict conversion using the default constructor of A, C++ only.
}

You Mean Casting?
Implicit mean you pass an instance of type, say B, that inherits from a type, say A as A.

For example:

Class A;
Class B extends A;

function f(A a) {...};

main() {
  B b = new B;
  f(b); // <-- b will be implicitly upcast to A.
}

There are actually other types of implicit castings - between primitives, using default constructors. You will have to be more specific with your question.

implicit with default constructor:

class A { 
  A (B b) { ... };
}

class B {};

main() {
  B b = new B();
  A a = b; // Implict conversion using the default constructor of A, C++ only.
}
月朦胧 2024-12-13 12:07:33

强制转换是一种显式类型转换,在代码中指定,并且在编译时遵循很少的规则。强制转换可能不安全;它们可能在运行时失败或丢失信息。
隐式转换是编译器执行的类型转换或原始数据转换,以符合数据提升规则或匹配方法的签名。在 Java 中,仅执行安全的隐式转换:向上转换和提升。\

另外,我建议阅读有关 C++ 隐式转换的内容:http://blogs.msdn.com/b/oldnewthing/archive/2006/05/24/605974.aspx

Casting is an explicit type conversion, specified in the code and subject to very few rules at compile time. Casts can be unsafe; they can fail at run-time or lose information.
Implicit conversion is a type conversion or a primitive data conversion performed by the compiler to comply with data promotion rules or to match the signature of a method. In Java, only safe implicit conversions are performed: upcasts and promotion.\

Also I would suggest reading about C++ implicit coversion: http://blogs.msdn.com/b/oldnewthing/archive/2006/05/24/605974.aspx

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