JAVA basics: getting started with Data Types

Source: Internet
Author: User
JAVA basics: the data type of the Getting Started tutorial-Linux general technology-Linux programming and kernel information. For details, refer to the following section. § 2. 1 Data Type

The data type specifies the status and behavior of a variable or expression. Java
The data types are as follows:

Java does not support pointer, struct, and shared body types in C and C ++.

This chapter mainly introduces simple types.

§ 2. 2 constants and variables

I. Constants

The constant value in Java is represented by a string, which is differentiated into different types.
Such as integer constant 123, real constant 1.23, character constant 'A', Boolean constant true,
False and the String constant "Thisisaconstantstring .".

Unlike C and C ++, Java cannot use the # define command to set an identifier.
Is defined as a constant, but is implemented with the keyword final, such as finaldoublePI = 3.14159 (
For usage of final, refer to [6.2.3]).

Ii. Variables

A variable is a basic storage unit in a Java program. Its definition includes the variable name.
Variable type and scope.

① A variable name is a valid identifier, which contains letters, numbers, and lower strokes.
The sequence of line or dollar sign "$". Java is case sensitive to variable names, and variable names cannot
It must start with a number and cannot be a reserved word. Valid variable names include: myName,
Value-1 and dollar $. Invalid variable names such as: 2 mail, room #, class (Reserved
Variable name must have a certain meaning to increase the readability of the program.

② The variable type can be any data type mentioned above.

③ The scope of the variable indicates a piece of code that can access the variable. Declare
Variable also specifies the scope of the variable. By scope, variable
There can be the following types of parameters: local variables, class variables, method parameters, and exception handling parameters.
Number. Local variables are declared in a code block of a method or method, and their scopes
The code block where it is located (a piece of code in the whole method or method ).

Class variables are declared in the class, rather than in a method of the class.
The scope is the entire class.

The method parameter is passed to the method, and its scope is this method.

The exception processing parameter is passed to the Exception Processing code. Its scope is an example.
External Processing Section.

In a definite domain, the variable name should be unique. Generally, a domain
Use braces.

The variables, parameter passing, and exception handling methods are described in [6.7.1],
[6.2.4] and chapter 8.

④ The Declaration format of the variable is:
Typeidentifier [= value] [, identifier [= value]…];

Example: inta, B, c;
Doubled1, d2 = 0.0;

Multiple variables are separated by commas. d2 = 0.0 assigns an initial value to the Real Variable d2.
0.0. Only local variables and class variables can assign initial values in this way, while method parameters
And the variable values of the exception handling parameters are provided by the caller.

§ 2. 3 Integer Data

1. Integer constants:

Similar to C and C ++, Java has three forms of integers:

① A decimal integer, such as 123,-, 0

② An octal integer starting with 0. For example, 0123 indicates the number of decimal places 83, and-011 indicates 10.
Hexadecimal number-9.

③ A hexadecimal integer starting with 0x or 0X. For example, 0x123 indicates the decimal number.
291,-0X12 indicates the decimal number-18.

An integer constant occupies 32 bits in the machine and has an int value. For a long value
Add L or l after the number. For example, 123L indicates a long integer, which occupies 64 digits in the machine.

Ii. integer variables:

The types of integer variables include byte, short, int, and long. The following table lists
The number of bits in each type of memory and its representation range.

(To be continued)

Int type is the most commonly used Integer type. It indicates that the data range is large enough and is suitable for 32-bit,
64-bit processor. However, for large-scale computation, a large integer is usually used, which is beyond the range indicated by the int type.
Long type.

Because different machines store multi-byte data in different ways, it may be from low-byte to high-byte storage
It may be from high-byte to low-byte storage. In this way, when analyzing network protocols or file formats
The byte type is suitable for expressing data. Generally
The range is small, which may easily cause overflow and should be avoided.

The short type is rarely used. It limits the storage of data to the first high byte, and then the lower byte.
Error.

Iii. Definitions of integer variables, such:

Byteb; // specify the variable B as the byte type.
Shorts; // specify the variable s as the short type.
Inti; // specifies that the variable I is of the int type.
Longl; // specify the variable l as the long type.

§ 2. 4 floating point (real) data

1. Real Constants

Similar to C, C ++, Java has two real constants:

① In decimal format, which consists of digits and decimal places, and must have a decimal point, such as 0.123,. 123,123., 123.0

② Scientific notation form. For example, 123e3 or 123E3, where e or E must have numbers before, and e or the following index must
Is an integer.

The real constant occupies 64 bits in the machine and has a value of the double type. For a float value, add f or F after the number, such
12.3F, which occupies 32 places in the machine and indicates low accuracy.

Ii. Real Variables

There are two types of real variables: float and double. The following table lists the memory occupied by these two types and their table examples.
Perimeter.

The number of digits occupied by the Data Type
Float323.4e-038 ~ 3.4e + 038
Double641.7e-308 ~ 1.7e + 308

Double type has higher precision and greater range than single precision float, which is often used.

Iii. Definitions of real variables, such

Floatf; // specify the variable f as the float type.

Doubled; // specify the variable d as double.

[Note] Unlike C and C ++, Java does not have unsigned integers, and it clearly specifies the values occupied by integer and floating point data.
The number of memory bytes ensures the security, robustness, and platform independence.

§ 2. 5 balanced data

I. character constants

A character constant is A character enclosed in single quotes, such as 'A' and 'A '. In addition, Java also provides
Characters starting with a backslash (\). The following table lists escape characters in Java.

Unlike C and C ++, the bytes data in Java is a 16-bit unsigned data that represents a Unicode set, not just
ASCII set. For example, \ u0061 indicates 'a' of the ISO Latin code '.

Escape Character Description
\ Ddd1 to three octal data characters (ddd)
\ Uxxxx1 to 4 hexadecimal characters (xxxx)
\ 'Single quotes
\ Backslash character
\ R press ENTER
\ N line feed
\ F go to paper form
\ T horizontal hop
\ B Return

Ii. Balanced Variables

The character type is char, which occupies 16 digits in the machine and ranges from 0 ~ 65535. The definition of the variable of the struct type is as follows:
:

Charc = 'a'; // specify the variable c as char type, and assign the initial value as 'A'

Unlike C and C ++, the string type data in Java cannot be used as integers, Because Java does not provide the unsigned integer type. However
It can also be operated as an integer.

For example:

Intthree = 3;

Charone = '1 ';

Charfour = (char) (three + one); // four = '4'

In the preceding example, when the addition is calculated, the one variable is converted to an integer, added, and the result is converted to a word.
Character type.

3. string constants

Similar to C and C ++, Java's string constants are a string of characters enclosed by double quotation marks (""), such as "thisastring. \ n ". However
The difference is that the String constants in Java are processed as an object of the String class, rather than a data. Related
Class String, which is described in Chapter 7.

§ 2. 6 boolean data

Boolean data has only two values, true and false, and they do not conform to any integer. It is often used in flow control.
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.