<java Basics > Basic Data Types <3>

Source: Internet
Author: User

There are 8 basic data types in Java that store numeric values, characters, and Booleans.
Basic data types: numeric, character, Boolean.
Numeric type: integer type (byte, short, int, long), floating-point type (float, double).
Character type (char). The Boolean type (Boolean).

the basic data type (////* These two symbols represent comments):
Public class test{//Create Class
Public static void Main (string[] args) {//Main method
                  /**
* Integer data is divided into Byte, short, int, and long,4 types, which have different range of values depending on the size of the memory it occupies.
* Byte data type: (Memory space (8 bits equals 1 bytes): 8 bits, Value range: -128~127)
* Short Data type: (Memory space (8 bits equals 1 bytes): 16 Bits, Value range: -32768~32767)
* int Data type: (Memory space (8 bits equals 1 bytes): 32 bits, Value range: -2147483648~2147483647)
* Long Data type: (Memory space (8 bits equals 1 bytes): 64 bits, Value range: -9223372036854775808~9223372036854775807)
* In defining the above 4 types of variables, pay attention to the range of variables, beyond the corresponding range will be wrong.
* For long values, if the assigned value is greater than the maximum value of type int or less than the minimum value of type int, you need to add L after the number to indicate that the number is a long integer.
* such as: long num = 2147483650L;
                   */
byte MyByte = 125; Declaring a byte variable and assigning a value
Short myshort = 32666; Declaring a short variable and assigning a value
int myint = 2147483644; declaring an int type variable and assigning a value
long mylong = 2147483650L; Declaring a long variable and assigning a value
                  /**
* Floating-point types represent numbers with decimal parts, floating-point types in the Java language are single-precision floating-point types (float) and double-precision floating-point types (double)
* Float Data type: (Memory space (8 bits equals 1 bytes): 32 bits, Value range: 1.4e~3.4028235e38)
* Double Data type: (Memory space (8 bits equals 1 bytes): 64 bits, Value range: 4.9e~1.7976931348623157e308)
* Decimals are considered double by default, and if you use float decimal, you need to add F or F after the decimal.
* plus the suffix D or D indicates that this is a double type data, without "D" and no error. such as: double MyDouble = 1234.12;
* But if you do not add "f" when declaring a float variable, the system will assume that the variable is a double type and an error occurs.
                   */
float myfloat = 13.23f; Declaring a float type variable and assigning a value
double mydouble = 1234.12d; Declaring a double type variable and assigning a value
                  /**
* Character types are used to store a single character.
* Char data type: (Memory space (8 bits equals 1 bytes): 16 bits)
* When defining a character type variable, enclose it in single quotation marks, such as ' a ' for a character.
* Instead of double quotation marks, "a" represents a string, although there is only one character, but because of the use of double quotation marks, it still represents a string, not a character.
* PS: The escape character is a special character variable that begins with a backslash "\" followed by a character. For the escape character please Baidu =w= yourself.
                   */
char MyChar = ' a '; Declaring a char type variable and assigning a value
char MyChar = 97; Since the sort position of character A in the Unicode table is 97, it allows the statement to be written like this
                  /**
* Boolean type is also called the logical type, the boolean type variable is defined by the keyword Boolean, the Boolean type is only true and false two values, respectively, represents the cloth
In the logic of "true" and "false", Boolean types are often used in process control as a condition of judgment.
                   */
Boolean myboolean = true; Declaring a Boolean variable and assigning a value
         }
}


<java Basics > Basic Data Types <3>

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.