First Understand java variables, data types and operators (1), first Understand java

Source: Internet
Author: User

First Understand java variables, data types and operators (1), first Understand java

Goal:

1. Understand the concept of Variables

2. Introduction ---- common data types are used

As we all know, every computer has an essential element of memory. What is the memory used? Actually, the computer memory is equivalent to the human brain. When processing data, the computer will use the memory to remember the data used for a large amount of computation. When there is memory data, A piece of memory will be allocated to the data, and a name will be defined based on the data. Giving him a name will help the memory better remember the data, it is like giving a name to a person so that we can better remember this person,This is the variable name!Then, the computer will classify the data based on the data usage and define a type for the data. For example, I saved a contact phone number on my mobile phone, but I don't know what this person did, I will add a category in front of it,This is the data type of the program!For example, if I want to save and save 1000 yuan, then 1000 is the amount of the money, right? Then 1000 is the value of the money ),This is the value of the variable!Humans are really a great creature. They invented this memory. Haha...

3. Question --- java Data Type

So how to define the data type in the program? Java defines many data types in different categories and divides them into different data types based on their purposes. Commonly used:

1. int

2. double

3. char

4. String

As explained above, I have defined a type that actually occupies memory. But what? Different data types occupy different memory sizes. Therefore, we need to use data tailored to avoid Memory waste. Here is a good article about the details: memory a = 3; string B = "d"; char c = 'C'; double v = 0.55; For more information, see [original] basic java data types and default memory usage.

Read 245 comments 0

JAVA 8 Basic Data TypesThe core of a program is a series of data, or program status. To facilitate data management, a computer sets a set of data types, in this way, it is convenient to allocate memory for data and operate data. This is the source of the data type. In fact, there are also various data types in real life, such as the number type, numeric type, and so on, the number type can be divided into integer type and decimal type, but there is no division. The most common and basic data type is the basic data type. These types of values can be used to represent some simple states. Eight basic data types: from small to big (byte-short-char-int-long-float-double-blooean)

Boolean, byte, char, double, float, long, int, short

3.5.1 OverviewTo learn data types, you need to master the characteristics of each data type and the corresponding detailed knowledge, which will help you select the type. Therefore, when learning for the first time, you need to remember a lot of content. There are a total of eight basic data types in Java language. The following are four categories divided by purpose: l Integer type: byte (byte type), short (short Integer type), int (integer type), long (long Integer type) l decimal type: float (single precision floating point type), double (double precision floating point type) l Boolean l 3.5.2 Integer typeAn integer represents an integer. When you need to represent an integer value, you can select the appropriate four types as needed. If there are no special requirements, you generally select the int type. The difference between the four integer types mainly lies in the size of the space occupied by each data in the memory and the value range. For details, see the table below: an integer parameter table with 8 bits in memory
Type name Keywords Occupied Space (bytes) Value Range Default Value
Byte type Byte 1 -27-27-1 0
Short integer Short 2 -215-215-1 0
Integer Int 4 -231-231-1 0
Long Integer Long 8 -263-263-1 0
Note: 1. integers in Java are all signed numbers, that is, positive and negative integers. 2. The default value is automatically initialized under specific circumstances. The specific situation will be described later. 3. the integer values in the program are of the int or lower types by default. To write a long value, you must add the letter L after the value, which is case-sensitive. 3.5.3 decimal typeA decimal type is a type that represents a small number. To represent a decimal number, you can select the following two types as needed. If there are no special requirements, the double type is generally used. Since decimals are stored in different ways than integers, decimals have certain precision, so they are not accurate enough in computer operations. Based on the precision and storage interval, two decimal types are designed. For details, see the following table: decimal parameter table.
Type name Keywords Occupied Space (bytes) Value Range Default Value
Single precision floating point type Float 4 -3.4E + 38-3.4E + 38 0.0f
Dual-precision floating point Double 8 -1.7E + 308-1.7E + 308 0.0
Note: 1. The value range is described in scientific notation. 2. In a program, the decimal operation speed must be lower than the integer operation speed. 3. float decimal places, which must be followed by a letter f. The case sensitivity is acceptable, for example, 1.01f. 3.5.4 accept typeCharacter represents a specific character. According to the previous knowledge, the computer stores the characters in the form of character sets. Therefore, the value of character type is actually only the number in the character set, not the character actually represented, the computer converts numbers to corresponding characters. Java uses the Unicode character set as the default character set to make it easier to internationalize. This character set contains common characters in various languages. In program code, characters are identified by A pair of single quotes plus characters to be expressed, such as 'A' and 'A'. Of course, character encoding can also be used directly, that is, a non-negative integer.

A Chinese Character occupies 2 bytes (for Chinese characters, gbk encoding occupies 2 bytes and utf8 encoding occupies 3 bytes), and a letter occupies 1 byte

Complex parameter table
Type name Keywords Occupied Space (bytes) Value Range Default Value
Character Type Char 2 0 to 216-1 0
Note: 1. Serial numbers do not contain negative numbers. 2. Attention-type numbers are stored, so they can be involved in mathematical operations. 3. the struct type can be used as an unsigned integer in Java. 4. The default value of character type is 0 rather than 0. 3.5.5 BooleanBoolean represents the true or false logic. True indicates true in Java, and false indicates false. Boolean is the type used to store logical values. In fact, many programs have the concept of logical values. Java uses Boolean to express logical values. Boolean parameter table
Type name Keywords Occupied Space (bytes) Value Range Default Value
Boolean Boolean   True or false False
Note: 1. the space occupied by the Boolean Type depends on the implementation of the Java Virtual Machine (JVM), which may be 1-bit or 1-byte. 3.5.6 SummaryThis section briefly introduces the basic features of the eight basic data types. In actual programming, you can select the corresponding types as needed. All numeric variables in JAVA are signed (positive and negative.

JAVA does not allow arbitrary type conversion. Only numeric variables can be converted, but not random. The numeric variables with decimal points are double by default. The float type must be forcibly converted.

Related 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.