Chapter II, the first chapter of the Java programming is still relatively smooth, but the students have almost all have difficulties in installing the Java environment, there is nothing to install, there is no environmental variables, there is JBuilder can't run. , Not enough, this is a normal phenomenon. I have a lot of effort when I first configure the Java environment. Today, it's all basic knowledge, more zero, including annotations, basic data types, strings, etc., look at it little. From the comment, Java broke the annotation of people's negligible annotations, in addition to traditional single-line comments and multi-line comments, Java also supported document comments. Document Note For programmers who do not write documents, in software companies, the programmer's task is responsible for writing the corresponding document, one is the document description described by the program interface, which is usually used What describes what you describe your program is included. Obviously, when the programmer has written these documents after writing these codes, it is quite boring, and the document comment allows you to complete these documents while writing the program. Do you feel that it is impossible? Take a look at the following code: / ** * An example of a demonstration document *
title: Document Note Demonstration p> *
Description: An example of a demonstration document notes P> *
Copyright: Copyright (C) 2005 p> * @Author underwind * @version 1.0 * / public class person {/ ** * name; / ** * Configure a Person object by incoming name * @Param name string Name * / public person (String name) {this.name = name;} / ** * running method, incoming distance, return to run this distance
* If running During the process, I accidentally fall, turn back to the fall. Abnormal * @Param meters int requires running distance (unit: m) * @Throws Fallexception falls unusual * @Return Double running time (unit: second) * / public Double Run (int Meters) THROWS FALLEXCEPTION {RETURN Meters / 10;} Of course, this class is just a demo, is it to understand the writing of document comments? Generally we write a document comment in the beginning of the class, indicating what this class is to do, who is written, and then write a document annotation before each member of the class (some people think that private members do not need Write, I don't feel it), explaining what this member is used. Look at the generated document:
I suggest written a document comment in JBuilder to write a method's annotation as an example, first write a declaration of the method, and then press Enter after the method declared by the method, and JBuilder automatically generates a comment. In the JBuidler X version, you can automatically generate a document on the HTML format next time by selecting Javadoc under the Wizards menu. Of course, you can also use Javadoc.exe manually compiled in the DOS environment, but it is more troublesome (searching the internet to learn Javadoc). Tell, talk about the basic data types in Java, what is the specialty of Byte, Short, long? some! The advantage of data types in Java is the length of the data type in Java is to standardize the length of the data type. For example: C / C , an int type variable takes a number of bytes, which is dependent on the running environment, so the INT type on some machines can represent the number of -32768 ~ 32767, on some machines It may be more, this inconsistency is a major obstacle to the C / C language cross-platform (thinking about why?). Java solves this problem, no matter which machine, Java's data type length is fixed. Note that Java is a strong type of language that is strict for conversion requirements between different types, such as writing int i = 3.3 is wrong, should be written into int i = (int) 3.3. Let's talk about the CHAR type, know that the CHAR can save a character, can it save a Chinese character? The answer is ok, because Java saves characters in the Unicode encoding format, not a traditional ASCII code, Unicode encoding uses two bytes to represent characters, whether English or Chinese (search the Internet, learn Unicode encoding). The students can feel so much, how to make such comments and basic data types! Oh, patience, there are more things. Say the basic type and talk about our most common type of non-basic type: String (string). String is a class in Java. We can easily use the string by defining the String object. Some common string operations are provided in the String class. These are speaking in JavaScript, no more, if you I forgot, turn the book forward. Focus on the comparison of the string, observe the following code: String a = "Hello"; string b = "hello"; string c = new string ("Hello"); string d = new string ("Hello"); System.out.println (a == b); system.out.println (b == c); system.out.println (c == d); system.out.println (a.equals (b)); System.out.println (B.Equals (c)); System.out.Println (C.Equals (D)); guess results? True, False, False, True, True, True. Analyze the reason, to lead a concept: string pool.
What is "string pool", this is the measure used by Java to improve memory utilization: When encountering String a = "Hello", Java will first look for this string in the string pool. If not, create a string hello object, then the variable A points to this address (yes, there is no pointer in Java, but everyone must have the concept of pointer); then encounter STRING B = "Hello", then There is already Hello in the string pool, so directly let the variable B also point to this address, saving the trouble of redistribution. In Java, the operator "==" For two basic types, it is to determine whether the content is the same. For two objects, it is determined whether the address is the same, so A == b returns TRUE. So how do String c = new string ("Hello")? If this is a way of writing, you will not access the string pool, but the space is opened for the variable C, then write the value to the space. So a == c returns false; c == D also returns FALSE. As for String's equals method, because it is not an object's address, but the value of the object, it is not surprising (detailed discussion on the Equals method in Chapter 4). Question: As mentioned earlier, variables A and B points to the same address, then if the statement a = "hi" is executed, the value of the variable B is changed? Answer: No, Java will find Hi in the string pool. If you find it, let the variable a point to this new address. If you don't find it, create a new Hi object, then let the variable a point to it. This operation does not affect the value of the variable b. The content of this lesson is a detail. I don't know if I have a great impact on the program, but I know that it will make the code more perfect. Next is an operator, it is common, and there is a new operator instanceof, which is used to determine whether an object belongs to a class. This operator will be used in the future. It is more convenient to speaking arrays in Java, usually there are two ways: int a [] = new int [10] or int [] a = new int [10], there is no difference between two ways. . But pay attention to a little, if you want to define an object array, such as the following example: Class Person {public string name; public int agent,} public class testArray {public static void main (string [] args) {person Persons [] = New Person [10]; Persons [0] .Name = "Tom";}}