Default values for initial variables in Java

Source: Internet
Author: User

There are 8 basic data types in the Java language, and the basic information is summarized as follows:

Serial number

Data type

Size/bit

Encapsulation class

Default value

can represent a range of data

1

BYTE (bit)

8

Byte

0

-128~127

2

Short (shorter integer)

16

Short

0

-32768~32767

3

Int (integer)

32

Integer

0

-2147483648~2147483647

4

Long (length integer)

64

Long

0L

-9223372036854775808~9223372036854775807

5

Float (single precision)

32

Float

0.0f

1.4e-45~3.4028235e38

6

Double (dual precision)

64

Double

0.0

4.9e-324~1.7976931348623157e308

7

char (character)

16

Character

'/uoooo ' (null)

0~65535

8

Boolean

8

Boolean

Flase

True or False

☆☆☆☆☆

When a variable is used as a member of a class, Java ensures that its initial value is given , preventing a program run-time error. But these initial values are not correct for your program. Therefore, you must explicitly specify the initial value.

However, the above complaint does not apply to "local" variables (i.e., fields that are not a class). Therefore, when you define int i in a method, the variable I may get any value. will not be initialized to 0. So pay an appropriate value before use. If you forget, Java will return an error at compile time. Tells you that this variable is not initialized. When you define a reference to an object in a class, the reference defaults to a null value if there is no given initialization value. That is, the default value for the base type in Java is 0, and the reference type defaults to NULL.

Default values for initial variables 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.