Javase (ii) Variables and constants, operators, type conversions

Source: Internet
Author: User

One, constant    1.1. What is a constant

Its value cannot be changed during program execution

  1.2, the classification of the constant 

Literal constants

The contents of a string constant enclosed in double quotation marks
integer constant all integers
Decimal constant All Decimals
Character constants are enclosed in single quotes, with only a single number, a single letter, or a single symbol
Boolean constants are special, only true and false
Empty constant Null (part of the array explained)

Custom Constants (Object-oriented section)

  

second, the variable   1.1. Introduction of Variables   

The basic function of the program is to process the data
The program uses variables to receive and represent data;
A variable must be defined in the program before it can be used;
The definition variable refers to the data type of the set variable and the name of the variable, and the Java language requires that the variable follow the rules defined first, then initialized, and then used.

The use of variables has a scope problem, scope refers to its existence, only within this scope, the program code to access it.

Second, the scope determines the life cycle of the variable. The life cycle of a variable is the beginning of a variable being created and allocated to the memory space,
The process by which the amount of memory is destroyed and the space occupied by it is cleared. When a variable is defined, its scope is determined. Depending on the scope,
Variables can be divided into the following types:

Member variables: Declared in a class, whose scope is the entire class;
Member variables are also called attribute/instance variables

Local variable: internal declaration of a code block inside a method or method. If declared within a method, its scope is the entire method, and if it is declared inside a code block of a method, its scope is this block of code. Code blocks are code that is within a pair of curly braces "{}".

Method parameter: The method or the parameter of the construction method, its scope is the whole method or the construction method.
Exception handling parameters: Similar to method parameters, the difference is that the former is passing parameters to the exception-handling code block, while the latter is passing parameters to the method or constructor method. An exception handling parameter is an exception parameter "E" in a catch (Exception e) statement whose scope is followed by a block of code following the catch (Exception e) statement.

1.2. Local variables and instance variables

1.2.1, local variables

1) defined inside of a method or within a code block of a method;

public void Method1 () {
int a = 0; Local variables, scope meth for the whole od01 method;
if (a==0) {
int b = 0; A local variable, scoped to the block of code in which it is located;
b = A;
}
b = 20; Compile error, B cannot be accessed
}

2) local variable has no default value, must be initialized before use;

3) Life cycle
Starting with the declaration, until the end of the code block where the local variable is directly located
public class Sample {
public int Add () {
int addresult = 1;
Addresult = addresult+2;
return addresult;
}

public static void Main (string[] args) {
Sample s = new sample ();
S.add ();
S.add ();
}
}

1.2.2, instance variables

      

1) declared in the class, its scope is the whole class;

Class Test {
private int n1=0;
private int n2=0;

public int Add () {
int result = N2 + N2;
return result;
}
}

2) instance variable has default value, can not be initialized before use;
Note The default value for each variable
In the base type:
Integer variable default value is 0
Floating-point default value is 0.0
Char default value ' \u0000 '
Boolean Default value False
The default value for a reference type is NULL

3) Life cycle
An object from the class is created to start, and the object is destroyed
Class Test {
private int n1=0;
private int n2=0;

public int Add () {
int result = N2 + N2;
N1 = n1+1;
N2 = n2+2;
return result;
}

public static void Main (string[] args) {
Test T1 = new test ();
Test t2 = new Test ();

T1.add ();
T1.add ();

T2.add ();
}
}

Three, operator  

In general, you do not have to deliberately remember the precedence of operators, and when you cannot determine the order in which operators are executed, you can use parentheses to display the specified order of operations.

3.1. Assignment operator:

=: int x=0,i=1,j=1;
*=: a*=b equivalent to A=a*b
/=: a/=b equivalent to a=a/b;
%=: a%=b equivalent to a=a%b;
+=
-=
...
3.2. Comparison operators

>: Greater Than
>=: greater than or equal to
<: Less than
<=: Less than or equal to

The above operators only apply to integer types and floating-point number types;

int a=1,b=1;
Double d=1.0;
Boolean RESULT1 = a>b; The value of RESULT1 is false;
Boolean result2 = a<b; The value of RESULT2 is false;
Boolean RESULT3 = a>=d; The value of RESULT3 is true;
Boolean RESULT4 = a<=b; The value of RESULT4 is true;

Instanceof: Determines whether the object referenced by a reference type is an instance of a class. The operator to the left is a reference type, and to the right is a class
Name or interface name. The form is as follows:

obj instanceof ClassName
Or
obj instanceof InterfaceName


3.3. Equality operator

= =: Is it equal to
! =: is not equal to

Can be either a base type or a reference type:

A. Basic type:

int a=1,b=1;
float c=1.0f;
Double d=1.0;

System.out.println (A==B); Output true;
System.out.println (A==C); Output true;
System.out.println (A==d); Output true;
System.out.println (C==d); Output true;

B. Reference type:

Both reference variables must reference the same object, and the result is true.

Student S1 = new Student ("Zs", 25,100);
Student s2 = new Student ("Zs", 25,100);
Student s3 = S1;

System.out.println (S1 = = s2); Output false;
System.out.println (S1 = = S3); Output true;
SYSTEM.OUT.PRINTLN (s2 = = s3); Output false;

3.4. Mathematical operator

+: Data Type value addition or string connection;

A. The data type value is added;

int a=1+2; A value of 3;
Double b=1+2; b value is 3.0;
Double b=1+2.0; C value is 3.0;

B. String connection;

System.out.println (1+2+ "a"); Output 3a
System.out.println (1+2.0+ "a"); Output 3.0a
System.out.println (1+2.0+ "a" +true); Output 3.0atrue
System.out.println ("a" +1+2); Output A12
System.out.println (1+ "a" +2); Output 1a2

/: Evenly divisible, such as operands are integers, the result of the operation is the integer part of the quotient

int A1=12/5; The value of the A1 variable is 2
int A2=13/5; The value of the A2 variable is 2
int A3=-12/5; The value of the A3 variable is-2
int A4=-13/5; The value of the A4 variable is-2
int A5=1/5; The value of the A5 variable is 0
Double A6=12/5; The value of the A6 variable is 2.0
Double a7=12/-5.0; The value of the A7 variable is-2.4

%: modulo operators, such as operands, are integers, and the result of the operation is the integer part of the quotient

int a1=1%5; The value of the A1 variable is 1
int a2=13%5; The value of the A2 variable is 3
Double a3=1%5; The value of the A3 variable is 1.0
Double a4=12%5.1; The value of the A4 variable is 1.8000000000000007

3.5. Shift operator

>>: Arithmetic Right shift operation, also known as symbolic right shift operation.

int a1 = >> 1; The value of the A1 variable is 6;
0000 1100 12
-----------
0110 >>1
-----------
0000 0110 complement because it is a positive number, the 0 result is 6.

int a2 = >> 2; The value of the A2 variable is 32;
int a3 = 129 >> 2; The value of the A3 variable is 32;
int A5 = -12 >> 1; The value of the A4 variable is-6;
0000 1100 12
---------
1111 0011 Take counter
---------
1111 0100 +1 This is the-12 binary form
----------
111 1010 >>1

1111 1010 Complement because it is a negative number, so fill 1 This negative is the final result
---------
1111 1001-1
---------
0000 0110 The inverse result is 6 so the end result above is-6

int a6 = -12 >> 2; The value of the A4 variable is-3;

0000 1100 12
---------
1111 0011 Take counter
---------
1111 0100 +1 This is the-12 binary form
----------
1101 >>2

1111 1101 Complement because it is a negative number, so fill 1 This negative is the final result
---------
1111 1100-1
---------
0000 0011 The inverse result is 3 so the end result above is-3

Note: A. The process of moving one bit to 12 right is: discarding the last digit of the binary number, adding a bit of sign at the beginning of the binary number, since 12 is a positive integer, so the added sign bit is 0;
B. The process of 12 right shift is: discarding the last two bits of the binary number, adding two bit sign bits at the beginning of the binary number, because 12 is a negative integer, so the added sign bit is 1;


>>>: Logical Right SHIFT operation, also known as unsigned Right shift operation.

int a1 = >>> 1; The value of the A1 variable is 6;
int a2 = -12 >>> 2; The value of the A2 variable is 1073741821;

Note: A. The process of moving one bit to 12 right is: Discard the last digit of the binary number and add a 0 at the beginning of the binary number;
B. The 12 right shift two-bit process is: Discard the last two bits of the binary number, add two 0 at the beginning of the binary number;

<<: Left shift operation, also known as unsigned left shift operation.

int a1 = << 1; The value of the A1 variable is 24;
int a2 = -12 << 2; The value of the A2 variable is-48;
int a3 = << 2; The value of the A3 variable is 512;
int a4 = 129 << 2; The value of the A4 variable is 516;

Note: A. The process of moving left one bit to 12 is to discard the beginning of the binary number and add a 0 to the tail of the binary number;
B. The 12-shift-left two-bit process is: discards the first two bits of the binary number, adding two 0 at the end of the binary number;

3.6, bitwise operator

&: With operations, each bits of two operands is performed with operations, with the following rules: 1&1->1, 1&0->0, 0&1->0, 0&0->0;
| : or operation, for each bits of two operands, the operation rules are: 1|1->1, 1|0->1, 0|1->1, 0|0->0;
^: XOR, for each bits of two operands, the operation rules are: 1^1->0, 0^0->0,1^0->1, 0^1->1,; The same as 0 different bits 1 operation characteristics: a^0=a; a^a=0;
~: Take back operation, ~1->0, ~0->1;

3.7. Logical operators

The short-circuit operator, if it can calculate the Boolean value of the entire expression according to the Boolean expression on the left of the operation, will not execute the operator to the right
Boolean expression;

&&: The value of the Boolean expression on the left is false, the entire expression value is False, and the Boolean expression to the right of execution is ignored.
|| : The value of the Boolean expression on the left is true, the entire expression value is true, and the Boolean expression to the right of execution is ignored.

3.8. Conditional operator


Trinocular operation
Boolean expression? Expression 1: Expression 2

If the value of the Boolean expression is true, the value of expression 1 is returned, otherwise the value of expression 2 is returned.

int score = 61;
String result = score>=60? " Pass ":" Failed ";

Iv. type Conversion

Implicit conversions: auto-Convert
Basic type: Small precision can be automatically converted to a large precision
byte B = 1;
int a = b;
Reference type: Subclass class type can be automatically converted to parent class type
Student s = new Student ();
Object o = s;
Explicit conversions: Forcing type conversions
Basic type: Large precision can be forced to convert the type to a small precision, but may lose precision
int a = 1;
byte B = (byte) A;
Reference type: Parent class type can force type conversion to subclass type, but type conversion error may occur
The object class is the parent class of the student class
This is the right one.
Object o = new Student ();
Student s = (Student) o;

This will be an error.
Object O1 = new Object ();
Student S1 = (Student) O1;

      

   

  

  

Javase (ii) Variables and constants, operators, type conversions

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.