Jumping into Java

zhaozj2021-02-08  461

Jumping into Java

Transparent translation

I saw that many people are asking: I should learn Java; I saw, many people are asking: Java and C which better?

I think I am not eligible to answer these questions. But I want Cuj qualifications. This article comes from the import.java. * Column - open a Java column in CUJ, which itself has proved that the majority of C programmers pay attention to Java. Which is better? Where is it? Will you learn? What should I learn? What should I learn? ... too much problem. I hope this article can give a brick to jade. I hope that the readers of this article can take some of these experts to form their own judgment.

The author Chuck Allison has rich experience in programming, and has many years of history using C . And I am also a fanatic fan of C . Therefore, I think this article should be very good for you - C developer - the appetite.

The quality of Java tourism depends on the guidance of this tour. We are lucky, Chuck Allison is an experienced wizard, regardless of C or for Java.

-------------------------------------------------- ------------------------------

Say again to you. "Import Java. *" Welcome to C / C programmers - to learn Java. In your previous case, you may have a lot of reasons to do this. Java is far more than just a mechanism to enhance the web page. It is a wide range of object-oriented programming languages, which is built on much demand for software engineering industries. When you start learning Java, you will become a better C / C programmer because Java's syntax is very similar to C . And, if you are a C programmer who likes the principle of object-oriented programming, you will feel that it is back to your home. Just like a lot of C programmers choose C , many C programmers are looking for a way naturally to Java.

I will analyze all the features of Java: language, libraries, and ideas, but from the perspective of C / C . This means that I am not a blind believers of Java (may be a fanatic enthusiast, but not a blind believer :-)). In fact, I have the mastery of C and hobbies might to make me biased toward another direction. This also means that I can do some bad things: I will compare these two languages ​​and try to explain some useful things. In this article, I only provide a probably observation of Java, and left the specific teaching in the future. But first, I let you see a little background.

Some words about "scam"

In 1984 with Apple Macintosh, I first heard the term "object-oriented" term. When I asked "What is the meaning of object-oriented", they said that they have a word about the graphic object on the screen. This is a myth: the graphical user interface makes something object-oriented.

In the mid-1980s, "object-oriented" term is abused, so that the only way to pierce this scam is to find a non-observed programming language and use it. When I found LISP in 1987, I began to understand the object-oriented ideas and have the first hand of the "language on packages, inheritance and polymorphism".

Later that year, I participated in the live sales demonstration of a relational database management system (RELATIONAL DBMS). I have been very proficient in their products, and I am "experts" in our company. I heard such a saying during the process of the presentation: " is object-oriented." I ran to the front question: "You said is object-oriented, what does this mean? ? "Then I didn't say anything in the entire demonstration. Some subsequent discussions show that is not object-oriented at all - it is just what they often say: scam. You may know that the trendy vocabulary in the 1970s is "structured design", which eventually obtained a good application in the industrial in the 1980s; the scam of the 1980s is "object-oriented", and it is orthodox today. So what is the scam today? Mode, I guess; and, yes, Java.

Foundation vs. scam

The reputation of C is so explosive growth without any sales, this is a fun thing. In 1982, C designer Bjarn Stroustrup faced a difficult choice: the support of users "c with classes" (later named C ) to swallow him more than half of the time, but there is not enough user to let him There is a fund for a formal agency. For this situation, Bjarne said:

"I see only two ways to get out of the predicament:

1. Stop support C with classes. 2. Develop a better new language to have enough users to get enough funds to support and develop agency. At that time I estimate at least 5,000 industrial users.

The third choice - increases the number of users through sales (scam) - never appeared in my mind [1]. "

In 1991, the number of C users had reached 400,000, and this number was doubled every 7.5 months [2]. (Little mathematical knowledge tells me: If this growth rate has been maintained, everyone on the earth will be C developers in 2000 - a truly terrible Y2K problem!)

Because there is no planned sales plan, and because C is such a "complex language" (maybe we have heard this statement), we can be quite grasped: C only rely on its technical advantages to become object-oriented Leader of development tools. Just like the previous C language, C starts from the foundation, which meets the enthusiasm of developers, which constantly enhances your ability to meet more needs.

However, the story of C rapid growth is not similar to Java's story.

Java people

At Bruce Eckel - the famous author and professor - he gave each participant a T-shirt. When you put it on it, there is a pattern on your left chest: an original person is rushing for life. Bruce called this person "Java people" because he represents you and me and most of the practitioners in most modern computer industries - we are trying to keep up with Java's wave. Browse a Java library slightly, you will see the speed of this wave of waves. When the Sun Microsystems (Sun) launched a JDK (Java Development Kit) version 1.0 in mid-1995, it has 212 classes and interfaces. There were 504 version 1.1 in 1997. There are now 1592 classes and interfaces (and 13635 methods) in the 1.2 version of the JDK library, and it is still in the Beta test phase! Moreover, it is still not enough to make our brain and fingers, each version of Java, etc. It will oppose many of the previous types and methods - we must also forget them. US companies have a slight conservative tradition in the application of new technologies. The three big companies I have worked only first using the programming language defined by ANSI standards. This is why I still use Fortran in my first job in 1978. Until 1986, I secretly introduced the C language in my work. The standardization of C language - C language was completed several years later, but it already had a history of 16 years, and very stable. I first used C in my work in work in 1994 - in the fourth year after the invention after the invention, the 5th year of standardization. In general, IT companies use only those "get verification technology."

So why almost all major software vendors don't take into account the instability of Java language, and provide Java tools to keep up with it? Why can Java have such an impact on so many big companies that usually have a small smash? Relevant events also include: I am teaching Java knowledge to a very conservative company in this spring so they can use Java to override their domestic accounting systems. Why is that?

Of course, the answer is simple: Internet. When the access Internet has become a family daily, Java is coming. "Free release" is of course not wrong, but it is the Internet to make "free release" practical. The ability to add Applet is unimagined. Applets make static pages active, which seducts countless beauty circles from our CFOs.

So, scam - hey, I mean, sales - is part of the answer. In 1996 I met with Bjarn Stroustrup, I asked him what is the idea of ​​"Java Revolution". He answered me:

"If I don't have a constraint of C language, I will definitely not design a language such as Java. However, Sun can get so much beauty, which makes me surprised. This is a lesson that will not be forgotten. And this will be a single programmer, a small company and the college's bad. If people insist on comparing C and Java - looks like they are doing this - I suggest them to see D & E (The Design and Evolution of C ) See why C is now like this, and considering the effect of these two languages ​​in design standards. The difference between C and Java is not limited to the surface, and it is impossible to have all the advantages. [3]

Where is beef?

Marketing may attract our attention, but it cannot guarantee continuous success. Although the Java revolution continues, now JavaScript and GIF animations have done most of our previous use of Applet. Java has been successful, that is because it has its own value behind the scam. Java is attractive to programmers because it is a clean, designed software development tool, and learning it is not "too difficult". Java is attractive to develop investors because it emphasizes objects and packages, because it's virtual machine system, because it supports controllable, cost-effective, (usually) security applications, issuing a way. In addition to operating system manufacturers, everyone likes "Write once, run everywhere" concept. Moreover, Java's unparalleled support from network programming allows it to win in the field of e-commerce. Let us face this fact: Java is cool! However, don't put your C compiler on this. Java cannot do everything. This is why it supports JNI (Java Insufficient Interface). C is still a winner (at least now). Moreover, although Sun has released embedded Java specifications, you can still be sure: Java replaces C as an embedded programming mixed language still takes a while.

In addition, Java is not easy to learn as you think. On the fourth day of the teaching of a company, a considerable number of students said with tired expression: "Man, this is not as easy as I think!" (In my memory, That day we were talking about object clones, reflections, and I / O.) I think Larry O'brien made the best summary, he said: "Say 'Java is more simple than C simple', just like saying" 2000 compared to Everest Short 'the same. "Java is complex, but complex ways are different from C . Is it really simple, depending on you and your needs. Solving practical problems will never be easy, no matter what tool you use.

What is missing?

Java comes from Oak, a language that peels with C for the language of the embedded system. Although you now get Java than OAK (and no longer suitable for embedded applications), Java does not support the following features of C :

 Explicit pointer and reference  Delete operator  Analysis system  Default parameters  Template  Global function and data  Local static data  Macro  Macro  Multi-inheritance  Declaration and definition separation

The first two entries appear to be a big selling point of Java. The pointer is one of the most difficult concepts, and the "arrival pointer" is the common desire of C / C from the industry. However, you still have to understand the basis of "indirect use", otherwise you can't use Java objects (then you can't use Java). All Java objects are dynamic and exist in the heap space. You use the New operator to create a Java object.

Foo f = new foo ();

f is actually still a pointer, pointing to the real foo object in the heap space, but you can only use the F as a tool for access objects; In order to make things easier to our C / C programmers, we need to represent f with a term "pointer" or "reference". "Handle" looks a nice term. Then, in order to access a class member, you can use a normal member selection operator on your handle, such as F.x. If you pass an object as a parameter to a function, it is actually a copy of the handle is passed. Like C, all parameters are passed by value, so Java has no quotation with C reference. This means that you can't write a function to effectively exchange its two parameters, but the function can modify the data member of the parameter object. For example, suppose I define the Foo class, it has an INT type member x:

Class foo {public int x;}

The following SWAP functions will exchange X member values ​​of its parameter objects:

Public Static Voidswap (FOO F1, FOO F2) {INT TEMP = f1.x; f1.x = f2.x; f2.x = Temp;

Such behavior and the following C functions are essentially the same:

Voidswap (struct foo * f1, struct foo * f2) {int temp = f1-> x; f1-> x = f2-> x; f2-> x = TEMP;}

Now look at the second item of this list above. what? Have you not DELETE operator? How irresponsible! No, not. The programmer does not need to return the used memory to the heap space, because Java has a garbage collection mechanism, which will automatically recover all storage space that is not referenced. The good news is: You don't have to worry about memory leakage. The bad news is: You won't know when the garbage collection will run, and which object will be reclaimed. This makes you impossible to control the recycling of resources through garbage collection, which is one of the reasons why Java has no parato. If you want to release some resources at a specific point in time, you must explicitly release.

have what?

We told so many Java nothing. It has a strong support for classes and objects, as well as a characteristic library - this library can meet any programming tasks you can imagine. C programmers will find the following important items when learning Java:

 Everything belongs to a certain class. (Remember, there is no overall thing.)  You don't have to worry about memory leakage.  Characters are encoded in Unicode.  Exception is not optional. (Do a good job in processing exceptions.)  Library supports threads and network programming.  Built-in (basic) types are the same on all platforms.

In order to illustrate the first point, I have given a complete "exchange" program:

// swap.javaclass foo {public int x;}

Public class swap {public static void swap (f2.x; f1.x = f1.x; f1.x = f2.x; f2.x = temp;} public static void main (string [] args) {foo F1 = new foo (); f1.x = 1; foo f2 = new foo (); f2.x = 2; SWAP (F1, F2); System.out.Println (F1.x); // 2 System. Out.println (f2.x); // 1}} Please note that there is no thing similar to the delete operator.

As mentioned in this list, the Java program is completely composed of classes, each class contains a field (data member) and / or method (member function). This simple organization, plus the convenient library packaging mechanism I will introduce in the later articles, providing a practical specification for the management of large-scale applications.

In order to compile this program using JDK, I do the following in the command line:

C:> Javac Swap.java

The file extension of the Java source code is .java. There must be at least one public class in each file. The name of this class must be the same as the file name (excluding .java). The javac command generates a .class file for each selected .java file. The .class file contains a range code, the byte code will be executed by your JVM (Java virtual machine). The java command will load the JVM and indicate that the JVM starts to execute .class file - first translate the byte code in the main method, just like this

C:> Java Swap21

SWAP classes do not contain data, and all the methods are static, which means they can be executed without a swap object (just like C ). In fact, the existence of SWAP classes is only a place to place SWAP methods and main methods. System.out is the middle price of Java in stdout, and the PrintLn method will convert the parameters into a string and then output.

A truly working Java program typically includes multiple .class files. When a class needs to call other class services, the JVM searches for the latter corresponding .class file. Because each function in Java is a method of a class, there is no conflict in a program. In fact, usually we will put a main method in each public class for the purpose of the test. There is no unique program-level main method means that the main method will not return what results to the operating system, but Java provides a special way for this purpose, similar to the EXIT function of C.

It is the portability of the virtual machine system to Java "Write once, run everywhere". As long as one platform supports JVM, it can run the bytecode from any platform. However, because JVM is an interpreter, in comparison with pure compilation languages, it also shows speed disadvantages. For partial compensation, JDK now uses the "JIT) compiler, which is compiled into an inherent code when the class is first loaded. This code only needs to be explained once, not every time being called. You can also use other commercial JIT compilers and purely code compilers.

to sum up

As you can see, Java's syntax is almost exactly the same as C - the statement ends with the semicolon, the composite statement within the braces. But please note that the definition of the class is not ending in the semicolon. In addition, there is no Struct key in Java (this will not make anyone worry). You must also define a separate specified access level separately for each field or method, which is forced us to make more beat keyboards. Perhaps the biggest cultural conflict is format I / O. For example, Java does not equivalent to Scanf or Printf, which makes Java's command line I / O a pure blow to potential C / C movies. However, this command line I / O becomes more and less important in the world of this graphical user interface today, and I will show some of the very complete features in Java I / O packages in future articles. Is this all all? No, this is just the beginning of my series of articles. My purpose in the first part is to make some paving this series and hang your appetite. In the subsequent section, I will start from the basic data type, operator, and control structure (simple material), to see in-depth observations for Java language. If you are interested in this topic, you'd better browse the "editing angle" of the CUJ website (www.cuj.com), there is a "basic reading list". I recommend the following two best books:

1. Arnold & gosling. The Java Programming Language, Second Edition (Addison-Wesley, 1998). An ambiguous, concise language introduced by the inventors of Java. 2. Bruce Eckel. Thinking in Java (Prentice-Hall, 1998). The most friendly and comprehensive Java books you can buy on the market.

Start enjoying your Java journey.

bibliography

[1] Bjarne Stroustrup. The Design and Evolution of C (Addison-Wesley, 1994), P. 63. [2] Bjarne Stroustrup. The Design and Evolution Of C , P. 163-164. [3] Chuck Allison. " C : The Making of A Standard, "C / C Users Journal, October 1996.

转载请注明原文地址:https://www.9cbs.com/read-494.html

New Post(0)