Home > Java > javaTutorial > body text

How to use the new keyword in java

王林
Release: 2023-05-03 22:16:05
forward
2746 people have browsed it

1. Concept

In the Java language, the "new" expression is responsible for creating an instance, which will call the constructor to initialize the instance ;The return value type of the constructor itself is void, which does not mean that "the constructor returns a reference to the newly created object", but that the value of the new expression is a reference to the newly created object.

2.Purpose

New class object

3.Working mechanism

Allocate memory space for object members, and specify default values

Explicitly initialize member variables

Execute Construction method

Calculate and return reference value

4.Instance

new operation It often means opening up a new memory space in the memory. This memory space is allocated in the heap area in the memory and is controlled by the jvm and automatically manages the memory. Here we use the String class as an example.

public class Test {
    public static void main(String[] args) {
    	String aString = "hello";
    	String bString = new String("hello");
    	String cString = "hello" ;
    	String dString = new String("hello") ;
    	System.out.println(aString==bString);
    System.out.println(aString == cString);	
    System.out.println(dString == bString);	
    System.out.println(aString);
    aString = "hi" ;
    System.out.println(aString);
    }
    
}
Copy after login

The above is the detailed content of How to use the new keyword in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!