Java (from C ++ 2 Java) (1)

Source: Internet
Author: User
Start learning Java! In fact, I have read a small part of the Java language a month or two before writing this document. At that time, a friend persuaded me to learn it, I hung up an eclipse under the vista of this book and started. But in a few days, I formatted the XP system for various reasons, I have never recorded some basic Java content. I will try it out today:

The Java language is derived from the C ++ language. Naturally, many places are similar to the C ++ language. The biggest difference lies in memory management. You can skip understanding the memory management in Java, although thinking is introduced in Java, you do not need to manually manage the memory to write Program More secure. After using new, you do not need to call the delete method (in fact, it does not exist)

Java operators are basically the same as C ++ operators. The difference is <symbol, unsigned left shift, which is seen when reading the book "programming art, at the same time, we can see that its needs are very practical.

Java does not have the Operator overload of C ++. Therefore, Java must be reloaded internally. It can only use class methods and does not know whether Java implements each function through a virtual function similar to C ++, I remember that C # seems to be implemented in that way.

In Java, the variables in the method must be initialized at the time of definition. Otherwise, the compiler will report an error. For the variables in the class members, Java will automatically initialize a default value for you, which is generally 0, null, false.

All methods in Java are implemented through classes. Therefore, static variables must be used for global access in Java. The implementation of static variables in Java is different from that in C ++. Java contains static statement blocks, such:
Static
{
Static object o = new object ();
}
Static Initialization is performed based on the declared sequence during the first call.

The constructor in Java can use this to call another constructor, but it can only be called once and placed in the first statement in the constructor.

The Finalize method can exist for non-displayed destructor in Java. It is called when the Garbage Collector runs the memory of the collection system, but not as reliable as the delete Statement of C ++!

Java uses the package method to differentiate classes. c ++ uses namespace to divide classes into namespaces. Java uses package to differentiate classes. Generally, the position of a package is searched by java_classpath, generally, the first value of java_classpath is '. ', indicating the current path. Similar to the namespace in C ++, the package is implemented based on the document path, and the namespace is implemented based on the Logical region.

In Java, the suffix of each package is Java and only one public class can be exported. All other classes are declared by default, that is, protected, which is visible inside the package and invisible to other packages, it is very similar to the general static declaration function in C ++.

The modifiers in Java are public and private, and protected is the default modifier. Each class, variable, and method must be displayed with a public or private modifier, which is different from C ++, c ++ is the start declaration until the start of the next Declaration, all of which are of the same Declaration type.

All classes in Java are inherited from objects. Each class uses the default function. The simplest method is the hashcode method and the tostring method.

There is a naming convention in Java, that is, naming all classes as titles. Class Members and methods are named by camel.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.