Personal Learning Path of Java--java Language Foundation

Source: Internet
Author: User

Tag: Pack executes newline bool out main relationship call Port

The Java language is an object-oriented programming language, the basic components of Java programs are classes, and the class body includes attributes and methods in two parts. Each program must contain a main () method, and the class containing the main () method is called the main class.

As in the following code:

1  Packagetext;2 3  Public classFirst {4      Public Static voidMain (string[] args) {5          for(inti=0;i<=9;i++) {6              for(intj=1;j<=i;j++) {7System.out.print (j+ "*" +i+ "=" + (i*j) + "\ T");//print indicates no line break output8             }9System.out.println ();//println for automatic line wrapping after outputTen          One } A } -}

PS: The output of the above code is a 99 multiplication table (it doesn't matter if you don't understand it, take it slow).

The above code we know: The program's package is text, the class is first.

A Java program is made up of several classes (first of which is a class). The package text declares the packages that the class is in, and the package is the keyword for the packages (it is not now known what the package is, but what is known later).

  declaration of a variable :

Variables are divided into global variables (member variables) and local variables;

In writing programs, the properties of a class are usually called global variables, and the properties of a method are called local variables. Global variables are declared in the class body, and local variables are declared in the method body. I and J in the above code are local variables (if the variable is declared in the public class first{...} class, but not inside main () is called a global variable.

The main () method is the main method in the class body. public static void is the permission modifier, static modifier, and return value modifier of the main () method, respectively. The main () method in a Java program must be declared as public static void. String[] args is an array of string types, which is the parameter of the main () method, where the program starts executing (that is, the program entry point).

When writing a Java program, you can import related classes by importing keywords (specifically, this is not written here).

 Basic data type:

In Java, 8 of the basic data types are stored in numeric values, characters, and Booleans.

Numeric type: Integer type: (Byte (1 bytes), short (2 bytes), int (4 bytes), Long (8 bytes) floating-point type: (Float (4 bytes), double (8 bytes))

Character type

Boolean type

 Integer type:

Integer types are used to hold integer values, that is, numeric values that do not have fractional parts. Can be a positive number, or it can be a negative number. Can be expressed in decimal, octal, hexadecimal. (PS: In decimal notation cannot start with 0, octal means must start with 0, hexadecimal means must start with 0 x or 0x)

For long values, if the assigned value is greater than the maximum range value of type int or less than the minimum range value, you need to add L or L after the number to indicate that the value is a long integer, such as a tel=18325045047l;

The following code is an instance of the data type declaration:

  

1  Packagetext;2 3  Public classNumber {//Create Class4      Public Static voidMain (string[] args) {//Main Method5         bytemybyte=100;//declaring a byte variable and assigning a value6          Shorta=12345;7         intb=3434343;8         Longc=123345435;//declaring a long variable and assigning a value9         Longresult=mybyte+a+b+C; TenSystem.out.println ("Sum of:" +result); One     } A}

 Floating-point types:

Floating-point types are the float type and double type.

When defining a floating-point type, one thing to note is that, by default, decimals are treated as double types, and to be defined as float types, add F or f after decimals.

 Character type:

The character type (char) is used to store a single character, occupying 16 bits (two bytes) of space. When defining a character variable, enclose the character variable in single quotation marks, such as Char ch= ' t ';

As in C, Java can also treat characters as integers, except that Java uses Uniicode encoding (the C language is ASCII encoded). That is, you can represent a char type with an int type, or you can represent an int type with a char type, such as:

      

1  Packagetext;2 3  Public classGess {//defining class character conversion output4      Public Static voidMain (string[] args) {//Main Method5         CharWord1= ' $ ', word2= ' @ ';//defines a char type variable, which is later converted to a Unicode value6         inta1=23045,p2=45213;7The SYSTEM.OUT.PRINTLN ("$ In Unicode location is:" + (int) word1);//What data type you need to output the variable to add the data type above8System.out.println ("@ In Unicode location is:" + (int) word2);9System.out.println ("The No. 23045 bit in the Unicode table is:" + (Char) 23045);TenSystem.out.println ("The No. 45213 bit in the Unicode table is:" + (Char) 45213); One     } A}

In the above code, the coercion of the data type is exploited.

  Boolean type:

A Boolean type is also known as a logical type, and you can define a Boolean type variable by using the keyword Boolean, but be aware that only true and false are two values, which represent true and False in Boolean logic, respectively. Boolean types cannot be converted to integer types and are often used in process control to determine conditions

 Identifier:

An identifier is a name that is used to denote a class name, variable name, method name, array name, file name, and so on.

java specifies that identifiers consist of any sequence of letters, underscores, dollar signs ($), numbers, and the first character cannot be a number, and identifiers cannot be reserved words for the Java language.

 Variables and constants:

In a program, the amount of value that cannot be changed is called a constant, and the value can be changed as a variable.

    Name of the variable:

1. The variable name must be a valid identifier

2. Variable names can not use keywords in Java

3. Variable names cannot be duplicated

4, should choose the meaningful word as the variable name (individual recommended hump name method)

  Declaring constants:

Syntax: Final data type constant name [= value];

PS: Constant names are usually denoted with uppercase letters

Such as: Final double pi=3.14;   Final Boolean bool=true; Don't forget to sign when you name

 Valid range of variables:

The valid range of a variable refers to the area where the program code can access the variable.

Member variables: variables defined in the class body, and member variables are valid throughout the class. The member variables of a class are also classified as static variables (class variables) and instance variables.

If you precede the type of a member variable with the keyword static, such a member variable is called a static variable.  The valid range of static variables can span classes, even within the entire program.   int x=10;   static int y=10; Here x is an instance variable and y is a static variable.

Local variables: Variables defined in the method body of a class, local variables are only valid in the current code block.

Variables declared in a method of a class, including the parameters of a method, belong to local variables. The life cycle of a local variable depends on the method, and when the method is called, the local variable takes effect, and when the method call finishes, the local variable is destroyed.

The local variable can be the same as the name of the member variable, at which point the member variable is hidden, that is, the member variable is temporarily invalidated in this method.

Personal Learning Path of Java--java Language Foundation

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.