One 、 Basic data type

  • byte、short、int、long( Integer types )
  • float、double( Floating point type )
  • char( Character )
  • boolean( Boolean type )

Java Most of the data is stored in the stack .

  • The stack area : Store local variables , Object declaration reference, etc .

  • Heap area : Deposit new keyword Create a class ( Contains member variables ) And arrays .

Advantages and disadvantages of heap and stack

  • The advantages of stacks : Stack data can be share , Access speed is faster than heap fast .   The disadvantage is that : In stack The data size and life cycle are determined Of .

  • The advantages of heaps : Sure Dynamically allocate memory size , You don't have to tell the compiler the lifetime . The disadvantage is that : The access speed is faster slow .

Take a chestnut , Here's the picture :

  • The compiler creates a... In the stack a References to , Then look up the stack with the literal value of 5 References to , take a Point to 5 The address of , If not found , I'll open up an address

    Deposit the literal value 5, And then a Point to the address . Definition int c=5; Because there are literal values in the stack 5, all c Will point directly to 5 The address of . Make a=8, Because it's in the stack

    There is no literal value for 8 References to , All will open up an address to store 8,a It will point back to 8 The address of ,c The direction of is not affected by a influence ,c The value of .

Two 、 Reference data type

1. Class( class )

2. Interface( Interface )

3. Array( Array )

  • Java yes object-oriented Language , It makes corresponding wrapper classes for each basic data type ( See the previous table ), That is to say, it implements the basic type variable to Packaging

    Object conversion . The reference type is generally through new keyword To create ,( object ) It's in memory Pile up in , Available at run time Dynamic allocation of memory size . quote

    The address of the variable content is stored in the variable , It's in memory Stack in . When reference type variables are not used ,Java The internal garbage collector GC Will automatically recycle .

1. Code example -Class:

public class Demo {
String Name;
int Age;
double Score;
Demo(String Name,int Age,double Score){
// Define a constructor with three arguments , To initialize
this.Name=Name;
this.Age=Age;
this.Score=Score;
};
public static void main(String[] args) {
Demo d1=new Demo(" Xiao Wu ",20,98);
Demo d2=new Demo(" Small days ",19,80);
}
}

  • The reference itself is reserved in Stack Medium , The object referred to by reference is stored in Pile up Medium .

2. Code example -Interface

interface Animal {// Definition Animal Interface 
public void eat();
public void speak();
}
public class Dog implements Animal {//Dog Class implementation Animal Interface
public void eat() {
System.out.println(" I like to eat bones !");
}
public void speak() {
System.out.println(" Wang Wang Wang !");
}
public static void main(String[] args) {
Dog d=new Dog();
d.eat();
d.speak();
}
}
  • Interfaces are written in a similar way to classes , But they belong to different concepts . Class describes the properties and methods of an object . The interface contains the methods that the class will implement .

  • The difference between interface and class :

  1. Interfaces cannot be used to instantiate objects .

  2. Interface has no constructor .

  3. All methods in an interface must be abstract methods .

  4. Interface cannot contain member variables , except static and final Variable .

  5. Interfaces need to be implemented by classes .

  6. Interface supports multiple inheritance .

3. Code example -Array

public class Array {
public static void main(String[] args) {
int[] myList = new int[] {5,2,0,1,3,1,4};
for(int list: myList) {
System.out.print(list);
}
}
}
  • In the above code ,myList Variables hold references to array objects , It's stored on the stack , The space occupied by array objects is allocated on the heap ( Just like the picture above ).

expand 1:

  • The return value type of the method is the class name : Returns the object of this class .

  • The return value type of the method is the interface name : Returns the object of the implementation class of the interface .

 public static Comparable findMax(Comparable[] arr) {
int maxIndex=0;
for(int i=1;i<arr.length;i++)
if(arr[i].compareTo(arr[maxIndex])>0)
maxIndex=i;
return arr[maxIndex];
}
  • In the above code , Pass parameters to implement Comparable An array of objects for the interface , Method returns Comparable Interface implementation class object .

  • We usually use them Object、 Interface type, etc , The goal is to support code reuse .

expand 2: Autoboxing and unboxing

  • Automatic boxing : If one int The type quantity is transferred to the need for a Integer Where the object is , The compiler will insert a pair behind the scenes Integer Call to construct method .

  • Automatic boxing : If one Integer Objects are placed where they need to be int It's the place of quantity , The compiler will insert a pair behind the scenes intValue Method call .

notes : Reference resources 《 Data structure and algorithm analysis Java Language description 》 and Novice tutorial

Java More articles on basic data types and reference data types in

  1. JAVA It is divided into basic data type and reference data type

    One . Basic data type : byte:Java The smallest data type in , In memory 8 position (bit), namely 1 Bytes , Value range -128~127, The default value is 0 short: Short , In memory 16 position , namely 2 Bytes , Value range -32768 ...

  2. java Basic data types and reference data types in

    java The basic data types in are 8 Kind of :byte,short,int,long,char,float,double,boolean There are four types of integers :byte short,int,long byte:   1 byte   ...

  3. Java The road to the foundation ( One ) Next -- Array of reference data types

    Last time we said java Basic data type of , Today, let's talk about arrays in reference data types . What is an array Array : The same data type stored in a continuous block of memory ( Reference data type ) Element collection for . Every data in an array is called an array element , Count ...

  4. java Data type of : Basic data types and reference data types

    Java Basic concepts of data types Data types are in the computer language , Is an abstract representation of a memory location , It can be understood as an abstract expression of memory . When I first came into contact with every language , There will be an understanding of data types , There are complex , There are complex , various ...

  5. java Basic data types and reference data types in

    One . Basic data type : byte:Java The smallest data type in , In memory 8 position (bit), namely 1 Bytes , Value range -128~127, The default value is 0 short: Short , In memory 16 position , namely 2 Bytes , Value range -32768 ...

  6. Java Learn the difference between basic data type and reference data type

    JAVA It is divided into basic data type and reference data type . Basic data type : byte:Java The smallest data type in , In memory 8 position (bit), namely 1 Bytes , Value range -128~127, The default value is 0 short: Short , In the memory ...

  7. Java The ginseng - The difference between basic data type and reference data type as parameters ( Value passed )

    java Methods in can pass parameters , The way to pass parameters is to pass values . Parameters have formal parameters and actual parameters , Parameters written when defining methods are called formal parameters , When the method is actually called , The parameters passed are called arguments . When calling a method , The argument is passed to the formal parameter , The inner part of the method is actually using formal parameters . the ...

  8. JavaScript The difference between basic data type and reference data type in

    1. Basic data types and reference data types ECMAScript Includes two different types of values : Basic data types and reference data types . Basic data types refer to simple data segments , A reference data type is an object that has multiple values . When we assign a variable to a variable ...

  9. js The difference between basic data type and reference data type in

    1. Basic data types and reference data types ECMAScript Includes two different types of values : Basic data types and reference data types . Basic data types refer to simple data segments , A reference data type is an object that has multiple values . When we assign a variable to a variable ...

  10. Java The difference between the basic data type and the reference type

    Java The difference between basic data type and reference type of constructor parameter passing in If the argument in the constructor is a basic data type , If there is no return value in the function , It doesn't change when it's called : And if it's a reference type , What's changing is the location of the storage , All whether or not they return ...

Random recommendation

  1. Rust Language multithreading programming

    When I write this essay , Comes at a time Rust1.0 Soon after it was released , Strictly speaking, it's a combination of C The efficiency of language execution and Java Powerful language for efficient development , Its ownership mechanism makes it impossible for you to write thread unsafe code , It's a system level language that can be used to write operating systems ...

  2. setprecision、fixed、showpoint To sum up the usage of

    First, add the header file :iomanip One :setprecision effect : Control the number of floating-point numbers displayed in the output stream ,setprecision(n) It's the output n Number , There will be rounding . such as :double s=20.784 ...

  3. nodejs Simple interface to implement

    var http = require('http'); var mysql = require('mysql'); var connection = mysql.createConnection({ ...

  4. Linux Use notes : customized core dump File name of the file

    In the development process , When one Linux When the program exits abnormally , We can go through core File to analyze the detailed reasons for its exception . By default ,Linux When the program is abnormal, it does not produce core file , If you want the program to exit abnormally core dump file , need ...

  5. [ turn ] C++ Redistributable Package Version details

    The programs we use often need C++ Redistributable Package Support for .C++ Redistributable Package There are many versions , It's inconvenient to install . at present (2013-12-04) ...

  6. Sql Server Batch stop job

    CREATE Proc [dbo].[Proc_StopJob] as begin declare @I int declare @JobID uniqueidentifier -- 1. creat ...

  7. nagios Secondary development ( Children under five )---nagios and nagiosql Relationship

    be based on nagios and nagiosql understand . This will be the simple relationship between the two , For more information , Please refer to the figure below :       As you can see from the diagram above ,nagios And nagiosql Shared hosts . Host group . service ...

  8. JDBC Experience

    Remember jdbc There are three steps , One is class Object out 2  Linked database 3 SQL  pre At the beginning 4 allow SQL,result,exeupdate, What I want to write here is to get objects through reflection , Hibernate Yes ...

  9. python --- 14 recursive Binary search

    One . recursive 1. The function calls itself 2. Official statement maximum depth 1000, But I can't 1000, It depends on the interpreter , Actually measured 998 3. send ⽤ Recursively traverse various tree structures Two .    Binary search Pinch the head and end in the middle ,  It has to be an ordered sequence ...

  10. Multithreaded programming : Caused by an instruction reorder chaos

    Post the correct code first : package com.xiaobai.thread.main; import lombok.extern.slf4j.Slf4j; @Slf4j public class Thr ...