The difference between final in Java and Const in C + + __c++
Source: Internet
Author: User
(1) Final in Java to define constants, can be used for basic types or class types, if the action on the class type, then this class type cannot be the parent
Class is inherited, which means that it cannot have subclasses below it, such classes are called atomic classes.
Const in C + + defines constants,
(2) Final in Java if it's for the base type, it's the same as C + + Const.
But if it's the object, it's different.
(3) final indicates that the handle is immutable
Final Object obj= (object) New String ("a");
Obj= (Object) New String ("Hello"); it's illegal.
However, you can still invoke the method of obj. such as ((String) obj). Length () is legal
C + + If an object is defined as a const, it cannot invoke the object's method. Unless this method is defined as a const.
Package test;
/* Final indicates that this handle is not to be changed
Final Object obj= (object) New String ("a");
Obj= (Object) New String ("Hello"); it's illegal.
However, you can still invoke the method of obj. such as ((String) obj). Length () is legal * *
public class Test {
public static void Main (string[] args) {
Final Object obj = (Object) New String ("a");
Obj= (Object) New String ("Hello"); Cannot assign to the final state local variable obj
System.out.println (((String) obj). Length ()); But the method that can still call obj
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service