Since the decision to work as a programmer in this industry, more learning something is always good, so began to learn a new language, do not know how to learn, try to see it
Identifiers: where you can name them, they are called identifiers, and they follow the rules of Identifiers.
Identifier composition
the Java identifier consists of a number, a letter and an underscore (_), and a dollar sign ($).
and must abide by the following rules :
1.Java identifiers can only consist of numbers, letters, underscores "_" or "$" symbols, and Unicode character sets
2. TheJava identifier must start with a letter, an underscore "_" or a "$" symbol, and a Unicode character set
3.java identifiers may not be Java keywords, reserved words (const, goto), and literals (true, false, Null)
4.Java identifiers are case-sensitive and case-insensitive
Naming conventions
Java identifiers should adhere to the following naming conventions:
1. class and interface name . The first letter of each word is capitalized, and contains the case for example:
- MyClass,HelloWorld, time
2. method name . The first character is lowercase, the remainder is capitalized, and is Case-sensitive. Use as little underline as Possible. For example:
- MyMethod,mygetdata//this naming method is called Camel-named.
3. constant name . The constant name of the base data type uses all uppercase letters, and the words are separated by underscores. Object constants can be mixed in size. For example:
- Size_name,size_age
4. variable name . Can be mixed in uppercase and lowercase, the first character is lower case, and the word delimiter is capitalized with the first letter of the Word. Don't underline, use the dollar sign Less. The name of the variable is to do as far as possible to see the Meaning. For example:
- Namelength,currenttime
Key words
Keywords are pre-defined in the language of a computer, with special identifiers, sometimes called reserved words, and variables of special significance.
Java 's keywords have special meanings for Java compilers, They are used to represent a data type, or to represent the structure of a program, and the keyword cannot be used as a variable name, method name, class name, package Name.
The keywords in Java are all lowercase english.
Constant
A constant is an identifier whose value remains constant during Operation. and constants can only be referenced in the program and cannot be Re-assigned.
Naming rules for Constants:
1. In JAVA , adding the final keyword to a variable declaration represents a constant, and adding the static keyword represents a class Variable. In general, we combine static with final to declare a constant.
2. Try to use the represented meaning intuitive to represent numbers or strings that will appear more than once in a Program.
- Public Static final double PI = 3.1415926;
A variable is declared in the Java interface and is automatically added to the public static final modifier at compile Time. is automatically declared as a global constant, so the Java interface is usually the best place to store Constants.
Variable
Java variables are the most basic storage units in a program, and their features include variable names, variable types, and Scopes.
Each variable in a Java program belongs to a specific data type and must be declared before it is used, in the form Of:
- Type VarName [=value][{,varName[=value]}];
- int a = ten;
- String B = "hello";
- int D = 3,e,f = 5;
type is One of the data types of Java. The identifier is the name of the Variable. Declare multiple variables of the specified type with a comma-delimited List.
In essence, a variable is actually a small chunk of memory in memory, using the variable name to access the AREA.
therefore, each variable must be declared before it is used, and then it must be assigned (populated) before it can be used.
Local variables and member variables
Local variables: variables defined inside a method or statement block are called local Variables.
- Public void addData() {
- int a = ten; The variable "a" is a local variable, and "a" cannot be used outside the curly braces of the Method.
- }
Member Variable: A variable that is defined internally by a method outside of a class is called a member Variable.
- Public class testadd{
- Static final int B = ten; The variable "b" is a member variable that can be used within the entire class.
- }
Member variables can also be divided Into: class variables and instance Variables.
Class Variables: also known as static variables, with static decoration, it can be called directly with the class name, can also be called by the object, and all objects of the same class variable is shared the same block of memory space. the static final variable, which must be initialized at the time of declaration or inside Static BLOCK.
Instance variable: without static adornments, It can only be called through an object, and the same instance variable of all objects is shared with different memory Spaces.
Note: if the name of a local variable in a method is the same as the name of a global variable, the global variable is temporarily invalidated in this method
Scope of the variable
variable scope refers to the part of a program in which the name of a variable can be referenced. This may be more abstract, the scope of the variable is commonly understood as a variable after the declaration, which part of the program can access the declared Variable.
Variable scope is called the accessibility of the Variable. There are a number of rules in Java :
1. Variables defined in a statement block can only be accessed in blocks. The scope of this variable is within the block that declares it
2. Variables defined in the inner block can access variables defined in the outer block
3. Variables defined as method parameters can be accessed within the method body
4. Class variables can be accessed from anywhere in the class
5. If a variable with the same name in the outer block is defined in the inner block, the variable in the inner block overrides the variable definition in the outer block
To give an example:
public class test{ int a = ten; public void getData () { int b = 5; for (int i = 0; i < b; i++) { int c = 9;}}} Variable A is accessible anywhere inside the test class, and variable B can only be accessed inside the method, and variable C can only work inside the for Loop.
Data type Partitioning
Data types can be divided into two categories:
1: basic Data type
- 1) integer type
- Includes:byte, Short,int,long
- 2) Float type
- Includes:float,double
- 3) Character type
- Includes:Char
- 4) Boolean type
- Includes:Boolean
2: Reference Data type
- 1) class
- Includes:class
- 2) interface
- Includes:Interface
- 3) Array
Logic type
the Boolean type is suitable for logical operations and is generally used for program flow Control.
Boolean data only allows values of true or false, and cannot replace true and falsewith integers of 0 or not 0. And be sure to lower case.
How to Use:
Boolean b1=false; // Direct Assignment Boolean b2=3>4; // assign a value by a conditional expression Boolean b3=b1; // assign a value by another Boolean variable Boolean B4=list.ready (); // the assignment is returned by the method, assuming that the Ready method returns a Boolean.
Boolean type variable in program
Examples of how to use:
Boolean false ; if (! Flag) {//dosomething}
Character type
the char type data is used to denote a "character" in the usual sense.
A character constant is a single string enclosed in single quotation marks. For example
char Echar = ' a '; char Achar = ' Medium ';
Char in java, it is 2 bytes. Java uses Unicode, 2 bytes (16 bits) to represent one character. The Unicode of a Chinese character is 2 Bytes.
Note: char types are not recommended for storing character data inJava , typically using string or stringbuffer storage.
Integer type
Integer type contains four types:int(integer), long (length), short, byte (byte Type)
They all define an integer, and the only difference is the range of data that they can represent.
They all have a fixed number of table ranges and field lengths, which are not affected by the specific operating system to ensure the portability of Java programs.
Range of Values:
type occupies a range of storage values byte 1 bytes [ -2 (7) ~ (7)-1] -any integer between 128 and 127 Short 2 -byte [ -2 (+) ~ (+)-1]Any integer between -32768~ 32767 int 4 bytes [-2 (31) ~ (31)-1]-231 to 231-1 any integer long 8 bytes [-2 (63) ~ (63)-1]-any integer between 263 and 263-1
The larger the range that can be represented, the greater the memory footprint, so in programming, you should choose the most appropriate type to define Integers.
Floating point Type
Similar to integer types,Java floating-point types have a fixed number of table ranges and field lengths that are unaffected by the platform
The variable data type that stores decimals---floating point numbers, which are relative to the fixed-point number, which is the decimal point. The decimal point can change position as Needed.
There are two types of floating-point numbers in the Java language:float, double.
where float is a single-precision type, occupying 32-bit memory space, and Double is a dual type, occupying 64-bit memory space.
Numeric value contains a decimal point or exponent, or a number followed by a letter F or F (float), D or D (double)
float can be accurate to 7 significant digits, the 8th digit is the 9th digit rounding on the obtained;
double can be accurate to 16 digits, the 17th digit is the 18th digit rounding on the
double a = 3.4; float f = 4.3f; // no F compile error, loss accuracy
Introduction to Java Basic Concepts (i)