Basic data types and reference data types in JAVA

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

First, the basic data type:

byte: 8 bits (BIT), which is 1 bytes, the maximum amount of data stored is 255, and the data range stored is-128~127. Default value 0

Short: 16 bits, 2 bytes, value range-32768~32717, default value 0

int: 32 bits, 4 bytes, value range-2147483648~2147483647, default value 0

long: 64 bits, which is 8 bytes-2^63~2^63-1, default value 0L

float: floating point, 32 bits in memory, 4 bytes, for storing a number with a decimal point (the difference from a double is that the float is a valid decimal point only 6~7 bit), and the default value of 0

Double: dual-precision floating point, used to store a number with a decimal point, 64 bits in memory, 8 bytes, default value 0

Char: Character type, used to store a single character, 16 bits, or 2 bytes, value range 0~65535, default value is empty

Boolean: boolean type, 1 bytes, used to determine true or false (only two values, that is, true, false), default value False

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 Char byte Short Int Long float Double void
bits 1 all 8 + + 32 --
bytes 1 2 1 2 4 8 4 8 --
Default initialization value false \ 0 0 0 0 0.0 0.0  
wrapper class Boolean Character Byte Short Integer Long Float Double Void

Second, the Java data type is stored in memory :

1) Basic data type Storage principle: All simple data types do not have the concept of "reference", the basic data types are directly stored in memory on the memory stack, the value of the data itself is stored in the stack space, and the Java language inside the eight data types is this storage model;

2) The principle of storage of reference types: The reference type inherits from the object class (also reference type) is stored in Java memory model for storing objects, using the Java memory heap and memory stack for this type of data storage, simply speaking, "reference" is stored on an ordered memory stack, And the value of the object itself is stored on the memory heap;

difference: The difference between the base data type and the reference type is primarily that the base data type is allocated on the stack, and the reference type is allocated on the heap (requires Java stack, heap concept),

The memory model of the base type and the reference type is essentially different.

Example 1: Let's analyze the difference between "= =" and Equals ().

First, I'll set the two String object

Stringa= "ABC";

stringb= "ABC";

And then

if (a==b) {

System.out.println ("A==b");

}else{

System.out.println ("A!=b");}

Program Output A!=b

Cause: The address of A and B is not the same, A==b compares the address of two variables

Example 2: Defining two basic types

int a=4;

int b=4;

if (a==b) {System.out.println ("a==b");}

Else

{System.out.println ("a!=b");}

Output: A==b

Cause: = = Compares the contents of a two variable

conjecture: Regardless of the basic data type or reference type, they will first allocate a piece of memory in the stack, for the base type, the area contains the basic type of content, and for the object type, this area contains a pointer to the real content, the real content is manually allocated to the heap .

Third, Java basic type value range calculation

From the point of view of the computer composition principle can be explained:

Byte is 8 bytes in the computer, and byte is a signed shape, with a binary representation when the highest bit is the symbol bit 0 for a positive number 1 for negative numbers.

Maximum value: 127 is 2 of the 7-square minus 1, the minimum: 2 of the preceding negative symbol: 7. (Contains the beginning, does not contain the end);

Positive numbers exist in the form of the original code in the computer;

Negative numbers in the computer is in its complement form exists, that is, the absolute value of the original code to the binary and then bitwise reverse after adding 1.

The following 10 and 10 are examples of: 10 original code: 00001010 its storage in the computer is 0000 1010,-10 According to the previous calculation except that its absolute value is 10, to the binary 0000 1010 bitwise REVERSE 1111 0101 plus 1:1 111 0110, this is-10 complement, OK, 1111 0110 in the computer is the representative-10.

Let's take a look at the binary representation of 128 absolute value 128:1000 0000 Bitwise counter 0111 1111 plus 1:1000 0000, that is 128 in the computer is 1000 0000, and then look at-129 in the computer representation, absolutely The range of value 129 has exceeded the number of bits in byte. So pay attention to this kind of problem;

iv. conversions 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. And in the method call, the actual parameter is smaller, and the method called the form parameter data is larger than the "big" (if there is a match, of course, will call the matching method directly), the system will automatically convert "small" data into "large" data, and then the method calls, naturally, for more than one overloaded method of the same name, will The "big" data that is most "close" and makes 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 can certainly lead to 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. Constants can be automatically type-converted within the range 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

v.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 an Student instance;

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

Basic data types and reference data types in JAVA

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.