• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Rancher
Posts: 3620
38
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 69493
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 4
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 3620
38
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 3620
38
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 69493
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 4
  • Mark post as helpful
  • send pies
  • 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: 69493
277
  • Mark post as helpful
  • send pies
  • 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.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic