Java Fundamentals---Java language overview

Source: Internet
Author: User
Tags class definition

One, two kinds of computer programming model

1, the process-oriented model---has the characteristics of linear execution, the code is considered to be used for data.

2. The object-oriented model---organizes programs around its data (that is, objects) and interfaces defined for that data, and is actually accessed with data-control code

Second, the object-oriented substantive elements---abstraction

People are dealing with complexity through abstraction, and abstract management methods use hierarchical classification, which allows you to break down complex systems into more manageable chunks based on physical meaning.

Iii. three principles of object-oriented

Encapsulation---is a programming mechanism that binds the code and its processed data together to ensure that the program and data are not externally disturbed and are not misused. You can think of encapsulation as a black box, which prevents data from being arbitrarily accessed by externally defined code in internal code and data. Access to code and data within the black box is tightly controlled through an appropriately defined interface.

Inheriting---is the process by which an object obtains the properties of another object, and inheritance is important because it supports the concept of classification by layer.

Polymorphism---an attribute that allows an interface to be used by multiple homogeneous actions. This concept is often said to be "an interface, a variety of methods"; This means that a common interface can be designed for a set of related actions. Polymorphism allows the same interface to be used by multiple actions of the same class, which reduces the complexity of the program.

Four, the first simple Java program

1. Type the program


Call the This file "Example.java". */
Class Test{public static void Main (string[] args) {System.out.println ("This was a simple Java program!");}}

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

2. Compile the program

To compile the sample program Example, run the compiler program Javac and specify the source program file name on the command line, as shown in the following format: C:\>javac Example.java

The compiler Javac produces a file named Example.class that contains the program's bytecode. As discussed earlier, Java bytecode contains the script that the Java interpreter will execute. Therefore, the output of Javac is not code that can be run directly.

To actually run the program, you must use the Java interpreter called Java. The practice is to enter the class name example as a command-line parameter, as shown in the following format:

C:\>java Example

To run the program, the output will be as follows: This was a simple Java program.

When the Java source file is compiled, each individual class is put into its own output file and named after the class name with a ". Class" extension. This is why the Java source program file must have the same name as the one it contains-the source program file will be the same as the ". Class" file. Running the Java interpreter is actually specifying the name of the class you want the interpreter to run, and it will automatically search for files that contain that name with a. class extension.

3. Detailed discussion of the first sample program

3, 1---/* This was a simple Java program. Call the This file "Example.java". */

This is a comment. Like most programming languages, Java allows you to annotate in a source file, and the content compiler for comments is automatically ignored. Comments are intended to explain or interpret the operation of the program to anyone reading the source code program

Java supports 3 types of annotations. The comment at the top of the sample program is called a multiline comment (multiline comment). This type of comment starts with "/*" and ends with "*/". Any content between these two annotations will be ignored by the compiler. As the multi-line comment name shows, a multiline comment can contain several lines of text.

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

The line declares a new class using the keyword class, example is the class name identifier, and the entire class definition (including all its members) will be between a pair of curly braces ({}).

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

The line starts at the main () method. As noted in the previous note, this is the first line that the program will start executing. All Java applications begin execution by calling main ().

3, 4 The following line of code is shown below. Notice that it appears inside main (). System.out.println ("This was a simple Java program.");

The line is printed on the screen with the string "This was a simple Java program." With an empty row behind the output result. The output is actually implemented by the built-in method println (). In this example, println () displays the string passed to it. You will see that the println () method can also be used to display other types of information. This line of code starts at System.out, and it's too early to elaborate on it, which involves a lot of complex content. Simply put, system is a predefined class of accessible systems, and out is the output stream that is connected to the console.

3.5 The first "}" in the program ends with main (), and the Last "}" ends the definition of class example.

V. Examples of the second program

  

/*here is another short example. Call this file "Example2.java". */class Example2{public static void Main (string[] args) {int num;//declares a variable named Numnum = 100;/ /Assign 100 to num this 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

The row declares an integer variable named num. Like most other languages, it is important to declare variables before using them in Java.

The following is the general form of a declared variable: type var-name; Here, type represents the type of the variable being declared, and Var-name is the name of the variable you want to declare. If you want to declare multiple variables that belong to the same type, simply separate the variable names with commas.

2. In the program, the following line of code assigns 100 to the variable num.

num = 100; This assigns num the value 100

In Java, an assignment symbol is an equal sign.

3, the following line of the program in the output variable value, first output the string "This is num:".

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

In this statement, the plus sign "+" before the variable num means that the value of NUM is concatenated with the string in front of it, and then the contents of the resulting string are output (in fact, the variable num is converted to a string by its value and then the value is then concatenated with the string before the plus sign.)

4, the next statement will be the variable num by 2 after the result is re-assigned to the variable num. Like most other languages, Java uses the "*" notation to represent multiplication operations. After executing this line of statements, the value of the variable num becomes 200. The next two lines of code for this program are: Chapter 2nd Java Language Overview System.out.print ("The value of num * 2 is"); SYSTEM.OUT.PRINTLN (num); There are a couple of new things in these two lines. First, the built-in method print () is used to display the string "The value of num * 2 is". The string does not wrap, which means that if a second output is generated, it will start output in the same row. The method print () is similar to Method println () except that it does not output a new line (that is, newline) after each call. Second, when calling println (), note that the variable num can be used by itself. The method print () and method println () can be used to output values of any built-in type of java.

VI. Basic Vocabulary

1. Blank delimiter (whitespace) Java is a form of free language. This means that you do not need to follow any special indentation writing specifications. For example, all the code for an example program can be on one line, and you can enter the program code as you like, provided that you must leave at least one white space delimiter between the tags already described by the operator or delimiter. In Java, the whitespace delimiter can be a space, tab, or line break.

2. Identifier (identifiers) identifier is the name assigned to a class, method, or variable. An identifier can be an arbitrary combination of uppercase and lowercase letters, numbers, underscores, dollar signs, but cannot start with a number. Otherwise, it is easily confused with numbers and constants. Again, Java is case-sensitive, and value and value are two different identifiers. Here are some valid identifiers: Avgtemp count A4 $test this_is_ok The following are some invalid variable names: 2count high-temp Not/ok

3, constants (literal) in Java, constants are represented by literal. For example, here are some constants: 98.6 ' X ' "This is a test" from left to right, the first represents an integer, the second is a floating-point value, the third is a character constant, and finally a string. Constants can be used anywhere it is allowed, representing a value of the owning type.

4. Note (comments) Java defines the types of 3 annotations. There are 2 types of annotations you already know: single-line comments and multiline comments. The 3rd type of annotation is called a document comment (documentation comment). This type of comment notes your program in the form of an HTML file. The document comment begins with "/**" and ends with "*/".   The documentation comments are explained in Appendix A. 5, delimiter (separators) in Java, some characters are used as delimiters, the most common delimiter is a semicolon (;), used to separate statements.

  

6. The Java keyword currently defines 48 reserved keywords in the Java language (see table 2-1). These keywords, together with the syntax of operators and delimiters, form the definition of the Java language. These keywords cannot be used for variable names, class names, or method names. The keyword const and goto are reserved but not used. In the early days of the Java language, several other keywords were reserved for later use. However, the current Java-defined keywords are shown in table 2-1. In addition to the keywords above, Java also has the following reserved words: True,false,null. These words are Java-defined values. You can't use these words as variable names, class names, and so on.

    

7, Java class Library in this chapter of the sample program is used in Java two built-in methods: println () and print (). As mentioned earlier, these methods are members of the system class that have been predefined by Java and are automatically included in your program. The Java environment relies on several built-in class libraries that contain a number of built-in methods to provide support for 27-string processing, networking, graphics, such as input/output (I/O), character 2nd, Java language Overview. The standard class also provides support for 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 of the features of Java. Undoubtedly, to be a Java programmer, part of the job is to learn to use standard Java classes. In the 1th part of this book, it is necessary to use a variety of standard library classes and methods

Java Fundamentals---Java language overview

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.