Use the original Java language

Source: Internet
Author: User
Tags printf

It is much easier to learn a new programming language than to learn a new spoken language. However, in both of these learning processes, extra effort is needed to learn to speak a new language without accents. If you are familiar with C or C + +, it is not difficult to learn the Java language, as it is for people who speak Swedish to learn Danish. Languages are different, but they are interoperable. But if you're not careful, your accent will always reveal the secret of not being a native language user.

C + + programmers tend to make some distortion of Java code, which distinguishes them clearly from native Java language users. Their code can run without error, but for native language users, there is something wrong. As a result, native language users may despise non-native users. When you go from C or C + + (or Basic, Fortran, Scheme, and so on) to the Java language, you need to eradicate some idioms and correct certain sounds so that you can use the new language fluently.

In this article, I explored some of the Java programming details that are often overlooked because they are not semantically important or even irrelevant. They are purely a matter of style and practice. Some of these details have plausible reasons, and others do not even have plausible reasons. But all of these details are real phenomena in the Java code written today.

What language is this?

Let's start with a piece of code that converts Fahrenheit temperatures to degrees Celsius, as shown in Listing 1:

Listing 1. A section of C code?

float F, C;
float min_tmp, max_tmp, x;

min_tmp = 0;
max_tmp = 300;
x = 20;

F = min_tmp;
while (F <= max_tmp) {
   C = 5 * (F-32) / 9;
   printf("%f\t%f\n", F, C);
   F = F + x;
}

What is the language used in Listing 1? It's obviously C-please wait a minute, let's take a look at the complete program, as shown in Listing 2:

Listing 2. Java Programs

class Test {

   public static void main(String argv[]) {
     float F, C;
     float min_tmp, max_tmp, x;

     min_tmp = 0;
     max_tmp = 300;
     x = 20;

     F = min_tmp;
     while (F <= max_tmp) {
      C = 5 * (F-32) / 9;
      printf("%f\t%f\n", F, C);
      F = F + x;
     }
   }

   private static void printf(String format, Object...  args) {
     System.out.printf(format, args);
   }

}

Whether you believe it or not, listing 1 and Listing 2 are written in the Java language. They are just Java code written in C dialect (honestly, listing 1 can also be C code). Here are a few idioms that indicate that the person who wrote this code is thinking in C, but simply translating it into the Java language:

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.