Java Getting Started tutorial two (Language Basics)

Constants and Variables

Also called literal constant value, which is represented directly by the data

constant

Real constant value

Java's real constant values ​​There are two main forms of

Decimal form: numbers and a decimal point composition, and must have a decimal point, such as 12.34, -98.0
form scientific notation: As 1.75e5 or 32 & E3, which must be digital before e or E, and the digits after the e or E must be integer.

Boolean constant value

Java Boolean constants are only two values, that is false (false) and true (true)

And character string constant value

Java is a character constant values ​​caused by single quotes to a character, such as' e ', E'. Note that, Java string constant values ​​of single and double quotation marks can not be mixed. Double quotes used to represent strings, like "11", "d" are all single-character string represented.

Define constants

Unlike constant constant value, it can be replaced with a constant value of symbols used, and therefore must be defined before use. Java language using the final keyword to define a constant.

The syntax is as follows:

final dataType variableName
final int COUNT=10

variable

Alias ​​variable is used to describe a piece of information, for example:

DataType identifier=value; 或 DataType identifier;
String name;
String name = "HelloWorld.Link"

Variable scope

Depending on the scope, generally variable assigned to different classes: class variables, local variables, methods and other parameter variables.

Class variables

Class variables, also known as member variables declared in the class, does not belong to any one method, the scope is the entire class. Examples are as follows:

public class HelloWorld
{
    private int age=20;     //定义类变量 price
    private String name;    //定义类变量 name
}

Local variables

Local variables are variables in the process or method defined in the code block, which in its scope is code block.

public class HelloWorld
{
    public static void main(String[] args)
    {
        int age=30;
        if(age>10)
        {
            int test=3;    //声明一个int类型的局部变量,只能在if内使用
        }
    }
}

Method parameter variables

As a method parameter variables declared in the scope of the whole approach.

public class HelloWorld
{
    public static void test(int input) //声明一个方法参数变量,只能在方法体内使用
    {
        System.out.println("input="+ input);
    }
}

type of data

Integer type

Java defines four integer type variable: byte (byte), short integer (short), integer (int) and a long integer (long). Signed values, positive or negative.

Byte (byte)

a byte is the smallest integer type. When processing the user data stream from a network or file, or when dealing with raw binary data may not be directly compatible with other types of built-in Java, which is useful type.

Short integer (short)

limiting short type data stored for the first high byte to low byte, so that an error occurs in some machines, this type is rarely used.

Integer (int)

int type is an integer type most commonly used.

Longs (long)

For large programs often encounter great integer, you should use the long type when out of range of type int represents.

Examples are as follows:

public static void main(String[] args)
{ 
    byte h=10;     //声明一个byte类型的变量并赋予初始值为10
    short e=20;    //声明一个short类型的变量并赋予初始值为20 
    int l=30;      //声明一个int类型的变量并赋予初始值为30
    long o=40;     //声明一个long类型的变量并赋予初始值为40
}

Floating-point type

Floating-point data type is the type with a fractional part, also called real. Floating-point data comprises a single-precision floating-point (float) and double precision floating point type (double), representative of the number with a decimal precision. The difference between the single-precision floating-point (float) and double precision floating point type (double) is mainly occupied by a different memory size, float type uses 4 bytes of memory, double type occupies 8 bytes of memory space. Double double type having a higher accuracy and greater range than representation type single precision float.

public static void main(String[] args)
{ 
    double a=2348.4;    //定义double类型的变量
    float b=0.01;       //定义float类型的变量
}

Boolean

Boolean (boolean) for two values ​​by logic operation, the judgment result is "true" or "false."

boolean a=true;
boolean b=false;  

Character Types

Java language character type (char) use two-byte Unicode encoding said it supports all languages ​​of the world, you can use a single quote character or integer type of assignment char.

char a='D';
char b='5';

Operators

Arithmetic operators

Operator according to the number of operands can be divided into unary operators, binary operators and ternary operator. Arithmetic operators is a function arithmetic operation, in addition to commonly used plus (+), subtract (-), multiply ( ) and other than (), there are the modulo operation (%). Plus (+), subtract (-), multiply ( ), except () mathematical operation and we usually contact have the same meaning. Remainder operation (%) refers to a variable or constant is connected to two division operation, the results were the remainder thereof.

int a=4,b=2,c=3;
int d=a*(b+c)%c; //最终d为:2

Assignment Operators

It refers to a symbol assignment operator specified value for the variable or constant. Symbol assignment operator is "=", which is a binary operator, the left operand must be a variable, not a constant or expression. Assignment operator used together with other operators, the effect can be expressed more variant assignment operation.
The numerical operators + = left plus right value, which assigns the result to the left of the variable itself
- = The value operator subtracts the left and the right value, which assigns the result to the left of the variable itself
* = The operator multiplied by the value of the left and the right value, which assigns the result to the left of the variable itself
and the right value is the value of the left side of the operator divisible / =, which assigns the result to the left of the variable itself
value% = the operator the right to the left of dividing after taking more than the value that assigns the result to the left of the variable itself

int a=5; a+=2; //a=7
int a=5; a-=2; //a=3
int a=5; a*=2; //a=10
int a=5; a/=2; //a=2
int a=5; a%=2; //a=1

Logical Operators

The relational expression logical operator connected together to form the respective operation a complex logical expression to determine the expression in the program is satisfied, the judgment result is true or false. It comprises logical operators &&, || ,! . Wherein && and || are binary operators, to achieve logical AND or logical;! It is a unary operator, non-implementation logic.

int x=1;y=99
x>0 && x<=100 //输入ture

Relational Operators

Relational operators used to compare the relationship between two values. Relational operators are binary operators, it is a boolean operation result. When the establishment of relational operators corresponding to the operation result is true, otherwise false. > Greater than operator,> = Greater than or equal to operator, <less than operator <= less than or equal to operators, equality operators ==,! = Not equal operator

2>3   //false
4>=2  //true
2<3   //true
4<=2  //false
4==4  //true
4!=2  //true

Increment and decrement operators

When a variable is to do processing incremented or decremented by 1, the operator can increment or decrement ++ -. + Or - is a unary operator, the front or rear of the operand placed is allowed. + And - the role of the variable increment or decrement the value of 1. Operand must be an integer or floating point variables. i ++ to use the value of i plus 1 assigned to the variable itself i, ++ i to increase the value of i i 1 assigned to the variable itself after use, i-- the value of i start with minus 1 assigned to the variable i itself, - after the value of i to i minus 1 assigned to the variable i reuse itself

int i=1;
int j=i++;//i为2,j为1
int x=1;
int y=++x;//x为2,y为2
int a=1;
int b=a--;//a为0,b为1
int c=1;
int d=--c;//c为0,d为0

Bitwise Operators

Bitwise 4 comprising: & (and), | (or) ~ (non) and ^ (exclusive OR). In addition ~ (Accession inverted) of unary operators, the rest are binary operators. & Bitwise ANDed, | bitwise ORed ^ bitwise XOR, ~ bitwise negation operation performed

4 & 5 //结果:4
4 | 5 //结果:5
4 ^ 5 //结果:1
~ 4   //结果:-5

Conditional operator

The conditional operator notation as "?:" Requires three operands used when the operator, so called ternary operator.

int x,y,z;
x=6,y=2;
z=x>y?x-y:x+y;//z为4

Data type conversion

Inconsistent data type conversion occurs at the assigned numeric types and data types of the variable are received, which need to convert from one data type to another data type. Data type to be converted into an implicit (automatic type conversions) and explicit conversions (casts) two.

Implicit conversion

Automatic type conversions need to satisfy two conditions: ① two types of data compatible with each other, ② a range greater than the target value of the type of source data type (data type is converted into lower advanced type of data). For example, to convert a byte type short, due to the large type of short range, it will automatically be converted to byte short type.

int a=1;
int b=2;
double c=a+b;//c为3.00

Explicit conversion

When both data types are not compatible, or in the range less than the target type of source type, it can not be converted automatically, then they need to be cast.

int a=3;
double b=5.0;
a=(int)b;//a为5

Guess you like

Origin www.cnblogs.com/lilinfeng/p/10969441.html