• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head first java chapter 3 code magnet site 64, why a create new object wasn't required?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


UPD by MZ: I formatted the code, and don't select "Disable BB Code in this message" checkbox -- it will disable Java code formatter in your message.
 
Bartender
Posts: 3904
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of this:



The step above created an array with 4 elements, each of the element is of type String (this info bound with array after it created and may not be changed), and each element not initialized yet (for Object types including String it will be null)

Now, the next step for initializing array element would be:



The code above -- special case for Strings where you can use string literal without keyword new.

Generic syntax for some other object type would look like this (more verbose so for string it's not used normally):



There is no class type Inseln, so your code would fail.

If you want specifically this type, the the class Inseln must be declared somewhere in classpath, and the code would be like this:



What you must remember -- array type defined at the moment of creation and may not be changed during array life.

 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Remember that the code magnets exercises should create a working program by using all the code supplied, and not adding anything additional. Remember that you can replacewith...but it only works for words with 5 letters makes the code magnets exercise less interesting.
 
us Naim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mikalai.

So I don't need to use  create  the  Array objects., when it Comes to String objects.

This includes the primitv types also?
They didn't use new for index-Array either.

Thr reason why Im asking, is that I had to add this "new" Statement for a similar code,



So I had to add this Statements

for(int i=0; i< 3;i++){
meineBuecher[i]= new Buch();
}

otherwise I got a NullPointerException
 
Mikalai Zaikin
Bartender
Posts: 3904
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do like this:



You move from this

meineBuecher -> null

to

meineBuecher --> [null; null; null]

But you still have

meineBuecher[0]  -> null

So, meineBuecher[0].title will give Null Pointer Exception

You have to do



After that you have:

meineBuecher --> [Buch(title = null, author = null); null; null]

And if you try meineBuecher[0].title it will return null, but there will be no exception.
 
Mikalai Zaikin
Bartender
Posts: 3904
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

us Naim wrote:

So I don't need to use  create  the  Array objects., when it Comes to String objects.



More accurate will be to say -- "no need to call explicitly constructor when you use String."

The new keyword is a sign of constructor (new object creation) -- whenever you see new you can think of "new object created"

With Strings is the opposite -- in 99.9% cases you will use string literals, e.g.



Instead of



Because second syntax:
1) longer
2) will create +1 extra object on the heap memory (which will be just consuming your resources)

So, for strings use simple literals without new keyword
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

us Naim wrote:. . . So I don't need to use  create  the  Array objects., when it Comes to String objects.

That's incorrect. What it means is that the String literal "us Naim" is a reference to an object already.

This includes the primitv types also?

Forget about primitives for a bit; they are completely different from objects.

They didn't use new for index-Array either. . .

It says . . . new int[4] in my copy of HFJ.

So I had to add this Statements . . .

That is because a newly‑created array for any reference type is implicitly filled with nulls and you can't add a book title to a non‑existent book.

Please avoid spaces before [], despite what it shows in HFJ: String[] please rather than String [].
 
us Naim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. "no need to call explicitly constructor when you use String."

Thanks guys, I really appreciate it.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

us Naim wrote:. . . "no need to call explicitly constructor when you use String."

Sort of, yes. It is usually possible to create all the String objects you need without having to write new String(...)

Thanks guys . ..

That's a pleasure

Please avoid coloured text, which I have reverted to black. The colours can be difficult to read.
reply
    Bookmark Topic Watch Topic
  • New Topic