new operation in java - from the complete reference Java

原创 2006年06月06日 14:21:00

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.

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.


  • StrideBin
  • StrideBin
  • 2017年06月23日 13:43
  • 7224


Java引用体系中我们最熟悉的就是强引用类型,如 A a= new A();这是我们经常说的强引用StrongReference,jvm gc时会检测对象是否存在强引用,如果存在由根对象对其有传递...
  • androidstar_cn
  • androidstar_cn
  • 2017年01月24日 22:06
  • 1196

JAVA菜鸟入门(12) reference variable是气球的线 +JVM中怎么实现

1 如果variable是primitive,那就拷贝原来变量的值到新变量。 2 如果variable是object referece, 那就拷贝原来reference的值到新的变量,所以就有2个re...
  • feliciafay
  • feliciafay
  • 2015年04月28日 23:42
  • 1646


转: Java 2 平台引入了 java.lang.ref 包,其中包括的类可以让您引用对象,...
  • javalfx
  • javalfx
  • 2015年10月09日 11:24
  • 1265


  • aitangyong
  • aitangyong
  • 2014年09月21日 18:31
  • 4506


Java的数据类型分为两类:primitive和reference类型  Java虽然是个面向对象的语言,也声称“Everything is object”- 一切都是对象。但是,我觉得还不够纯粹和...
  • songjunyan
  • songjunyan
  • 2014年11月03日 14:16
  • 1314

JAVA 关于abstract 方法里参数的问题

网上查了半天也没查到关于抽象方法里有参数怎么办的解决方案。 今天编程突然冒了这么一句:“Syntax error, insert "... VariableDeclaratorId" to comp...
  • xdzcz
  • xdzcz
  • 2015年04月30日 16:06
  • 3198

JAVA菜鸟入门(6) java中的reference 和 C++中的pointer的区别

首先说明,这里比较的是java中的reference和c++中的pointer,不是java中的reference和c++中的reference,因为这后二者差别非常明显。 Refe...
  • feliciafay
  • feliciafay
  • 2015年04月09日 13:34
  • 1842


启动的时候要加上启动配置: -Xms10m -Xmx10m -verbose:gc 如果要使用Jconsole还要加上
  • u010841296
  • u010841296
  • 2016年03月18日 10:44
  • 708

Java 8 官方教程翻译——方法引用(method reference)

  • cwt8805
  • cwt8805
  • 2014年12月26日 14:43
  • 912
您举报文章:new operation in java - from the complete reference Java