I am coming from C++ so there is one feature of java that I don't quite understand. I have read that all objects must be created using the keyword new
, with the exception of primitives. Now, if the compiler can recognise a primitive type, and doesn't allow you to create an object calling its constructor without new
, what is the reason to have the keyword new
at all? Could someone provide an example when two lines of code, identical except for the presence of new
, compile and have different meaning/results?
Just to clarify what I mean by redundant, and hopefully make my question clearer. Does new
add anything? Could the language have been expressed without new
for instantiation of objects via a constructor?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…