Java basic Syntax (i)---keywords, constants, variables, operators
First, the key word
Definition: A word that is given special meaning by Java.
Features: The expression is English lowercase.
1. Keywords for defining data types
Basic data type:
Integer type: Byte (1 bytes, maximum value 127) Short (2 bytes, maximum 2^15-1)
Int (4 bytes, Max 2^31-1) long (8 bytes, maximum 2^63-1)
Floating-point type: float (single-precision real number in memory as 32bit significant digit 6~7 bit)
Double (dual precision real number in memory unit is 64bit valid digit is 15~16 bit)
Character type: Char such as ' a ', ' 1 ', etc.
Boolean: Boolean (Flas true)
Reference data type:
Class (classes) interface (interface)
void for functions that represent no return value type
2. Keywords for defining data type values
True (True) False (false) null (NULL)
3. Keywords for defining Process Control
Judge:
If Else
Switch case Default
Cycle:
For
While
Continue loop: Continue end loop or select structure: Break return: Return
4. Keywords used to define access rights modifiers
Private: Used in classes defined on methods or variables, external classes cannot access
Protected (Protected): Available when some properties in a parent class only want subclasses to inherit
Public: Permissions are highest and can be used to decorate classes, methods, and so on.
5. Keywords for defining classes, functions, variable modifiers
Abstract (abstraction); final (Final): Static:
Synchronized (synchronous): Judging the existence of a lock
Enum (enum): Using in Vector container
6. Keywords used to define the relationship between classes and classes
Extends (inheritance): used when subclasses inherit a parent class
Implements (Implementation): used when implementing an interface
7, to define the establishment of instances and reference instances, to determine the key words of the instance
New (build instance) this (current reference) Super (parent class reference)
instanceof (Judging object type): if (obj instanceof Student) determine if obj is a student type
8. Keywords for exception handling
try{code that needs to be instrumented;}
catch () {The code that handles the exception;}
finally{code that is bound to execute;}
Throw throw is defined within a function to throw an exception object
Throws: Defined on a function, used to throw an exception class, can throw multiple separated by commas
9, for the package of keywords
Package (Create packages)
Import (Importing package) For example: import java.util.*; (* on behalf of all)
10. Other modifier keywords
Native (local)
STRICTFP (Strict float point, precision float)
Transient (variable modifier, which declares an instance variable, is not serialized when the object is serialized)
Volatile (type modifier, used to decorate variables that are accessed and modified by different threads)
ASSERT (assertion, which can be thought of as an advanced form of exception handling, captures the assumptions we make in our code)
Note: Main is not a keyword, but it is a name that is recognized by the virtual machine.
Second, identifiers
Some of the names that are customized in the program. Made up of 25 letters of the alphabet, the number 0-9, in accordance with _ $ composition.
Rules for defining legal identifiers:
1, the number can not start.
2, can not use keywords.
Note: The case of identifiers is strictly distinguished in Java. In the name of the time, in order to improve the reading, to try to make sense.
Name specification in Java:
Package Name: All letters are lowercase when multiple words are composed.
Xxxyyyzzz such as MyPackage
Class name Interface name: When multiple words are composed, the first letter of all words is capitalized.
Xxxyyyzzz such as Mycomparato
Variable name and function name: When multiple words are composed, the first letter is lowercase, and the second word starts with the first letter of each word capitalized.
Xxxyyyzzz toString
Constant name: All letters are capitalized. For multiple words, each word is connected with an underscore.
Xxx_yyy_zzz For example max_priority highest priority
Three, constant
Definition: A fixed value in Java that represents an immutable number.
Categories of Constants in Java:
1, integer constant. All integers.
2, decimal constant. All decimals.
3, Boolean constant. More unique, only two values. True false.
4, character constant. A numeric letter or symbol is identified by a single quotation mark (' ').
5, string constant. Identifies one or more characters in double quotation marks ("").
The 6,null constant. There is only one value: null.
For integers, Java has three representations:
Decimal: 0-9, full 10 in 1.
Octal: 0-7, full 8 in 1, denoted by the beginning of 0.
Hex: 0-9,a-f, full 16 in 1, denoted by the beginning of 0x.
Basic conversion of the binary:
Other decimal in binary
Integer.valueof ("Y", X): X is the number of binary, Y is the value you want to turn
Decimal Turn binary: tobinarystring (x)
Decimal conversion to 16 binary: tohexstring (x)
Decimal into octal: tooctalstring (x)
Iv. variables
Concept: A storage space in memory. Has its own name (variable name) and type (data type).
Function: It is used to keep the same type of data, and can be reused.
Define the format of the variable: data type variable name = initialization value;
Data type:
There are two types of data in Java: 1, basic data type, 2, reference data type. The basic data types are mainly explained here.
Description: In Java program, integer default: int decimal default: Double
automatic promotion of type:
In operations, when low-type data is computed with high-type data, the system automatically promotes the low-type data in the expression to a high type. Such as:
byte B = 3; int C;
C= B + 2; b automatically promotes the operation of the int type.
To force type conversions:
When a data type needs to be transformed in a program, it can be cast. Such as:
byte B = 3;
B = B + 4;//error
b = (byte) b+4;//coercion type conversion, forcing the result of the b+4 to be converted to a byte type and then assigned to B.
V. Operators
1. Arithmetic operators
Note: When taking a modulo operation, if there is a negative number, the result of the operation depends on whether there is a negative number on the left.
String data and any data using + are connected and eventually become strings.
2. Assignment operators
Symbol: = = = = *=/=%=
Example: int a,b,c; A=b=c = 3;
int a = 3; a+=5;//equivalent operation A=a+5;
3. Comparison operators
Note: 1, the result of the comparison operator is Boolean, which is either true or false. 2. The comparison operator "= =" cannot be mistakenly written as "=".
4. Logical operators
Logical operators are used to concatenate expressions of type Boolean.
&: As long as the Boolean expression on both sides results in a false, the result is false. Only both sides are true and the result is true.
|: As long as one of the two sides is true, the result is true; only both sides are false and the result is false.
^: The same result on both sides is false, and the different results on both sides are true.
The difference between & and &&:
Single &, both True and false on the left, the right side of the operation;
Double & If the left is true, the right side participates in the operation, if the left is false, then the right side does not participate in the operation.
| and "| |" The difference is the same, dual or time, the left is true, the right does not participate in the operation.
5, bitwise operator (head to small small why move)
Left:<<; vacancy 0, the removed high position is discarded, the vacancy bit is 0.
Right shift:>>; is shifted the highest bit of the binary is 0, after the right shift, the vacancy is 0, the highest bit is 1, the vacancy bit is 1.
Unsigned right shift:>>>; is shifted binary highest bit either 0 or 1, the vacant bit is 0 complement.
With: &; bits for & operation, only 1&1 when the result is 1, otherwise 0.
Or: |; Bits | operation, only 0 | 0 o'clock the result is 0, otherwise it is 1.
XOR: ^; any of the same bits ^ operations, the result is 0;1^1=0, 0^0=0.
Not the same bits ^ operation result is 1. 1^0=1, 0^1=1.
Inverse code: ~; Reverse binary.
6, ternary operator
D=a? B:c, that is, when a is true, D=b,a is false, B=d.
Gets the large number of two numbers.
int x=3,y=4,z;
z = (x>y)? The x:y;//z variable stores a large number of two numbers.
Precedence of the operator:
Six, escape character
Concept: Change the meaning of the letter or symbol behind it by \.
Common escape characters:
\b: Backspace
\ n: Line break
\ t: tab, equivalent to Tab key
\ r: Enter
\ \: Represents a backslash
\ ': denotes single quotation marks
\ ": represents double quotation marks
Vii. Practical Exercises
1 classOperatedemo2 {3 Public Static voidMain (string[] args)4 {5 intx = 4270;6 7x = x/1000 * 1000;//results: x=4000;8System.out.println ( -1%5);//Results-19 Ten intA = 3, B; One A //a++;//- A = a + 1; - - theb = ++a;//results b=4; - -System.out.println ("a=" +a); - //string data and any data using + are connected and eventually become strings. + //System.out.println ("5+5" + (5+5));//"5+5=55" - + A /* at escape Character: Change the meaning of the letter or symbol by \. - \ n: line break. - \b: Backspace. Equivalent to BACKSPACE. - \ r: Press ENTER. Window System, the carriage return is represented by two characters \ r \ n. - \ t: tab. Equivalent to the TAB key. - */ inSystem.out.println ("Hello \ t World"); - //System.out.println ("Hello java"); to +System.out.println ("\\hello\\"); - the Charch = ' \ '; * $ Charc = ' a ';Panax Notoginseng - the } +}
1 classLucknumber2{/*there are N children holding hands in a circle, numbered 1~n, children in turn starting from 1 to report M,3 Check in M automatically exit the circle, the game to the last remaining is the number of children? 4 Analysis: Save the number of children in the array,5 when reporting m, the array element is changed to 0;6 when it touches 0 o'clock, the pointer automatically reads the next element,7 When checking in the last one, the pointer jumps to the 0-point mark and starts anew8 */9 Public Static voidSOP (Object obj)//Print any objectTen { One System.out.print (obj); A } - Public Static intGetluck (intArr[],intm) -{intPos=-1,num=0; the for(intx=0;x<arr.length-1;x++)//Take out the number of elements-1, to remove the only last element left - { - for(inty=0;y<m;y++)//makes the element 0 each time it is counted over m nonzero elements -{pos++; + if(pos==arr.length) -Pos=0; + while(arr[pos]==0) A{pos++; at if(pos==arr.length) -{pos=0; - } - } - - } inArr[pos]=0; - } to for(intt=0;t<arr.length;t++)//Looking for + { - if(arr[t]!=0) the { *num=Arr[t]; $ }Panax Notoginseng } - returnnum; the } + Public Static voidMain (string[] args) A { the intn=3,arr[]=New int[n];//set up an array, passing the 1~12 to the array through a for loop + for(inti=1;i<=arr.length;i++) - { $arr[i-1]=i; $ } -SOP (Getluck (arr,2));//the array and the eliminated number are passed in - } the}
the topic of the article is summarized from the following network link, because the framework of their own summary is not specific, so I found this blog on the network, and think this blog is very good, so on its basis, added some of their own summary, and deleted the part. (There is no commercial purpose, only for oneself can summarize, comb)
http://blog.csdn.net/kangmiao89757/article/details/10615069
Java basic Syntax (i)---keywords, constants, variables, operators