Java basic data Type &java variable type

Source: Internet
Author: User
Tags modifiers wrapper
statement: This part of the article reproduced, part of my summary. Part One:java basic data type

The variable is the application memory to store the value. that is, when you create a variable, you need to request space in memory.

The memory management system allocates storage space for variables based on the type of variable, and the allocated space can only be used to store that type of data.

Therefore, by defining different types of variables, you can store integers, decimals, or characters in memory.

Two large data types of Java: Built-in data type reference data type I. built-in data types

The Java language provides eight basic types. Six numeric types (four integers, two floating-point types), one character type, and one Boolean type.

byte: byte data type is a 8-bit, signed integer represented by a binary complement; The minimum value is-128 ( -2^7); The maximum value is 127 (2^7-1); the default is 0; byte type saves space in a large array . Mainly replaces integers because the byte variable occupies only one-fourth of the type of int; example: byte a = 100,byte B =-50.

Short : The short data type is 16-bit, signed, and the integer minimum represented as a binary complement is-32768 ( -2^15); The maximum value is 32767 (2^15-1); Short data types can also save space like byte . A short variable is one-second of the space occupied by an int variable; example: Short s = 1000,short r = 20000.

int: int data type is 32-bit, signed, Integer expressed in binary complement, minimum value is 2,147,483,648 ( -2^31), maximum value is 2,147,485,647 (2^31-1), general integer variable defaults to int type; Example: int a = 100000, int b =-200000.

Long: A Long data type is a 64-bit, signed integer that is represented by a binary complement; The minimum value is-9,223,372,036,854,775,808 ( -2^63); The maximum value is 9. 223,372,036,854,775,807 (2^63-1); This type is mainly used on systems that require larger integers ; Example: Long a = 100000l,int B = -200000l.

Float : float data types are single-precision, 32-bit, IEEE 754-compliant floating-point numbers, 1-bit sign-bit + 8-bit exponential-bit (q) + 23-digit-bit (b), i.e. b^q; float saves memory space when storing large floating-point groups; Floating point numbers cannot be used to represent exact values, such as currency; float F1 = 234.5f.

Double : double the data type is a double-precision, 64-bit, IEEE 754-compliant floating-point number, a 1-bit sign bit + 11-bit exponential bit (q) + 52-bit bottom, or b^q; the default type for floating-point numbers is the double type; The double type cannot also represent an exact value, such as a currency; example: double D1 = 123.4.

Boolean: boolean data type represents a bit of information; only two values: true and false; The default value is false;

Char: char type is a single 16-bit Unicode character; the minimum value is ' \u0000 ' (that is, 0); The maximum value is ' \uffff ' (that is, 65,535); Char data type can store any character; example: Char Letter = ' A '. Summary: Byte,short,int,long are signed, binary complement representation of integers, the difference is occupied space, the actual use can be based on the memory of the choice. Their respective range of expression is ( -2^ (x-1) ~2^ (x-1)-1); Float,double represents a floating-point number, one of which is the difference in their accuracy (the precision is reflected in the digits of the mantissa), and the other is the range that can be represented (because of the different space they occupy). Java wrapper class: The Java language is an object-oriented language, but the basic data types in Java are not object-oriented , and this is a lot of inconvenience in the actual use, in order to solve this problem, design a class for each basic data type of a corresponding class to represent, These eight classes corresponding to the basic data type are collectively referred to as wrapper classes (wrapper Class), also translated as an outer-covering class or data type class. All wrapper classes are subclasses of the abstract class number. Packing classes are located in the Java.lang package.
For the wrapper class, the purpose of these classes consists of two main types:

A, as the class type corresponding to the basic data type exists, to facilitate the operation of the object involved .

b, the related attributes of each basic data type such as the maximum value, the minimum value, and so on, as well as related methods of operation.

two. Reference type reference type variables are created by the constructors of the class and can be used to access the referenced object. These variables are specified as a specific type when declared, such as employee, Pubby, and so on. once a variable is declared, the type cannot be changed. objects, arrays are reference data types . The default values for all reference types are null. A reference variable can be used to reference any type that is compatible with it. Example: Animal Animal = new Animal ("Giraffe"). three. Java Constants

A constant is a fixed value. They do not need to be computed, directly representing the corresponding values. Constants are quantities that cannot be changed. In Java, the final flag is used to declare the same way as a variable.

Part Two:java Variable type

The types of variables supported by the Java language are: local variable member variable class variableA. Java Local variablesLocal variablesdeclaration in a method, construction method, or statement block, the variables are destroyed when they are finished, and the access modifiers cannot be used for local variables; local variables areassigned on the stack.local variable has no default value, so the amount of local variables is declared,after you have initialized,Before you can use. two. Instance VariablesInstance variables are declared in a class, but outside methods, construction methods, and statement blocks; When an object is instantiated, the value of each instance variable is then determined;instance variables are created when the object is created and destroyed when the object is destroyed;The value of an instance variable should be referenced by at least one method, construct method, or statement block, so that the external can obtain the instance variable information in these ways, and the instance variable can be declared before or after use;access modifiers can decorate instance variables(Package (default), private, public, and protected);Instance variables are visible to methods, construction methods, or statement blocks in a class. In general, you should set the instance variable to private. You can make instance variables visible to subclasses by using an access modifier;The instance variable has a default value. The default value for a numeric variable is 0, and the default value for a Boolean variable is false, and the default value for the reference type variable is null. The value of a variable can be specified at declaration time or in a constructor method, and an instance variable can be accessed directly from the variable name. However, in static methods and other classes, you should use the fully qualified name:Obejectreference.variablenamethree. Class variable (static variable)A class variable is also called a static variable,declaration in a class with the static keyword, but must be outside the method construction method and the statement block. No matter how many objects a class creates, the class has only one copy of the class variable. Static variables are stored inStatic Storage Area。 Often declared as constants, a static declaration variable is rarely used alone. Static variables are created at the start of the program,destroyed at the end of the program. has similar visibility to instance variables. Most static variables are declared public, however, in order to be visible to the consumers of the class. The default value is similar to the instance variable. The default value for a numeric variable is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time it is declared, or it can be specified in the constructor method. In addition, static variables can also be initialized in static statement blocks. Static variables can be passed by:Classname.variablenameWay to access. When a class variable is declared as a public static final type, the class variable name must use uppercase letters. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.










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.