Java Grammar Basics Look at this one, that's enough. __java

Source: Internet
Author: User

In recent years of popular development language rankings, Java has been ranked top. This not only depends on the Java itself has a lot of advantages to achieve a very good object-oriented theory, allows programmers to do sophisticated programming in a graceful way, and because Java has a better ecological environment, for example, it is also the development level of our common Android operating system (of course, the future is likely to be Kotlin replaced).
Anyway, to understand Java is necessary, let's comb the Java syntax knowledge. It covers data types, variables, constants, operators, selection statements, loop statements, arrays, classes, references, methods, and so on. This article hopes to summarize the way to explain, to facilitate future inspection of developers. 1. Data type

Unlike C + +, the data types in Java are classified as basic data types and reference data types. 1.1 Basic data Types

Java's basic data types are divided into four categories, a total of eight kinds, respectively: logical type (Boolean) character (char) integer (byte, short, int, long) floating-point type (float, double)

Where Boolean-type values have only true and false two logical values (cannot replace true and false with integers of 0 or not 0). The char type represents the amount of constants, which is a character enclosed in single quotes, such as char C1 = ' a ' or char C2 = ' i '. Because Java characters are encoded in Unicode, each character occupies two bytes.
Note: This means that char is two bytes in Java memory, but the number of bytes per character when saved to a file depends on the character encoding of the file. 】
Integer and floating-point types can be collectively referred to as numeric data types, and the range and storage size of various numeric data types are as shown in the following table.

type name Range Storage size
Byte -27 (-128) ~ 27-1 (127) 8-bit signed number
Short -215 (-32768) ~ 215-1 (32767) 16-bit signed number
Int -231 (-2147483648) ~ 231-1 (2147483647) 32-bit signed number
Long -263 ~ 263-1 64-bit signed number
Float Negative range: -3.4028235E+38 ~ -1.4E-45
Positive range: 1.4E-45 ~ 3.4028235E+38
32-bit, standard IEEE 754
Double Negative range: -1.7976931348623157E+308 ~ -4.9E-324
Positive range: 4.9E-324 ~ 1.7976931348623157E+308
64-bit, standard IEEE 754

Careful you can see that in addition to the Boolean type, the other seven types of data have a clear storage size, then the Boolean type in Java, how many bytes.
After testing found:

Boolean a=true; 
A Boolean of A is 4 bytes in the JVM, i.e. 32bit

boolean[] b = new BOOLEAN[10];
Each Boolean in the Boolean array B occupies 1 bytes in the JVM, or 8bit

In fact, there is a description in the Java Virtual Machine specification: "Although Boolean is defined as a data type, it provides very limited support." In a Java virtual machine, there is no byte-code directive for Boolean values, and a Boolean value that is manipulated by a Java-language expression is replaced with the int data type in the Java Virtual machine after compilation, and the Boolean array is encoded as B of the Java Virtual machine Yte array with a Boolean element of 8 bits per element.
So we can explain the test above, and we conclude that the Boolean type takes up 4 bytes in a single use, and 1 bytes in the array.
As to why Boolean type is 1 bytes or 4 bytes, it is actually a game between computational efficiency and storage space. For developers, we just remember that the Boolean value of Java can only be true or false. 1.2 Reference data Types

Java has five reference types (object types): Classes (Class), interfaces (interface), arrays (array), enumerations (enum), callouts (Annotation).
A class reference can be a class in the Java library, or it can be a class that you create yourself. Common classes in Java are: Object, String, Date, Void, and the eight basic data types mentioned above have corresponding encapsulated classes, as shown in the following table.

Basic data Types Encapsulation Class
Boolean Boolean
Char Character
Byte Byte
Short Short
Int Integer
Long Long
Float Float
Double Double

It should be noted that the arrays in Java are of reference type and that they create objects. For example: int[] A = new int[5];,a is an int-type array object.
(interfaces, enumerations, annotations are described in other articles.) ) 1.3 Data type conversions

(To be continued)

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.