new operation in java - from the complete reference Java

In java, we can use new operator to dynamically allocates memory for an object, like this: 
class-var = new classname(); 
Here, class-var is a variable of the class type created. The classname is the name of the class that is being instantiated. The class name followed by parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created. Constructors are an important part of all classes and have many significant atributes. Most real-world classes explicitly define their own constructors within their class definition. However, if no explicit constructor is specified, then Java will automatically supply a default constructor.

    At this point, you might be wondering why you do not need to use new for such things as integers or characters. The answer is that Java's simple type are not implemented as objects. Rather, they are implemented as "normal" variables. This is done in the interest of efficiency. As you will see, objects have many features and attributes that require Java to treat them differently than it treats the simple types. By not applying the same overhead to the simple types that applies to objects, Java can implement the simple types more efficiently. Later, you will see object versions of the simple types that are available for your use in those situations in which complete objects of these types are needed.

    It is important to understand that new allocates memory for an object during run time. The advantage of this approach is that your program can create as many or as few objects as it needs during the execution of your program. However, since memory is finite, it is possible that new will not be able to allocate memory for an object because insufficient memory exists. If this happens, a run-time exception will occur.

    The distinction between a class and an object. A class creates a new data type that can be used to create objects. That is a class creates a logical framework that defines the relationship between its members. When you declare an object of a class, you are creating an instance of that class. Thus, a class is a logical construct. An object has physical reality. (That is, an object occupies sapce in memory.) It is important to keep this distinction clearly in mind.

Remeber:
When you assign one object reference variable to another reference variable, you are not creating a copy of the object, you are only making a copy of the reference. For example:
Box b1 = new Box();
Box b2 = b1;
//.....
b1 = null;
Here, b1 has been set to null, but b2 still points to the original object.

Java: The Complete Reference, Eleventh Edition By 作者: Herbert Schildt ISBN-10 书号: 1260440230 ISBN-13 书号: 9781260440232 Edition 版本: 11 出版日期: 2018-12-12 pages 页数: (1955) The Definitive Java Programming Guide Fully updated for Java SE 11, Java: The Complete Reference, Eleventh Edition explains how to develop, compile, debug, and run Java programs. Best-selling programming author Herb Schildt covers the entire Java language, including its syntax, keywords, and fundamental programming principles. You’ll also find information on key portions of the Java API library, such as I/O, the Collections Framework, the stream library, and the concurrency utilities. Swing, JavaBeans, and servlets are examined and numerous examples demonstrate Java in action. Of course, the very important module system is discussed in detail. This Oracle Press resource also offers an introduction to JShell, Java’s interactive programming tool. Best of all, the book is written in the clear, crisp, uncompromising style that has made Schildt the choice of millions worldwide. Coverage includes: Data types, variables, arrays, and operators Control statements Classes, objects, and methods Method overloading and overriding Inheritance Local variable type inference Interfaces and packages Exception handling Multithreaded programming Enumerations, autoboxing, and annotations The I/O classes Generics Lambda expressions Modules String handling The Collections Framework Networking Event handling AWT Swing The Concurrent API The Stream API Regular expressions JavaBeans Servlets Much, much more
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值