Java basics-java language overview, java-java Overview

Source: Internet
Author: User
Tags java keywords

Java basics-java language overview, java-java Overview

I. Two types of computer programming

1. process-oriented model-linear execution is characteristic, and code is considered to act on data.

2. object-oriented model-organizes programs around its data (that is, objects) and the interfaces defined for this data. In fact, it uses data to control code access.

Ii. Substantive elements of object orientation-Abstraction

People process complexity through abstraction, and the abstract management method uses hierarchical classification, which allows you to break down complex systems into more small pieces that are easy to process based on physical meaning.

3. Three object-oriented principles

Encapsulation is a programming mechanism that binds code and the data it processes together. This mechanism ensures that programs and data are not subject to external interference and are not misused. You can think of encapsulation as a black box, which can prevent data from being freely accessed by external code. Strictly control the access to code and data in the black box through an appropriate defined interface.

Inheritance is the process in which an object obtains the attributes of another object. inheritance is very important because it supports the concept of layer-based classification.

Polymorphism-a feature that allows an interface to be used by multiple similar actions. This concept is often referred to as "one interface, multiple methods", which means that a common interface can be designed for a group of related actions. Polymorphism allows the same interface to be used by multiple actions of the same class, reducing the complexity of the program.

4. The first simple java program

1. Type A Program

/* This is a simple Java program. 
Call this file "Example.java". */
class Test{public static void main(String[] args) {System.out.println("This is a simple java program!");}}

The first important thing about java is to know the name of the source file, because in java, a source file is called a compilation unit, it is a text file that contains one or more class definitions. The java compiler requires that the source file use the. java file extension.

2. Compile the program

To compile the sample program Example, you must run the compiler program javac and specify the source program file name on the command line. The format is as follows: C: \> javac Example. java

The javac compiler generates a file named Example. class, which contains the bytecode of the program. As discussed earlier, the Java bytecode contains the instruction code to be executed by the Java interpreter. Therefore, javac output is not code that can be directly run.

To really run this program, you must use a java interpreter named Java. The specific method is to input the class name Example as a command line parameter in the following format:

C: \> java Example

Run This program and output the following content: This is a simple Java program.

After the java source file is compiled, each individual class is put into its own output file and named after the class name and the ". class" extension. This is why the Java source program file must have the same name as the class contained in it-the source program file will be the same as the ". class" file. Running the Java interpreter actually specifies the name of the class you want the interpreter to run. It will automatically search for files containing the name and with the. class extension.

3. discuss in detail the first example Program

3. 1 ---/* This is a simple Java program. Call this file "Example. java ".*/

This is a comment. Like most programming languages, java allows you to annotate in the source file, and the content compiler automatically ignores the annotation. Annotations are used to explain or explain the operations of the program to anyone who reads the source code program.

Java supports three types of annotations. The comments at the top of the sample program are called multiline comments ). This type of annotation starts with "/*" and ends with "*/". Any content between the two annotators will be ignored by the compiler. As shown in the "multi-line comment" name, a multi-line comment can contain several lines of text.

3, 2 --- the first line of code class Example {

This row uses the keyword class to declare a new class. Example is the class name identifier. The entire class definition (including all its members) is located between a pair of curly braces,

3. 3. The next line of code public static void main (String args []) {

This row starts with the main () method. As mentioned above, this is the first line of execution that the program will start to execute. All Java applications start execution by calling main.

3. 4. The following code lines are shown. Note that it appears in main. System. out. println ("This is a simple Java program .");

The line outputs the string "This is a simple Java program." on the screen, and the output result is followed by a blank line. The output is actually implemented by the built-in method println. In this example, println () shows the string passed to it. You will see that the println () method can also be used to display information of other types. This line of code started with System. out. It is too early to elaborate on it and involves a lot of complicated content. Simply put, System is a predefined class that can access the System, and out is the output stream connected to the console.

3.5 The first "}" in the program ends main (), and the last "}" End Class Example definition.

V. Example of the second program

  

/* Here is another short example. call this file "Example2.java ". */class Example2 {public static void main (String [] args) {int num; // declare a variable named numnum = 100; // assign 100 to the num variable System. out. println ("This is num:" + num); num = num * 2; System. out. print ("The value of num * 2 is"); System. out. println (num );}}

1. The first line of code: int num; // this declares a variable called num

This row declares an integer variable named num. Like most other languages, variables must be declared first in Java before they can be used.

The following is the general form of declaring a variable: type var-name; here, type indicates the type of the variable to be declared, var-name is the name of the variable to be declared. If you want to declare multiple variables of the same type, you only need to use commas to separate them.

2. In the program, the following code will assign the variable num to 100.

Num = 100; // this assigns num the value 100

In Java, the value assignment is an equal sign.

3. The following program outputs the string "This is num:" Before outputting the variable value :".

System. out. println ("This is num:" + num );

In this statement, the plus sign "+" before the variable num is used to connect the value of num with the string before it, and then output the content of the result string (in fact, the variable num is assigned a value first, then converted into a string, and connected to the string before the plus sign.

4. In the following statement, the result of the variable num multiplied by 2 is assigned to the variable num. Like most other languages, Java uses the "*" symbol to represent multiplication. After this line of statements is executed, the value of the variable num is 200. The next two lines of code in this program are: Chapter 1 Java language Overview 21 System. out. print ("The value of num * 2 is"); System. out. println (num); there are several new contents in the two rows. First, The built-in print () method is used to display The string "The value of num * 2 is ". This string is not followed by a line break, which means that if the second output is generated, it will start to output in the same line. The print () method is similar to the println () method, but it does not output a new line (line feed) after each call ). When calling println (), note that the variable num can be used by itself. Both method print () and method println () can be used to output any built-in type value of Java.

6. basic vocabulary

1. whitespace Java is a free-form language. This means that you do not need to follow any special indentation writing rules. For example, all the code of the example program can be on one line, or you can enter the program code as you like, the premise is that there must be at least one blank separator between the tags described by the operator or delimiter. In Java, the blank separator can be a space, a Tab hop key, or a line break.

2. identifiers are the names assigned to Classes, methods, or variables. An identifier can be a combination of uppercase and lowercase letters, numbers, underscores, and dollar signs in any order, but cannot start with a number. Otherwise, it is easy to mix with numbers and constants. Again, Java is case sensitive, and VALUE and Value are two different identifiers. The following are some valid identifiers: AvgTemp count a4 $ test this_is_ OK. Below are some invalid variable names: 2 count high-temp Not/OK

3. in Java, constants are represented by literal. For example, the following are some constants: 100 98.6 'x' "This is a test" from left to right. The first one represents an integer and the second is a floating point value, the third is a character constant, and the last is a string. A constant can be used by the allowed types anywhere, representing a value of the type.

4. comments Java defines three types of annotations. You already know two types of Annotations: single-line and multi-line annotations. The 3rd annotation types are called documentation comment ). This type of annotation is used as an HTML file to annotate your program. The document comments start with "/** and end. The document notes are explained in Appendix. 5. In Java, some characters are used as separators. The most common Delimiter is semicolon (;), which is used to separate sentences.

  

 

6. Java keywords currently, the Java language defines a total of 48 reserved keywords (see Table 2-1 ). These keywords, together with the operator and delimiter syntax, constitute the definition of the Java language. These keywords cannot be used for variable, class, or method names. Keywords const and goto are retained but not used. In the early days of Java, several other keywords were reserved for future use. However, currently, the keywords defined in Java are shown in Table 2-1. In addition to the above keywords, Java also has the following reserved words: true, false, null. These words are Java-defined values. You cannot use these words as variable names, class names, and so on.

    

 

7. the Java class library uses two built-in Java methods in the sample programs in this chapter: println () and print (). As mentioned above, these methods are a member of the System class, which has been predefined by Java and automatically included in your program. The Java environment relies on several built-in class libraries that contain many built-in methods to provide), character 2nd chapter Java language overview 27 string processing, network, graphics support. Standard classes also support window output. Therefore, as a whole, Java is a combination of the Java language itself and its standard classes. You will see that the Java class library provides many Java functions. Undoubtedly, to become a Java programmer, part of the work is to learn to use standard Java classes. In part 1 of this book, various types of standard library libraries and methods are required.

 

Related Article

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.