Java Variables _ Basic article

Source: Internet
Author: User

1. Name of the variable:

General rules for variable naming:

1. Letters, Numbers, "$" or "_" characters
2. Cannot start with a pure number
3. Strictly case-sensitive
4. Cannot use Java reserved words

1        //Java Basics Eight Types2         //int type, only integers can be entered3         intx1=10;4         //Short type, only integers can be entered5          Shortx2=10;6         //The value range of byte and short is small, and the range of long is too large,7         //occupy more space, basically int can meet our daily calculations,8         bytex3 = 1;9         //Char can only point to one character (English, Chinese, punctuation, Japanese, and other languages and escape characters)Ten         //such as: A but AB this multi-character is not possible; One         Charx4= ' A '; A         //float x5 = 12.5F; you need to add F or F after the value -         floatx5=12.5f; -         //a long type that can not only enter integers, but also add letters the         Longx6=12l; -         //The Boolean type has only two results, one of which is true,2 is false -         BooleanX7 =true; -         //double (dual precision), float (single precision) +         Doublex8 =1.1;  -         //The result of the operation of Byte and short and char is that the data is automatically converted to int type by default; +         intii1=x1+x2+x4;

2. Eight types of categories:

The base data type is passed as a value, string is passed as a value, and the other is delivered as an address

Integer: A byte short int long is an integer representing the

Number of characters
Byte 1
Short 2
Int 4
Long 8

The value range of byte and short is small, and the range of long is too large, occupy more space, basically int can satisfy our daily calculation,

and int is also the most used integral type. In general, if there is an integer number such as 35 in Java, then this number is int type,

If we want it to be a byte type, you can add a b:35b to the data, indicating that it is of type Byte,

The same 35S means short type, 35L means long, int we can do nothing,

However, if you want to represent a long type, be sure to add "L" after the data.

float: float double denotes floating point type, except for different precision

Number of characters
Float 4
Double 8

The double type is larger and more accurate than the float type, so the usual floating-point data is double in the case of non-declaration.

If you want to indicate that a data is float type, you can add the "F" face after the data.

The floating-point data is not completely accurate, so sometimes it is normal to have floating in the last few decimal places when calculating.

Character type: Char

The data type used to hold characters, 2 bytes, Unicode encoded, and ASCII compatible with the first 128 bytes of encoding
Character storage range in \u0000~\uffff, when defining the character type of data should pay attention to ", such as ' 1 ' means the character ' 1 ' instead of the value 1,
char c = ' 1 ';
We try to output C and see, System.out.println (c); The result is 1, and if we output it like this System.out.println (c+0);
It turns out to be 49.
If we define C like this, look at
char c = ' \u0031 '; The result of the output is still 1, because the character ' 1 ' corresponds to the Unicode encoding that is \u0031

Boolean Type: Boolean


This type has only two values, True and False (true and non-true).

Java Variables _ Basic article

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.