Java programming language

Source: Internet
Author: User

1. Overview.
Sun Microsystems was officially launched in 1995. (Java seems to be java. Coffee is good in this place,
Programmers like coffee, Golden State comments) with security, cross-platform, object-oriented, simple, applicable to network, etc.
Features (especially cross-platform, can be said to be great, think about windows programs can be directly obtained without modification
On a UNIX system? I guess not. Haha, but java can. Note .)
Working principle: the java source program needs to be compiled into a. class file (bytecode file) through the compiler, and then the interpreter
OK.
Java programs include java application and java applet.
Example:
Memory condition output in the java application example.
Import java. util)

Public class jinzhoutest {(this class is named. Jinzhou annotation)
Public static void main (String args []) {(fixed format, Golden State comment)
System. out. println (new Date (); (output Date, Golden State comment)
Properties p = System. getProperties (); (get object, defined as p, Golden State annotation)
P. list (System. out); (output p, Golden State comment)
System. out. println ("jinzhou Memory Usage:"); (output that string, Golden State Annotation
Runtime rt = Runtime. getRuntime (); (obtain the system's Runtime object, Golden State annotation)
System. out. println ("Total Memory ="
+ Rt. totalMemory ()
+ "Free Memory ="
+ Rt. freeMemory (); (output memory, Golden State comment)
}
}

Java applet example: displays a sentence.
Import java. awt. Graphics; (it indicates that Graphics-like objects need to be used. The following sentence is similar. Jinzhou notes)
Import java. applet. Applet;

Public class Hellojinzhou extends Applet {
Public String s;
Public void init () {(format, Golden State comment)
S = new String ("Welcome to E.S. T ");
(Generate a new string, Golden State annotation)
}
Public void paint (Graphics g ){
G. drawString (s, 25, 25); (output at coordinates of 25.25, Golden State annotation)
}
}
(See the difference. The latter does not use the main function. This indicates that the function needs to be embedded for execution. Generally, it is embedded in a browser and Golden State annotations .)
Previous example: Embedding Method

 

 

A complete java source program should include the following parts:
Package statement; // This part contains at most one sentence, which must be placed in the first sentence of the source program.
Import Statement;/* This part may have several or no import statements, which must be placed in all
Before class definition */
Public classDefinition; // public class definition section. At most, there is only one public class definition.
// The java language requires that the file name of the java source program must be exactly the same as that of the public class.
ClassDefinition; // class definition, which can have 0 or more class definitions
InterfaceDefinition; // interface definition, which can be zero or multiple
Golden State skipped the establishment of the java development environment.

II. Basic Java knowledge
Jinzhou is as follows,
Data Type Division
Java data types include simple and composite types:
Simple data types include:
Integer: byte, short, int, long
Floating: float, double
Character type (Textual): char
Boolean Type (Logical): boolean

Composite data types include:
Class
Interface
Array
Constant: implemented with the reserved word final
Final typeSpecifier varName = value [, varName [= value]…];
For example, final int NUM = 100;

Variable: a basic storage unit in a java program. Its definition includes the variable name, variable type, and scope.
The definition format is as follows:
TypeSpecifier varName [= value [, varName [= value]…];
For example, int count; char c =;
Arithmetic Operators: +,-, *,/, %, ++ ,――
Relational operators:>, <, >=, <=, = ,! =
Boolean logical operator :!, &, |
Operator priority
Priority Operator
1. [] ()
2 + + --! ~ Instanceof
3 new (type)
4 */%
5 +-
6 >>>>>< <
7> <>=< =
8 =! =
9 &
10 ^
11 |
12 &&
13 |
14? :
15 = + =-= * =/= % = ^ =
16 <=>>>>>>>=
Conditional statement if-else
Multi-branch statement switch (these are mostly the same as the format used in other languages, not to mention, Golden State comment)
Loop statement while statement do-while statement for statement
Jump statement break statement continue statement return Statement return
Exception Handling statement: try-catch-finally, throw
Array, string processing gold. The State is skipped. This is a simple introduction.

3. Object-oriented features in Java
Basic Features 1. encapsulation 2. Inheritance 3. Polymorphism
Class: includes two parts: class declaration and class body.
This is troublesome to say. You only need to remember that this language is intended for objects rather than processes. So this language
It is easy to use, that is to say, you only consider how to operate a computer, instead of thinking about why this operation is implemented.
For example, if I write this object, the saved file will be saved, instead of calling any functions to save the object.

4. Java Exception Handling and I/O Stream
Exceptions: for example, Jinzhou is going to Zhengzhou, and there is a car accident on the road. That is to say, the program is faulty and cannot be executed.
I/O input and output.
Example:
Class ExceptionDemo2 {
Public static void main (String args []) {(format, Golden State comment)
Int a = 0; (let a = 0, Golden State comment)
System. out. println (5/a); (input 5/a, Golden State comments)
}
}
Because the divisor cannot be 0, this is an exception. Java exception classes can be divided into two types of errors. Exception

Two exception handling mechanisms: 1. Capture exceptions: When the Java runtime system gets an exception object, it will
Call the stack layer-by-layer backtracking to find the code that handles this exception. After finding a method that can handle such exceptions
The system submits the current exception object to this method for processing. This process is called a catch exception. This is a positive exception
Processing Mechanism. If the system cannot find a method to capture exceptions during Java runtime, the runtime system will terminate and the corresponding Java program will exit.
Catch exceptions are implemented using try-catch-finally statements.

2. Declare to discard an exception: if a method does not know how to handle the exception, you can declare to discard the exception (throws) When declaring a method.
This is a negative exception handling mechanism. The declared discard exception is specified in the throws clause in a method declaration.

I/O Stream Layer 1. byte stream 2. Object Stream 3. Other
In I/O processing, the most common operation is file operations,
File Description Example: list all. java files in Directory D: \ ex
Import java. io. *; (io processing, Golden State annotation)
Public class FileFilterTest {
Public static void main (String args []) {(name in the preceding format. Jinzhou notes)
File dir = new File ("d: // ex"); (use the directory in the File object, Golden State annotation)
Filter filter = new Filter ("java"); (generate a Filter named java, Golden State annotation)
System. out. println ("list java files in directory" + dir );
String files [] = dir. list (filter); (enter the list Directory, Golden State comment)
For (int I = 0; I File f = new File (dir, files); (assign the object to the directory dir, Golden State comment)
If (f. isFile () (output if found, Golden State comment)
System. out. println ("file" + f );
Else
System. out. println ("sub directory" + f); (print the directory name if it is a directory)
}
}
}
Class Filter implements FilenameFilter {
String extent;
Filter (String extent ){
This. extent = extent;
}
Public boolean accept (File dir, String name ){
Return name. endsWith ("." + extent); (returns the file suffix, Golden State comment)
}
}

File or directory generation
Public File (String path);/* If the path actually exists, the File object
/* Table

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.