How C + + programmers go to Java

Source: Internet
Author: User

how C + + programmers go to Java

Sneak in, at this time finally hollow write an early want to write an article. In fact, the title of this article is somewhat less accurate, C + + programmer to write Java code is not to say that you have to change to write Java, discard C + +, but only one more choice. Each of the two languages have advantages, here we do not talk about the struggle for language, it is assumed that a friend needs a role change, wrote for many years C + +, a sudden project needs to be written in Java.

The Java language has a lot of similarities to the C + + language syntax, but it is inherently different. A qualified C + + programmer will have to write down every line of code in the heart of the time when the entire program runs, how each variable in the code is stored, what is the structure of the stack, which block of memory if handled improperly will be a big trouble. But Java programmers are a little different, because the Java language Specification and Java Virtual machine specifications are separate, plus automatic GC, the maneuverability is somewhat poor, but this is not a bad thing, after all, manual management of memory has its complexity.

Personally, for C + + programmers, Java syntax is absolutely no problem, it can be said that more than 80% of the structure is the same, so this is not a problem. But have you learned English grammar and you can have English level six? Can we have a meeting with the foreigner? Not necessarily. By the way, we have to learn dialect, idiomatic methods and patterns, so-called in the Romans, into the Java world we have to follow the rules of Java play. For example, how the object needs to be created, how the two objects are compared, how the enumeration type is used, how the generic code is written, what kind of superstructure the concurrency code has to achieve is better, when to throw what exception in the code, and when to use reflection. I think a person who is more dedicated to technology may also need at least 3 months of project tempering to apprenticeship, of course, I say the premise is that the C + + engineers have a fairly basic, so as to stand a mountain and look at another mountain high.

So the personal thought of C + + programmers to the Java shortcut is: Learn Java idioms, patterns, coupled with the project temper.

another need to say to the C + + Programmer's advantage, because the mainstream JVM is written in C/s code, for the love of deep-travel friends can completely discovering, as long as your debugging technology enough, all panoramic view, this method is also applicable to JavaScript, because V8 is also written in C + + . Here are two Java idioms to share with you.

Idiom 1, Java using enumerations to implement singleton patterns

 Public enumAnimalhelpersingleton {INSTANCE; PrivateAnimalhelpersingleton () {} Publicanimal[] Buildanimallist () {Finalanimal[] Animals =NewAnimal[10]; animals[0] =NewSimpleanimal (Animal.AnimalClass.MAMMAL,"Dog",true, Color.gray); animals[1] =NewSimpleanimal (Animal.AnimalClass.MAMMAL,"Cat",true, Color.yellow); animals[2] =NewSimpleanimal (Animal.AnimalClass.AMPHIBIAN,"Frog",true, Color.green); animals[3] =NewSimpleanimal (Animal.AnimalClass.BIRD,"Crow",true, Color.Black); animals[4] =NewSimpleanimal (Animal.AnimalClass.BIRD,"Cardinal",true, color.red); animals[5] =NewSimpleanimal (Animal.AnimalClass.ARTHROPOD,"Mantis",false, Color.green); animals[6] =NewSimpleanimal (Animal.AnimalClass.ARTHROPOD,"Spider",false, Color.orange); animals[7] =NewSimpleanimal (Animal.AnimalClass.MAMMAL,"Tiger",true, Color.orange); animals[8] =NewSimpleanimal (Animal.AnimalClass.MAMMAL,"Bear",true, Color.Black); animals[9] =NewSimpleanimal (Animal.AnimalClass.BIRD,"Owl",true, Color.Black); returnanimals; }}

How to implement Equals and hashcode with third-party class libraries in idiomatic 2,java

 Public classbean{PrivateString name; Private intlength; PrivateList<bean>Children;} here' s Equals () and hashcode () implemented with Commons/lang:@Override Public inthashcode () {return NewHashcodebuilder (). Append (name). Append (length). Append (children). Tohashcode (); @Override Public BooleanequalsFinalObject obj) {    if(objinstanceofBean) {        FinalBean other =(Bean) obj; return NewEqualsbuilder (). Append (name, Other.name). Append (length, other.length). Append (chil    dren, Other.children). Isequals (); } Else{        return false; }}and here with guava: @Override Public inthashcode () {returnObjects.hashcode (name, length, children);} @Override Public BooleanequalsFinalObject obj) {    if(objinstanceofBean) {        FinalBean other =(Bean) obj; returnobjects.equal (name, other.name)&& length = = Other.length//Special handling for primitives&&objects.equal (children, other.children); } Else{        return false; }}

Summarize

This article through the simple analysis of C + +, Java two language differences, according to personal experience to give the C + + programmer How to turn Java recommendations, and give the Java two common usages, I hope to be helpful to everyone.

How C + + programmers go to Java

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.