Java beginners (II) -- Java BASICS (I) and java beginners

Source: Internet
Author: User
Tags java keywords

Java beginners (II) -- Java BASICS (I) and java beginners

In this article, we start to learn about Java. First, we need to understand the basic structure of Java programs before learning how to write Java.

I. Basic Structure of Java program

The basic structure of a Java program can be divided into packages, classes, main () primary methods, identifiers, keywords, statements, and annotations, as follows:

1 package hello; // define package 2 3 public class Structure {// create class 4 5 static int num = 1; // define the class member variable 6 7 public static void main (String [] args) {// define the main method 8 9 String str = "http://www.cnblogs.com/adamjwh "; // define the local variable 10 11 System. out. println (num); // output the value of the member variable 12 13 System. out. println (str); // output the local variable value 14 15} 16 17}

Let's analyze each statement one by one. I will not go into details about too many concepts, just the most basic ones.

The first statement "package hello;" defines that the package of the class in the Java program is "hello", "hello" is an identifier, defined by the programmer, and "package" is a keyword. Note: The identifiers and keywords are case sensitive.

The second statement "public class Structure" is used to create a class named Structure. The class name is defined by the programmer. public and class are keywords, the usage of public and static will be mentioned in subsequent chapters.

The third statement "static int num = 1;" defines the member variables of the class. Both static and int are keywords, while num is an identifier defined by the programmer.

The fourth statement "public static void main (String [] args)" is the main method of the class. The Java program is executed from here, except that you can change "String [] args" to "String args []", you cannot change any part of this statement.

The fifth statement "String str =" substring (To put it simply, if you want to create a String, use the String class). str is the name of a local variable, an identifier defined by the programmer. The URL in the quotation marks is the value of the local variable str, and "=" is the value assignment operator.

The sixth statement "System. out. println (num);" is the output Statement, which is a fixed writing method of the output statement. It is case sensitive and outputs The println statement without printing the output.

The seventh statement is also an output statement. Execute the statement to output the str value, that is, http://www.cnblogs.com/adamjwh.

Ii. Identifiers and keywords

Is it complicated to say so much? What are identifiers and keywords. In fact, identifiers can be simply understood as a name, used to identify the effective Character Sequence of the class name, variable name, method name, array name, and file name.

For example, to define a variable I and assign a value of 100, we can write the following code: int I = 100;

This is a typical value assignment statement, where int defines an integer, I is the identifier, named by the programmer, but there are certain rules, simply putIt consists of letters, numbers, underscores, and dollar signs. The first character cannot be a number, case sensitive, and cannot be a keyword or reserved word.

  For example, valid identifiers such as name, user, _ u8080, tc_bvt, and invalid identifiers such as 5 work and 7fix.

The keywords and reserved words mentioned above are a group of words that have been given specific meanings in the Java language and cannot be used as identifiers. For example, the int in the above Code is a keyword, as for the java keywords, I will not describe them much. After all, there are a lot of detailed introductions on the Internet, and we will mainly start with the code.

III. Basic Data Types

After learning about the basic structure of the above Java program, do you have some feelings about Java? Therefore, writing a program requires a lot of data, how is the data in Java classified and written?

There are 8 Data Types in Java, 6 of which are numerical type, and the other two are character type and boolean type, as shown below:

1 public class Type {2 3/* Integer Type */4 byte myByte = 45; // byte Type variable, occupies one byte, value range:-128 ~ 127 5 short myShort = 100; // The short type variable, that is, a short integer, occupies two bytes. The value range is-32768 ~ 32767 6 int myInt = 450; // int type variable, that is, integer, which occupies four bytes. value range:-2147483648 ~ 2147483647 7 long myLong = 45261636L; // long type variable, that is, a long integer, which occupies eight bytes and ranges from-9223372036854775808 ~ 9223372036854775807 8 9/* floating point type */10 float myFloat = 15.621F; // The Single-precision floating point type occupies four bytes and must end with 'f' or 'F ', if you do not add a variable of the double type, the value range is 1.4E-45 ~ 3.4028235E-3811 double myDouble = 15.621; // double Floating Point type, which occupies eight bytes. You can add 'D' or 'D' at the end or not. The value range is 4.9E-324 ~ 1.7976931348623157E-30812 13/* character type */14 char myChar = 'a'; // character type variable, used to store a single character, occupying two bytes, it must be enclosed in single quotes for 15 16/* boolean type */17 boolean myBoolean = true; // boolean type, also known as logical type, has only true and false values, "True" and "false" 18}

When we want to define a variable, first determine the Data Type of the variable, and then select the appropriate type from the top 8 types to use, the definition method is like the above Code, it can be in the form of "[data type] [variable name] = [value]". The variable name must be named automatically to meet the conditions for the above-mentioned identifiers.

There is also a special character in the character type, which starts with the Backslash "\" and is followed by one or more characters. It has a specific meaning and is called an escape character.

Escape characters Description
\ Ddd 1 ~ Three octal characters, for example, \ 456
\ Dxxxx 4-digit hexadecimal characters, for example, \ 0051
\' Single quotes
\\ Backslash character
\ T Vertical tab, move the cursor to the next tab
\ R Enter
\ N Line feed
\ B Return
\ F Form feed

 

 

 

 

 

 

 

 

  

Escape characters are generally used for output. For example, "\ n" returns a line break and "\ t" moves to the next tab. If you want to output single quotes, double quotes, underscores, and other characters, escape characters are also required for output.

Iv. Variables and constants

We have just mentioned variables. Next we will talk about what variables and constants are. During program execution, the value that cannot be changed is called a constant, and the value that can be changed is called a variable. The declaration of variables and constants must use valid identifiers. All variables and constants can be used only after they are declared. The following is an example of declaring a variable:

Int age; // declare the int type variable char c = 'J' // declare the char type variable and assign a value

Since it is called a variable, it can be changed. Now let's try to change the variable:

1 public class Variable {2 3 public static void main (String [] args) {4 int num = 10; // define a Variable num and assign it to the initial value 10 5 System. out. println ("num Initial Value:" + num); // output the current variable num value to 10 6 7 num = 100; // assign 100 to num 8 System. out. println ("num current value:" + num); // output current variable num value 100 9} 10 11}

The above is a piece of test code. We first define a variable named num, then assign it an initial value of 10, and then assign a value of 100 to the variable to see the changes of the two values, the running result is as follows:

  

It can be seen that the value of the variable can be changed during running.

A constant can only be assigned a value once in the entire program. It must be specified by the final keyword, for example, final double PI = 3.1415926;, to define a constant named PI (circumference rate ), if we define a variable as "double PI = 3.1415926;", we can add a final keyword before double to define a constant.

 

Do you have some knowledge about Java statements and definitions? Maybe we have seen some variables with the keyword "static" before. What is the purpose of this keyword? The valid range of the variable is described here.

The effective scope of a variable refers to the region where the program code can access the variable. If the variable is accessed beyond the region where the variable is located, an error occurs during compilation. It can be divided into "member variables" and "local variables ".

Variables defined in the class body are called member variables. member variables are valid throughout the class, including static variables and instance variables.

Class var {int x = 45; // defines the instance variable static int y = 90; // defines the static variable
}

X is the instance variable, and y is the static variable. A static variable is called a static variable before the type of the member variable. The valid range of the static variable can be cross-class, or even within the application. The class name can also be used. static variables are used in other classes. (For more information, see the relevant documents ).

The variables defined in the method body of the class (that is, the declared variables between "{" and "}") are called local variables. It is valid only in the current code block. Simply put, it is valid only in the braces defined by it. The following is an example:

1 public class Val {2 3 static int times = 3; // define the member variable times 4 public static void main (String [] args) {5 int times = 4; // define the local variable times 6 7 System. out. println ("times value:" + times); // output times to 8 System. out. println ("the value of the static variable times is:" + Val. times); // output static variable 9} 10 11}

Output result:

  

From this code, we can see that a member variable times is defined out of the main method and a static variable, and a local variable times is defined in the main method. Someone will ask, isn't the variable name the same? Note: here the first times is a member variable, and its scope is for the entire class. The second times is a local variable and only valid in the main method, so the two duplicate names do not conflict. However, if times is output directly, for example, the output result of the first output is 4, it is a local variable, becauseWhen the local variable and the member variable name are the same, the member variable is hidden, that is, the member variable is temporarily invalid in this method.To call a member variable, use the class name. static variable "call, such as the second output, class name Val, static variable name times, with" Val. the value of the static variable to be called is 3.

V. Code comments

Annotations can improve the readability of the program. The text in the annotations does not affect the program. in Java, there are mainly the following types of code Annotations:

1. Single Row comment

"//" Indicates a single line comment. All content starting from "//" to the line feed is commented out and ignored by the compiler.

// This is a single line comment
2. Multi-line comment

"/**/" Indicates multi-line annotation. All content between the symbol "/*" and "*/" is the annotation content and can be wrapped in a line break.

/* Comment content 1 comment content 2 ......*/

You can nest single-row comments in multi-row comments. For example, the following usage is correct:

/* Name: blog garden // time */

However, multi-line comments cannot be nested.

3. document comments

"/***/" Indicates the document annotation. The content between the symbol "/**" and "*/" is the document comment content. When a document comment appears before any declaration, it is read as the content of the Javadoc document by the Javadoc document tool. The format is the same as that of Multiline comments.

1 /**2     * name: Hello World3     * time: 2018-1-204     * author: Adam5 **/

 

In the next article, we will continue to introduce operators and type conversion in the Basic Java language.

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.