Data types in Java

Source: Internet
Author: User

Data types in Java

The Java language data types include two, basic data types, and reference data types.

Defined

Basic data type: The variable name points to a specific value.

Reference data type: The variable name points to the memory address of the stored data object, that is, the variable name points to the hash value.

Basic data type: integer type (byte,short,int,long), floating-point type (float,double), character type (char), Boolean type (Boolean).

Reference data types: Classes (Class), interfaces (interface), arrays ([]). Classes that reference data types are subclasses of the object class.

Memory allocation

Basic data type: When created, the stack is divided into a piece of memory, the value is stored directly on the stack .

Reference data type: When it is created, it first allocates a piece of memory to its reference (handle) on the stack , and the object's specific information is stored on the heap memory, and then the reference above the stack points to the address of the object in the heap.

For example, there is a class person, with attribute Name,age, with the constructor method of the argument,

Person p = new person ("Zhangsan", 20);

The specific creation process in memory is:

1. First in the stack memory bit its p allocated a space;

2. Allocate a block of space to the person object in heap memory and set the initial value "" for its three attributes, 0;

3. According to the definition of the property in the class person, the object's two attributes are assigned operation;

4. Call the construction method and assign a value of two to "Tom", 20; (Note that there is no connection between p and person objects at this time);

5. Assign the address of the person object in the heap memory to p in the stack, and the reference (handle) p to find specific information about the objects in the heap.

Related knowledge:

static zones: saves automatic global variables and static variables (including static global and local variables). The contents of the static zone are present throughout the lifetime of the program, and are allocated by the compiler at compile time.

Heap Area: The memory allocated by the MALLOC series function or new operator, typically released by the programmer, whose life cycle is determined by either free or delete. exists until it is released, until the program ends and is released by the OS. It is characterized by the use of flexible, space is relatively large, but error-prone.

Stack Area: automatically allocated by the compiler to release, save local variables, the contents of the stack only within the scope of the function, when the function is finished, the content will be automatically destroyed, which is characterized by high efficiency, but limited space size.

literal constant: The constant string is placed here and released by the system at the end of the program.

Program code area: binary code that holds the body of the function.

Reference: https://www.cnblogs.com/fxust/p/4622105.html

What is null in Java?

As I said, NULL is a very important concept in Java. NULL is designed to represent something that is missing , such as a missing user, resource, or other thing. However, a year later, the headache of a null pointer exception caused a lot of harassment to the Java programmer. In this material, we will learn the basic details of the NULL keyword in Java, and explore some techniques to minimize null checking and how to avoid disgusting null pointer anomalies.

1) First, NULL is a keyword in Java, like public, static, final. It is case sensitive and you cannot write null or NULL, and the compiler will not recognize them and then make an error.

// Not Ok NULL  // Ok

Programmers using other languages may have this problem, but now the use of the IDE has made the problem trivial. Now, when you hit the code, the IDE, like Eclipse and NetBeans, can correct the error. But using other tools like Notepad, Vim, Emacs, this problem will waste your precious time.

2) Just as each primitive type has a default value, such as the default value of 0,boolean for int defaults to False,null is the default value for any reference type , not strictly the default value for all object types. Just as you create a Boolean type variable, it will be false as its default value, and any reference variable in Java will be null as the default value. This applies to all variables, such as member variables, local variables, instance variables, and static variables (but when you use a local variable that is not initialized, the compiler warns you). To prove this, you can observe the reference variable by creating a variable and then printing its value, as shown in the code:

Private Static Object myObj;  Public Static void Main (String args[]) {    System.out.println ("What is value of MYOBJC:" +null

This is true for both static and non-static object. As you see here, I define myobj as a static reference, so I can use it directly in the main method. Note that the main method is a static method and non-static variables cannot be used.

Transferred from: http://www.importnew.com/14229.html

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.