JAVA basics 2. java Basics

Source: Internet
Author: User

JAVA basics 2. java Basics

 

2.1 keywords

Common keywords include:

  

 

2.2 identifier

In short, an identifier is actually a symbol used to identify something.

  

 

2.3 annotation Application

Description

  

Comment:

  

Annotation application:

  

 

2.4 constants and variables

Definition and classification of constants

  

Origin of hexadecimal

  

Note: byte is 1 byte = 8 binary bits, 1 K = 1024.

 

Basic conversion of hexadecimal notation

  

    

 

Negative number base

  

 

Variable Introduction

JAVA data types mainly include the reference type and basic data type, of which there are 8 Basic Data Types

  

 

 

Basic variable demonstration

  

 

Description of Type escalation and forced conversion

  

The results are respectively 97 and 98, which are actually the relationship tables corresponding to binary and text in life, namely, the ASCII encoding table, and vice versa: System. out. println (char ('A' + 1), the output result is equal to B, where the number in the encoding table corresponding to B is 98.

 

Type Operation Details

1 class demotest00001015 2 {3 public static void main (String [] args) 4 {5 // int x = 3; 6 // byte B = 5; 7 // x = x + B; 8 // System. out. println ("x =:" + x); 9 10 // byte B = 3; 11 // B = (byte) (B + 200); 12 // System. out. println ("B =:" + B); // The output result is-53, and the forced type conversion is 13 14 // System. out. println ('you' + 0); // The output result is 20320, that is, the relationship between the unicode International encoding table is 15 16 bytes B = 4; 17 // B = 3 + 7; 18 // System. out. println ("B =:" + B); // output Result: 1019 // byte b1 = 3; 20 // byte b2 = 7; 21 // B = b1 + b2; 22 // System. out. println ("B =:" + B); // an error is reported during compilation. int conversion may result in loss of precision because b1 and b2 are variables and their values are changed, unable to judge; 23 // and above B = 3 + 7 is a fixed constant and can be judged, so the output result is equal to 10.24 25 int x; 26 int x1 = 10; 27 int x2 = 98; 28 x = x1 + x2; 29 System. out. println ("x =:" + x); // The output result is 108. Because x1 and x2 are of the int type by default, the final calculation result is still an integer, that is, the int type. 30 31 32} 33}

 

2.5 Operator

JAVA mainly contains the following six operators:

  

Arithmetic Operators

Note: Arithmetic Operators include: +-*/% (remainder, modulo operation) ++ (auto-increment: + 1 on the basis of the original data, and then assigned to the original data )--

  

Exercise:

1 int x = 6370; 2 3 x = x/1000*1000 4 5 System. out. println (x); // The output is 6000 6 7 System. out. println (5% 2); // The output result is 1 8 9 System. out. println (2% 5); // The output result is 210 11 System. out. println (3% 5); // The output result is 312 13 System. out. println (-5% 2); // The output result is-114 15 System. out. println (5%-2); // The output result is 116 17 System. out. println (5% 5); // The output result is 018 19. Note: Any number is connected to a string and the result is a string, for example, System. out. println ("AB" + 5 + 5); // The output result is ab5520 21 int a = 3; 22 23 a ++; // a = a + 1; 24 25 System. out. println (a); // The output result is 426 27 int a = 3, B; 28 29 B = a ++; 30 31 System. out. println ("a =" + a + ", B =" + B); // output result a = 4, B = 332 33 int a = 3, B; 34 35 B = ++ a; 36 37 System. out. println ("a =" + a + ", B =" + B); // output result a = 4, B = 438 39 int I = 3; 40 41 I = I ++; 42 43 System. out. println (I); // The output result is 3.

  

 

Value assignment operator

Note: It mainly includes: = + =-=/= * = % =

1 int a = 4; 2 3 a + = 2; // a = a + 2; 4 5 short s = 3; 6 7 s + = 4; // The output result is 78 9 // s = s + 4; // an error is reported when running the compilation, which may result in loss of precision due to the absence of strong conversion, s + = 4 can be automatically converted

 

 

Comparison Operators

  

 

Logical operators

Thinking: what is the use of logical operators?

Used to connect two boolean expressions, for example, 2 <x <5, x> 2 & x <5;

 

& Symbol operation features:

True & true = true;

True & false = false;

False & true = false;

False & false = false;

& Symbol operation rules:

& If either operator is false, the result must be false. If both operators are true, the result is true.

 

| Features of symbol operation:

True | true = true

False | true = true

True | false = true

False | false = false

| Symbol operation rule

| If either side of the operator is true, the result must be true. If both sides are false, the result is false.

  

^: Unique or symbol operation features:

True ^ true = false

False ^ true = true

True ^ false = true

False ^ false = false

  

^ Symbol operation rules:

^ If the two sides of the symbol have the same results, the result is false. If the two sides have different results, the result is true.

 

! : Non-operator to determine the other side of a thing

! True = false;

! False = true;

!! True = true;

 

& Features of symbol operation:

The calculation result is the same as that of &, but the calculation process is slightly different.

& No matter what the operation result is on the left, the right side is involved in the operation. & when the left side is false, the right side is not involved in the operation.

 

| Features of symbol operation:

And | the calculation result is the same, but the calculation process is slightly different.

| The right side of a symbol is involved in the operation regardless of the result on the left. | when the left side is true, the right side is not involved in the operation.

 

Bitwise operators

  

<Equivalent to a multiple of 2

> Equal to dividing by a multiple of 2

Shift n bits, that is, multiply by or divide by the n power of 2.

Illustration

  

  

 

Ternary Operators

Definition: in simple terms, it is the symbol of three elements involved in the operation.

  

Demo:

1 class OperateDemo 2 {3 public static void main (String args []) 4 {5 int x = 0, y; 6 y = (x> 1 )? 100:200; 7 System. out. println (y); // The output result is 200 8 9 // get the larger integer 10 int a, B; 11 int max = (a> B) of the two integers )? A: B; 12 13 // obtain the large integer 14 int o, p, q; 15 int temp = (o> p) among the three integers )? O: p; 16 int max2 = (temp> q )? Temp: q; 17} 18 19}

 

Exercise:

  

1. directly operate binary bits or use shift operations (2 <3)

  

 

2,

1 class OperateTest 2 {3 public static void main (String [] args) 4 {5 // the most efficient way to calculate 2 multiplied by 8 equals to 6 systems. out. println (2 <3); 7 8 // swap the values of Two integer variables (no third-party variables are required) 9 int a = 3, B = 5; 10 System. out. println ("a =" + a + ", B =" + B); 11 12 // used during development, in the form of a third-party variable, strong Readability: 13 // int c; 14 // c = a; 15 // a = B; 16 // B = c; 17 // System. out. println ("a =" + a + ", B =" + B); 18 19 20 // This method is not recommended. If the values of the two integers are too large, it will be beyond the int range and will be forced to convert and the value will change. 21 // a = a + B; // a = 3 + 5 = 8; 22 // B = a-B; // B = 3 + 5-5 = 3; 23 // a = a-B; // a = 3 + 5-3 = 5; 24 // System. out. println ("a =" + a + ", B =" + B); 25 26 // 27 a = a ^ B During the interview; // a = 3 ^ 5; 28 B = a ^ B; // B = (3 ^ 5) ^ 5; B = 3; 29 a = a ^ B; // a = (3 ^ 5) ^ 3; a = 5; 30 System. out. println ("a =" + a + ", B =" + B); 31 32} 33}

 

2.6 statement

 

2.7 Functions

 

Array 2.8

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.