Java basic types and reference types

Source: Internet
Author: User
Tags character set

Java includes the basic and reference types. The following describes some attributes and usage methods of each data type.

I. Basic types

(1) numeric type

1. Floating point: (1) float (2) double
Attributes of special floating point values

Expression result
Math. sqrt (-1.0)-> nan
0.0/0.0-> nan
1.0/0.0-> infinity
-1.0/0.0-> negative infinity
Nan + 1.0-> nan
Infinity + 1.0-> infinity
Infinity + infinity-> infinity
Nan> 1.0-> false
Nan = 1.0-> false
Nan <1.0-> false
Nan = nan-> false
0.0 =-0.01-> true


The basic floating point type and the packaging floating point type have different comparison behaviors.

To make things worse, the rules used to compare nan and-0 are different between the basic float type and the package class float. If the float value is equal to the two nan values, false is returned. If the float. equals () is used to compare the two nan float objects, true is returned. This is because, if not, the nan float object cannot be used as a key in hashmap. Similarly, although 0 and-0 are considered equal when they are expressed as floating point values, float is used. when compareto () is used to compare the 0 and-0 values of a float object, the-0 value is smaller than 0.


2. Integer type (1) byte (2) short (3) int (4) long

Various types and features of integers

Name length range
Long integer 64-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
Integer 32-2,147,483,648 ~ 2,147,483,647
Short integer type: 16-32,768 ~ 32,767
Byte 8-128 ~ 127

Let's discuss each type of integer separately.

3.3.1 byte)
The smallest integer type is the byte type. It is a signed 8-bit type, and the number range is-128 ~ 127. Byte variables are especially useful when you process data streams from networks or files. It is also useful when you process unprocessed binary data that may not be directly compatible with other built-in java types.


Byte variables are defined using the keyword "byte. For example, the following two variables are defined: B and c: byte B, c;
3.3.2 short integer (short)
Short is a signed 16-bit type. The value range is-32,768 ~ 32,767. Because it is defined as high-byte-first (known as the big-endian format), it may be the least used type in java. This type is mainly applicable to 16-bit computers, but this type of computer is rarely seen now.

The following are some examples of declaring the short variable:

Short s;
Short t;

3. Character type (1) char

Char type
---- In c language, the most popular base data type is char. This type of model is often used up, because it was originally set to 8 bits, and in the latest 25 years, 8 bits are also the smallest data storage units in the memory of the computer. We put the latter (eight digits are the smallest data storage unit in the memory of the computer) and tie the ascii character set to a condition of 7 characters, you can see that, the char type is a common "usable" data type. In c language, a pointer to char-type variable volume is also used as a pointer, all data that can be guided to the char type can be computed and guided to the char type.
All classes in java are subclasses of the object class. They directly or indirectly inherit the methods of the object class, including the tostring () method. This method is used to convert an object into a string. Therefore, when a string is connected to an object (that is, an instance object of a class), the tostring () method of this object is called to convert the object to a string and then perform the connection operation.

Import java. io .*;
Public class bogus {
Public static void main (string args []) {
Fileinputstream FCM;
Datainputstream dis;
Char c;
Try {
FCM = new fileinputstream ("data.txt ");
Dis = new datainputstream (FCM );
While (true ){
C = dis. readchar ();
System. out. print (c );
System. out. flush ();
If (c = 'n') break;
              }
FCM. close ();
} Catch (exception e ){}
System. exit (0 );
    }
}

 


(2) boolean type

II. Reference type

1. Reference

(1) class type

(2) interface type

(3) array type

It is clear that the most basic types are actually eight, two floating point types, float and double; four integer types: byte, short, int, long; one character type: char, A boolean type.

In fact, you need to determine the value range and memory space occupied by these types. Why do you need to know the value range and memory space occupied by the 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.