Java Basics Combing--picking up flowers (two)

Source: Internet
Author: User

1, Java language grammar rules and file format;

First Java program:
/**
First Java program:
Console output Hello world!
*/
public class test{
The access modifier class keyword is used to declare a class using the following class name
public static void Main (String args[]) {//entry to the program, main function Main method;
System.out.println ("Hello world!"); /output Statements
}
}

Java languages are case-sensitive;
Public is the access modifier;
class keyword, used to declare classes;
/**/multiple lines of comments; Comment out the content is not considered executable by the program;
Single-line comment;
A statement ends with a ";" semicolon ending, indicating that the statement has ended;
The Java language can be separated by spaces or line breaks, tab characters, and so on.

File format: A Java program that needs to have a class declaration, generally a. java file has only one class; However, there can be multiple classes present, at this point in the idea: A. java file, there can only be a public decorated class, that is: a Java filename, Should be the same as the class name that is decorated in this file; only one public-decorated class can exist in a Java file;


2, the entry main function of the program (Main method);

In Java, the main function of the primary method, the statement format is as follows: public static void Main (String args[]) {}

The main function can only exist in a public decorated class, otherwise the main method cannot be executed, and if it is placed in another class that is not decorated with public, there is no syntax error, but it cannot be executed.


3, the Declaration and assignment of variables;

Variables are mainly used for the storage of data, which need to be declared before use;
1, global variables: Declared in the class, methods, many methods can be used at the same time, also known as "attributes", class variables, member variables, etc., you can not assign an initial value, global variables can be preceded by the type of access modifier, to control the scope of the variable is accessed:
For example:
private int i=100;
Pulic String str= "";
protected short T;

2, local variables: Declared in the method for use by the current method, the declaration must be assigned or used must have an assignment operation;
For example:
BYTE type: Byte b=99;
Short integer type: int i=11;
Integral type: short t=5;
Long integer: l=1898989;
String variable: string str= "HELLO";
Float Type: Single precision: float f=1.78;
Floating-point: double d=1.7878787878;
Boolean type: Boolean b=true;
Character type: Char c= ' A ';


4, the classification of data types in Java;

Java can be divided into two categories of data types:
1. Basic Data type:
Integral type:
byte type (8-bit): Any integer from byte-128 to 127
Short-Integer (16-bit): Any integer between short-32768~32767
Integer (32-bit): Any integer between int-2^31 and 2^31-1
Long Integer (64-bit): Any integer between long-2^63 and 2^63-1
Floating point type:
Single Precision (32-bit): Float 32-bit single-precision floating-point number according to ieee754-1985 standard
Double Precision (64-bit): double
Boolean type: boolean (True/false)
Character type (16-bit):Char

2. Reference data type: Only the address of the object is stored in the memory unit of the reference type, and the object itself is stored in the memory heap, simply referring to the address of the object. Non-basic data type variables, which are reference data types;
(1) Classes class For example: Object class Date Class
(2) Interface interface
(3) Array of arrays


5, the conversion between the basic data types;

In Java, there are several ways to convert:
1, implicit conversion (also known as automatic type conversion): The system by default converts it to the type of data we need;
Conversion rules: From a type with a small storage scope to a type with a large storage range.
The specific rules are: Byte→short (char) →int→long→float→double

That is, variables of type short can be automatically converted to int type
For example: short t=10,10 default to int type;
Note: In Java, integers are defaulted to type int, and decimals default to double type;

2, Cast: Parentheses before the variable, write the conversion type, cast it to the specified type, coercion type conversion, also known as explicit type conversion, refers to a type conversion that must be written to complete the code.
This class type conversion is likely to have a loss of precision, so you must write the appropriate code and be able to tolerate that type of conversion.
Conversion rules: From a type with a large storage range to a type with a small storage range.
The specific rules are: Double→float→long→int→short (char) →byte
The syntax format is: (the type converted to) the value that needs to be converted
For example:
(1) byte<-Short<-int<-long (which indicates the direction of assignment) for example:
int I=10;short t= (short) i;
float f= (float) 10.123;
Long l=10; int i2= (int) L; Short t2= (short) L;
(2) double->float; (double assigned to float type)
Float/double-> (Byte/short/int/long) For example:
float f= (float) 10.123; float f=10.123f;
Double d=10.123;int i3= (int) d;short t3= (short) D;long l2= (long) D; byte b= (byte) D;
(3) (byte/short/int/long/float/double)->char
Char c= ' A ';
c= (char) i;
Special cases: Type conversions are implemented by some JAVAAPI-related methods (not conversions between basic data types)
For example:
Double dres=double.valueof ("10.89");

6, keywords, identifiers and constant usage;

1. Keyword (word): is a unique word in Java, is the computer language (here we refer to Java) in advance defined, has a special meaning of the identifier, sometimes called the reserved word, but also the special meaning of the variable.
For example:
Abstract class or method of abstraction
Assert used to find internal program errors
Break jumps out of a switch or loop
BYTE 8-bit integer type
A branch of case switch
Try block clause for catch catch exception
2. Identifiers
(1) consisting of letters, numbers, _ underscores, or $ dollar signs;
(2) cannot begin with a number;
(3) Case-sensitive, unlimited length, but to see the name of the meaning;
(4) cannot be a Java keyword or a reserved word;
For example:
Legal: Person, user_name, _username, $abc _123, Userproject
Illegal: 2Username, #Test, class, void, pulic

P.s. Classes, methods, properties (variables), constant naming conventions:
Class Name: The initial letter must be capitalized, followed by each word the first letter is also uppercase, other lowercase letters;
Method Name: Initial letter lowercase, followed by the first letter of each word capitalized;
Constants: all uppercase letters;

3. Constants: For example: When we define a gender identity in the project, such as male: sex_men= "1", when we use, never modify this data, at this point we can think of it as a constant, after defining the constant, will not be modified; (note: the declaration is used as a global variable in a class) for example:
Public final sex_men= "1";


7, operator, expression;
8, conditional Judgment statement: If statement and switch statement;
9, circular sentence structure;
10, the definition and use of strings;
11, the definition and use of arrays;

p.s. "Java Basics Comb-to Blossom" series by He Dongfei according to their own work for a few years to understand the understanding of Java, the author's leisure time will continue to update, there is no place please advise. qq:983489147

Java Basics Combing--picking up flowers (two)

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.