2 _ basic data types and operators

Source: Internet
Author: User
Tags case statement

Literal:
1. The integer literal is an integer int.
2. The small numeric area is an integer double Floating Point. Therefore, when assigning a float value, you must add an F at the end to indicate that this is a float decimal point.

Shows Common Data Types in java:

Data Type table number range relationship:

Byte <short <int <long <float <double

If the value does not exceed the data type range, you can assign values to byte and short using integers.
Pay attention to the numerical expression range of each data type during forced type conversion (in the same way as the C language ).

Next, let's take a look at the value assignment method for each data type variable.

Public class Test {public static void main (String argc []) {boolean B = true; // declare a boolean variable and assign a value; optional values: true and false; the default value is false byte byte1 = 1; // declare the byte type variable byte, range:-127 ~ 128, the default value is 0 short s = 1; // declare the short type variable s, range:-32768 ~ 32767 int I = 1; // declare int variable I, range:-2147483648 ~ 2147483647, the default value is 0 long l = 11; // declare the long type variable l, the default value is 0L float f = 1.1f; // declare float, pay attention to the final f, the default value is 0F double d = 1.11. // declare the double variable d. The default value is 0D char c = 'C'. // declare the char variable C, range: '\ u0000 '~ '\ Uffff'. The default value is \ u0000 System. out. println ("B =" + B); // outputs the values of each data type. system. out. println ("l =" + l); System. out. println ("f =" + f); System. out. println ("d =" + d); System. out. println ("c =" + c); System. out. println ("byte1 =" + byte1); System. out. println ("s =" + s );}}

 

The values of each data type are as follows:

1 public class PrimitiveTypeTest {2 public static void main (String [] args) {3 // byte 4 System. out. println ("basic type: byte binary digits:" + Byte. SIZE); 5 System. out. println ("Packaging class: java. lang. byte "); 6 System. out. println ("minimum value: Byte. MIN_VALUE = "+ Byte. MIN_VALUE); 7 System. out. println ("maximum value: Byte. MAX_VALUE = "+ Byte. MAX_VALUE); 8 System. out. println (); 9 10 // Release 11 System. out. println ("basic type: short binary digits:" + Short. SIZE); 12 System. out. println ("Packaging class: java. lang. short "); 13 System. out. println ("minimum value: Short. MIN_VALUE = "+ Short. MIN_VALUE); 14 System. out. println ("maximum value: Short. MAX_VALUE = "+ Short. MAX_VALUE); 15 System. out. println (); 16 17 // int18 System. out. println ("basic type: int binary digits:" + Integer. SIZE); 19 System. out. println ("Packaging class: java. lang. integer "); 20 System. out. println ("minimum value: Integer. MIN_VALUE = "+ Integer. MIN_VALUE); 21 System. out. println ("maximum value: Integer. MAX_VALUE = "+ Integer. MAX_VALUE); 22 System. out. println (); 23 24 // long25 System. out. println ("basic type: long binary digits:" + Long. SIZE); 26 System. out. println ("Packaging class: java. lang. long "); 27 System. out. println ("minimum value: Long. MIN_VALUE = "+ Long. MIN_VALUE); 28 System. out. println ("maximum value: Long. MAX_VALUE = "+ Long. MAX_VALUE); 29 System. out. println (); 30 31 // float32 System. out. println ("basic type: float binary digits:" + Float. SIZE); 33 System. out. println ("Packaging class: java. lang. float "); 34 System. out. println ("minimum value: Float. MIN_VALUE = "+ Float. MIN_VALUE); 35 System. out. println ("maximum value: Float. MAX_VALUE = "+ Float. MAX_VALUE); 36 System. out. println (); 37 38 // double39 System. out. println ("basic type: double binary digits:" + Double. SIZE); 40 System. out. println ("Packaging class: java. lang. double "); 41 System. out. println ("minimum value: Double. MIN_VALUE = "+ Double. MIN_VALUE); 42 System. out. println ("maximum value: Double. MAX_VALUE = "+ Double. MAX_VALUE); 43 System. out. println (); 44 45 // char46 System. out. println ("basic type: char binary digits:" + Character. SIZE); 47 System. out. println ("Packaging class: java. lang. character "); 48 // convert Character into a numeric value instead of a Character. MIN_VALUE is output to the console 49 System. out. println ("minimum value: Character. MIN_VALUE = "+ (int) Character. MIN_VALUE); 50 // convert Character in the numeric form instead of the Character form. MAX_VALUE is output to the console 51 System. out. println ("maximum value: Character. MAX_VALUE = "+ (int) Character. MAX_VALUE); 52} 53}

 

Operator

Common and non-equal operators are similar to the C language. I will not talk about them any more. Here we will focus on the similarities and differences between logic and conciseness.

Logic and <----> short circuit and (or conciseness and)
& <----> &&

Similarities: all operations are logical operations, results are compared, and results are logical values.

Differences:
1. & the relations on both sides are compared,
2. When & is used, if the previous expression is already false, the following expression is no longer judged. Improved efficiency.

Eg:

1 int I = 5; 2 boolean j = I> 5 & I ++ = 5 // j = false, I = 6; // & both sides of the expression must judge 3 boolean k = I> 5 & I ++ = 5 // j = false, I = 5; // & the expression on the left is false, so it is not judged on the right.

Switch ...... Case statement

The I value in switch (I) can only be char, byte, short, int.
Under normal circumstances, a break statement must be added at the end of each case statement to exit the current switch.
Note: If the break statement is ignored after the case statement that meets condition I, all the statements after the case statement are considered as sequential statements for one-by-one execution, and no longer determine whether the execution conditions are met, the current switch is exited only when the next break statement is encountered.

Similarly, if ...... Elseif ...... Elseif ...... After an if statement that meets the conditions is run, the later if statement will not be executed.

 

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.