Classification and initialization of Java variables

Source: Internet
Author: User

2017/6/25

The first to learn Java most authoritative is the official document, today from the beginning to read the document, some small details to clarify.

Variable

The variables in the Java language are divided into the following 4 categories:

1. Instance Variables: (non-static fields) is a non-static field in a class

2. Class Variables: Static field in the (Static fields) class

3. Local Variables: Locally variable

4. Parameters: Parameters

Two terms to note, respectively, are field and variable. field refers to the above 1 and 2, which is owned by class. Instead of the field variable is called variable, corresponding to the above 3 and 4, or local is variable.

Named

The Java documentation explicitly writes the recommended naming method, which is why the code in Java is basically the same name. The format is to use consecutive meaningful words, starting with the second word and capitalizing the first letter. For example: Speed, Currentgear

Basic datatype Primitive Data type

Java has 8 basic data types

Integer type 4: Byte, short, int, long

Floating point 2: float, double

Character 1: Char

Boolean 1: Boolean

Their default initial values are as follows:

Data Type Default Value ( for fields)
Byte 0
Short 0
Int 0
Long 0L
Float 0.0f
Double 0.0d
Char ' \u0000 '
String (or any object) Null
Boolean False

Default value

The default values for variables that are not initialized, C + + is not set for you, and Java is different, and the official documentation describes it very clearly:

The eight primitive data types Are:byte, short, int, long, float, double, Boolean, and Char. The java.lang.String class represents character strings. The compiler would assign a reasonable default value for fields of the above types; For local variables, a default value of is never assigned. A literal is the source code representation of a fixed value. An array is a container object, holds a fixed number of values of a single type. The length of an array was established when the array is created. After creation, it's length is fixed.

For field, it is automatically set to the default value, such as int set to 0,object set to null. For variable, it will not be set automatically, if the compiler does not initialize the error

Reference:

Https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variablesummary.html

Https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Classification and initialization of Java variables

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.