Java data types, just look at this one

Basic data types

	基本数据类型表示简单的数据,基本数据类型分为4大类,共有8种数据类型。
  • Integer types: byte, short, int, long
  • Floating-point types: float, double
  • Character types: char
  • Boolean type: boolean

The basic data types as shown below, where the integer type, float type, the character is numeric types are interchangeable between them
Here Insert Picture Description

Integer type

The integer type comprises java byte, short, int, long, in fact, only the difference between them and that the ranges of different widths.

Integer type width
byte 1 byte (8 bits)
short 2 bytes (16 bits)
int 4 bytes (32-bit)
long 8 bytes (64 bits)

The default language Java integer type is an int, long after the need to add l / L. For example, try

package java_hw;

public class HelloWorld {

	public static void main(String[] args) {
	//输出一个默认整数常量
	System.out.println("默认整数常量(int) = " + 7);
	byte a = 7;
	short b = 7;
	int c = 7;
	long d = 7L;
	long e = 7l;
	
	System.out.println("byte = " + a);
	System.out.println("short = " + b);
	System.out.println("int = " +c);
	System.out.println("long = " + d);
	System.out.println("long = " + e);
	}
}

Floating-point type

For storing decimal floating-point type, may be too large to store an integer range, it is divided into floating-point (float) and double precision floating point Double), double precision floating point number is larger than memory, and may represent the range of values accuracy as well.

Floating-point type width
float 4 bytes (32-bit)
double 8 bytes (64 bits)

Representation of value

1. progress of numerical representation

If a scalar integer assignment, use binary, octal, hexadecimal, which is represented as follows

  • Binary: at 0 b / B prefixed
  • Octal: 0 prefixed
  • Hexadecimal: 0 x / X as a prefix

Example (expressed as follows with several integer int 28)

int decimaInt = 28;
int binaryInt1 = 0b11100;
int binaryInt2 = 0b11100;
int octalInt = 034;
int hexadecimalInt1 = 0x1c;
int hexadecimalInt2 = 0X1c;
2. Index type

Mathematical calculations tend to use the value of the index indicated. If index decimal notation, you need to use e / E represents power, e2 represents 2 10 ^

double myMemory = 3.33e2;
double freeSpaceMemory = 3.33e-2;

Represents 2 wherein myMeory power, freeSpaceMemory represents a negative quadratic 3.33 10 3.33 10

3. Character Types

Character type character constant represents a single character, a character type declared in Java char, Java must be a single character enclosed in single quotes, as follows

char a = 'B';

Java byte Unicode characters are encoded, two bytes (16 bits), and thus can be used in the form of coded hexadecimal (unsigned) indicated that their manifestations are \ UN, where n is a 16-digit hexadecimal number , so the 'A' may be the character '\ u0041' represents a Unicode
Example
Here Insert Picture Description

In Java, in order to express some special character preceded by the backslash (\), which is called the escape character, common escape character is shown below

Character representation Unicode encoding Explanation
\t \ u0009 Horizontal tab tab
\n \u000a Wrap
\r \ u000d Enter
\" \ U0022 Double quotes
\’ \ U0027 apostrophe
\\ \u005c Backslash
4. Boolean

Statement Boolean keyword in the Java language is a boolean, it has only two values: true and false

The value type of conversion

5.1 automatic type conversion
automatic type conversion is the need to convert between types is automatic, not to take other means, the general principle is a small range of data types can be automatically converted to a wide range of data types. Sequence type data conversion as shown below, from under the total dynamic completed.
Here Insert Picture Description
Automatic type conversion not only in the evaluation process, the automatic type, turned also occur during mathematical calculations, the operation is often the first call type to the same type, and then, in calculations. Calculation Rules in the following table

Type Operand 1 Type Operand 2 Type after conversion
byte、short、char int int
byte、short、char、int long long
byte、short、char、int、long float float
byte、short、char、int、long、float double double

Case

public static void main(String[] args) {
		//声明整数变量
		byte byteNum = 16;
		short shortNum = 16;
		int intNum = 16;
		long longNum = 16;
		
		//byte类型转化为int类型
		intNum = byteNum;
		//声明char变量
		char charNum = '能';
		//char类型转化为int类型
		intNum = charNum;
		
		//声明浮点变量
		//long转化为float类型
		float floatNum = longNum;
		//float类型装华为double类型
		double doubleNum = floatNum;
		
		//表达式最后计算类型是double;
		double result = floatNum * intNum + doubleNum * shortNum;
	}

This expression is in turn composed of four different data types, the largest scope of things double, so in the calculation process them first into double, so the final result is double type
5.2 mandatory type conversion
during the conversion value types, in addition to plus "target type" outside before implementing automatic type conversion, and sometimes need to cast, mandatory variable or variables, the following example code:

//int型变量
int i = 10//把int型变量强制转化成byte
byte b =byte)i;

The code (byte) i casts achieve expression. Casts primarily used to convert a large width for the type of case where a small-width type, such as the int into byte. Example code:

//int型变量
int i = 10//把int变量i强制转换为byte;
byte b = (byte)i;
Published 19 original articles · won praise 85 · views 1341

Guess you like

Origin blog.csdn.net/qq_45828877/article/details/103337029