Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Head First p.535 confusion  RSS feed

Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking at this code and it's not clicking, there are a lot gaps in my knowledge of arrayLists and different nuances of programming, my comment are on the things I don't understand and things I think I do understand please clarify anything I have right or things I don't understand.

Thank you kindly

The code:

I know that's a lot of questions please be patient!

I would like to see if I understand arrayLists correctly. It's my understanding that an arrayList is nothing more than a storage warehouse for Objects? would that be a fair estimate? so we can add things to the warehouse and delete things from the warehouse, provided they are objects.

Thank you
Posts: 3648
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This line is create a array list of String objects, not casting it as String

This line has 2 steps into 1, without declaring a variable. It's the same as saying

You said the one liner isn't the standard instantiation of an object, what is the standard?

Method declaration can go anywhere in the class.

getSongs() in go method can be called as is because getSongs() is in the same class. If you call a method in the same class, you don't need to do things like

The getSongs() is called in the same class so the access modifier (public, protected, private) can be omitted. If not specified it's default package. This page talks more about modifiers.

The try/catch is a way to handle exceptions. The code in the try block like the getSongs() method does the normal flow of the operations. In case there is any exception/error the code will jump to the catch block where you can handle what to do with XXXException occurs or ABCException occurs.

The exceptions specified in the catch block is from specific to general. For example, Exception is the parent of exception classes, any subclasses of it will have to go before it

Every exception has a printStackTrace method. This prints the error log to console allowing you the programmer to figure what or where went wrong such as which line had the exception, which class and method etc.

The File and BufferedReader classes are in the package. At the beginning of the program, you imported all the classes in the java.util and package. The * indicates "all classes".

you should look into the API for File and BufferedReader to understand how these objects are instantiated.

In the addSong method, it calls the String's split method which split the string to arrays using the separator you passed in (the "/" in your example).
Arrays can be declared like String[] str = {"Bob","Sally"};
When you don't know how many elements you will have, the split method can come in handy.

Lastly array lists are 0 (zero) based, just like arrays. ary[0] or list.get(0) is the first element.

Hope this answers most of your questions.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!