Summary of Java Basic data types

Source: Internet
Author: User
Tags class definition ranges java reference

A primitive type, or a built-in type, is a special type that differs from a class in Java. They are the most frequently used types in our programming. Java is a strongly typed language, and the first declaration of a variable must describe the data type, and the first variable assignment is called the initialization of the variable.

1. Java's simple type and its wrapper class

There are eight basic types of Java, the basic type can be divided into three categories, character type char, Boolean type Boolean and numeric type Byte, short, int, long, float, double. Numeric types can also be divided into integer types, byte, short, int, long, and floating-point number type float, double. There are no unsigned numeric types in Java, and their range is fixed and will not change with the machine hardware environment or the operating system. In fact, there is another primitive type Void in Java, which also has a corresponding wrapper class java.lang.Void, but we cannot manipulate them directly. The 8 type representation ranges are as follows:

Byte:8 bits, the maximum amount of storage data is 255, the stored data range is between -128~127.

Short:16 bits, the maximum data storage is 65536, the data range is -32768~32767.

Int:32 bits, the maximum data storage capacity is 2 of the 32-bit minus 1, the data range is negative 2 of 31 times to positive 2 square minus 31.

Long:64 bits, the maximum data storage capacity is 2 of the 64-bit minus 1, the data range is negative 2 63 times to positive 2 square minus 63.

Float:32 bit, the data range in the 3.4e-45~1.4e38, the direct assignment must be in the number after the addition of F or F.

double:64 bit, data range in 4.9e-324~1.8e308, the assignment can be added D or D can also not add.

Boolean: Only True and false two values are taken.

Char:16 bits, storing Unicode codes, and assigning values in single quotes.

Java determines the size of each of the simple types. These sizes do not change as the machine structure changes. This immutable size is one of the reasons why Java programs have a strong ability to migrate. The following table lists the simple types defined in Java, the number of bits, and the corresponding wrapper classes.

Simple Type

Boolean

Byte

Char

Short

Int

Long

Float

Double

void

Number of binary digits

1

8

16

16

32

64

32

64

--

Wrapper class

Boolean

Byte

Character

Short

Integer

Long

Float

Double

Void

We do not need to force memory for the range of values of the base types of numeric types, because their values are already defined in the corresponding wrapper class as constants. Such as:

Base type Byte bits number: byte.size min: byte.min_value max: Byte.max_value

Basic type short bits number: short.size min: short.min_value max: Short.max_value

Basic type char bits number: character.size min: character.min_value max: Character.max_value

Basic type Double bits number: double.size min: double.min_value max: Double.max_value

Note: The minimum value of float, Double Two types is not the same as Float.min_value, Double.min_value, in fact Float.min_value and Double.min_value respectively refer to the The minimum number of positive numbers that float and double types can represent. That is, there is a case where the value between 0 and ±float.min_value, the Float type cannot be represented, and the value between 0 and ±double.min_value Double type cannot be represented. This is not surprising because the values in these ranges exceed their accuracy range.

The minimum and maximum values for float and double are output in the form of scientific notation, and the trailing "e+ number" indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14x1000=3140,3.14e-3 is 3.14/1000=0.00314.

Java primitives are stored in stacks, so they are accessed faster than instance objects stored in the corresponding wrapper class in the heap. Starting with Java5.0 (1.5), the Java Virtual machine (Java VM) can perform the basic types and the automatic conversion between their corresponding wrapper classes. So we use their wrapper classes in terms of assignment, parameter passing, and math, like using basic types, but that doesn't mean that you can call their wrapper class with a basic type to have a method. In addition, the wrapper classes for all basic types (including void) use final adornments, so we cannot inherit them to extend new classes or to override any of their methods.

Basic types of benefits: Data storage is relatively simple, efficient operation

Packaging class Advantages: Some easy, such as the set of elements must be object type, satisfies the Java all is the idea of the object

Constants in 2.Java

Hexadecimal integer constants: When in hexadecimal, you need to start with 0x or 0X, such as 0xff,0x9a.

Octal integer constant: octal must start with 0, such as 0123,034.

Long integer: Long integer must end with L, such as 9l,342l.

Floating-point constants: Because the default type of the decimal constant is double, you must add F (f) after the float type. The same variable with decimals defaults to the double type.

such as: float F;

f=1.3f;//must declare F.

Character constants: Literal constants are enclosed in two single quotes (note that string constants are enclosed in two double quotes). The characters in Java account for two bytes. Some of the commonly used escape characters:

①\r means accept keyboard input, equivalent to press ENTER;

②\n indicates a line break;

③\t represents a tab, equivalent to the table key;

④\b means backspace key, equivalent to back space key;

⑤\ ' denotes single quotation marks;

⑥\ ' means double quotation marks;

⑦\\ represents a slash \.

3. Conversion between data types

1). There are two ways to convert a simple type of data: auto-conversion and casting, which typically occurs in an expression or when a method's arguments are passed.

Automatic conversion

Specifically, when a more "small" data with a more "large" data, the system will automatically convert "small" data into "big" data, and then the operation. In the case of a method call, the actual parameter is "small", and the method called by the form parameter data is more "large" (if there is a match, of course, will call the matching method directly), the system will automatically convert "small" data into "big" data, and then the method calls, naturally, for multiple overloaded methods of the same name, will be converted to the most " Close the "big" data and make the call. These types are from "small" to "large" respectively (Byte,short,char)--int--long--float-double. What we call "big" and "small" here is not the amount of bytes, but the size of the range that represents the value.

① The following statements can be passed directly in Java:

byte B;int i=b; Long l=b; float f=b; Double d=b;

② if the low-level type is char, converting to the advanced type (int) is converted to the corresponding ASCII value, for example

Char c= ' C '; int i=c;

System.out.println ("Output:" +i); output: output:99;

③ for the Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and can use the following coercion type conversions.

Short i=99; Char c= (char) i; System.out.println ("Output:" +c); output: output:c;

Forced conversions

When converting "large" data to "small" data, you can use forced type conversions. That is, you must use the following statement format: int n= (int) 3.14159/2; As you can imagine, this conversion is sure to cause a drop in overflow or precision.

2) The data type of the expression is automatically promoted, and the following rules are noted for automatic elevation of the type.

① all the values of the Byte,short,char type will be promoted to the int type;

② if one operand is long, the result is a long type;

③ if one of the operands is a float type, the result is a float type;

④ if one of the operands is double, the result is a double type;

example, byte B; b=3; B= (Byte) (b*3);//must declare byte.

3) Package Transition type Conversion

In general, we declare a variable first, and then generate a corresponding wrapper class, you can use the wrapper class of various methods for the type conversion. For example:

① when you want to convert the float type to double type:

float f1=100.00f;

Float f1=new Float (F1);

Double D1=f1.doublevalue ();//f1.doublevalue () is a method of returning a double value type for the float class

② when you want to convert a double type to an int type:

Double d1=100.00;

Double D1=new double (D1);

int I1=d1.intvalue ();

A simple type of variable is converted to the appropriate wrapper class, which can take advantage of the wrapper class's constructor. That is: Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value

And in each packing class, the total tangible is xxvalue () method, to obtain its corresponding simple type data. This method can also realize the conversion between different numerical variables, for example, for a double-precision real class, intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double-precision real variable.

4) conversion between strings and other types

Conversion of other types to strings

① the string conversion method of the Calling class: X.tostring ();

② Automatic conversion: x+ "";

③ method of using String: string.volueof (X);

string as a value, to other types of conversions

The ① is converted to the corresponding wrapper instance before the corresponding method is converted to another type.

For example, the character "32.1" converts the value of a double type to the format: New Float ("32.1"). Doublevalue (). can also be used: double.valueof ("32.1"). Doublevalue ()

② static Parsexxx method

String s = "1";

byte B = byte.parsebyte (s);

Short T = Short.parseshort (s);

int i = Integer.parseint (s);

Long L = Long.parselong (s);

Float f = float.parsefloat (s);

Double d = double.parsedouble (s);

③character getnumericvalue (char-ch) method

5) Conversion of date class to other data types

There is no direct correspondence between the integer and date classes, except that you can use the int to represent the year, month, day, time, minute, and second, thus establishing a correspondence between the two, in which you can use the three forms of the date class constructor:

①date (int year, int month, int Date): type int for years, months, and days

②date (int year, int month, int Date, int hrs, int min): int, month, day, time, minute

③date (int year, int month, int Date, int hrs, int min, int sec): type int for years, months, days, hours, minutes, seconds

There is an interesting correspondence between the long and the date classes, which is to represent a time as a number of milliseconds from 0:0 GMT, January 1, 1970, 0 seconds. For this correspondence, the date class also has its corresponding constructor: date (long date).

Gets the year, month, day, time, minute, second, and week of the date class you can use the date class's getyear (), GetMonth (), GetDate (), getHours (), getminutes (), getseconds (), GetDay ( ) method, you can also interpret it as converting the date class to int.

The GetTime () method of the date class can get the number of long integers that we said earlier, and as with the wrapper class, the date class has a ToString () method that can convert it to a string class.

Sometimes we want to get a specific format for date, for example 20020324, we can use the following methods, first introduced in the file,

Import Java.text.SimpleDateFormat;

Import java.util.*;

Java.util.Date Date = new Java.util.Date ();

If you want to get the YYYYMMDD format

SimpleDateFormat sy1=new SimpleDateFormat ("YyyyMMDD");

String Dateformat=sy1.format (date);

If you want to get a separate year, month, day

SimpleDateFormat sy=new SimpleDateFormat ("yyyy");

SimpleDateFormat sm=new SimpleDateFormat ("MM");

SimpleDateFormat sd=new SimpleDateFormat ("DD");

String Syear=sy.format (date);

String Smon=sm.format (date);

String Sday=sd.format (date);

Summary: Only Boolean does not participate in conversion of data types

(1). Conversion of automatic type: A. Constant is capable of automatic type conversion within the range of the number of tables

B. Small data range capable of automatic conversion of large data types (note exceptions)

int to Float,long to Float,long to double is not automatically converted, otherwise the precision will be lost

C. The reference type can be automatically converted to the parent class's

D. Basic types and their packaging types are capable of converting to each other

(2). Coercion type conversion: Enclose the target type in parentheses, before placing the variable

4.Java reference types

Java has 5 reference types (object types): Class Interface Array Enumeration callout

Reference type: The underlying structure and basic types differ greatly

The memory space of the JVM: (1). Heap Heap Space: Allocation object new Student ()

(2). Stack stack space: temporary variable Student stu

(3). Code area: class definition, static resource Student.class

Eg:student stu = new Student ();//new creating objects in the heap space of memory

Stu.study (); Assigning an object's address to a STU reference variable

The above example implements the steps: A. JVM loading student.class to code area

B.new Student () allocates space in the heap space and creates a Student instance

C. Assign the address of this instance to the reference Stu, the stack space

Summary of Java Basic data types

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.