Travis Roberts

Ranch Hand
+ Follow
since Jul 13, 2009
Travis likes ...
C++ Linux
Merit badge: grant badges
Biography
When I'm not working I'm learning Java!
For More
Austin, TX
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Travis Roberts

Thank you! That explains a lot. I remember templates from C++ class, it sounds like generics is similiar for Java. Generics is covered in the intermediate Java class so I am sure I'll be tackling that pretty soon.
8 years ago
Hello Java Ranchers:

In prepping for my second Java test, I was given the following problem which I got wrong:



The compiler does not recognize ArrayList. So the answer is compiler error.

But when I add "import java.util.*; and compile again, I get the following:

C:\Practice Test Code>javac Xy144.java
Note: Xy144.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Practice Test Code>javac -Xlint:unchecked Xy144.java
Xy144.java:15: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
ref.add("ABC ");
^
where E is a type-variable:
E extends Object declared in class ArrayList
Xy144.java:16: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
ref.add("DEF ");
^
where E is a type-variable:
E extends Object declared in class ArrayList
Xy144.java:17: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
ref.add("GHI");
^
where E is a type-variable:
E extends Object declared in class ArrayList
3 warnings



Can anyone decifer this? It mentions "raw type" which I do not understand yet. Its references to "E" I assume is something to do with a template meaning that it will accept any data type?
8 years ago

John Stuart wrote:So, I've just started learning Java and have some difficulties understanding basic Java programming terms. These terms are:

1. Class
2. Object
3. Methods
4. Instance
5. Instance Variable
6. Others (e.g., member variable, class variable, member methods, instance methods, and many more)



An instance, in object-oriented programming (OOP), is a specific realization of any object. An instance variable is going to be a piece of data that belongs to that object. A class variable is going to be a piece of data that is shared among every class object that gets created. If you created a class named Student and created a new instance of student for every person enrolled, you are going to have a lot of instances of student. Each of those students are enrolled at the same school, so it doesn't make sense to make an instance variable of schoolName. If you have 9,000 students you are going to have 9,000 schoolName variables taking up space in memory. So you can create a class variable name which basically just creates the variable once and forces it into every instance that gets created from a specific class.
8 years ago

enrique davis wrote:I'd like to add to this topic

I'm studying for the OCA and I'm at chapter 4 but I don't remember what was in chapter 1 and 2
I'm not sure if I'll ever be able to pass this exam

My problem is to find time to study, I only study one or 2 nights a week.

Can someone suggest a working study strategy

Thanks



You should definitely try to study every day, even just 15 minutes is better than nothing.

If you cannot study at work then make index cards to bring with you to review throughout the day. I did that with the A+ and Network+ certifications and it worked wonders! The human brain requires repeated exposure to new information over the span of days/weeks. You cannot study something one day and expect it to still be in your mind weeks from then. When you learn new information your brain is laying out new neurological paths in your brain and you need to continue using those paths to strengthen them... at least that's what I read.

Henry Wong wrote:

Travis Roberts wrote:
aObj.m(bObj);

Does the reference variable aObj not matter?



What do you mean by "not matter"? As already mentioned in a previous post, the compiler uses the type to determine the method to call. And since it is a class A instance, it will call the m() method that takes a class A instance, as that is the only method that matches.

Henry



Yes, but I am passing in an instance of B class, and the m() method in the A class accepts a A class object as a parameter, not B class..

Maybe I just need to study more. The online book that comes with the class I am taking doesn't go over the codes I posted - at least not that I can recall - but I found them in a practice test.
8 years ago

Henry Wong wrote:

Travis Roberts wrote:
And this line of code is even more confusing because I have called the method inside class A and passed it a class B object. Class A does not inherit from B. In this case I expected the compiler to blow up.

I am utterly confused.



Class B extends class A... so doesn't that means that an instance of class B IS-A instance of class A? An instance of B is supposed to work everywhere that expects an A, so why should it "blow up"?

Henry



Well I was just trying to communicate the chaos in my mind. I do not know why it would compiler error, that was just my assumption. Obviously, I do not yet understand overloading and polymorphism.

I know that the reference variable is not totally arbitrary, when I try to use reference variable of type Object it will not work. So in the case of

aObj.m(bObj);

Does the reference variable aObj not matter?
8 years ago
calls the method inside the B class. That much makes sense.

I guess what is confusing me is I read that an object inherits all methods in its ancestral line and in the case of



I have called the m() inside class B (right?) but have passed it a class A object. The m() inside class B accepts only B objects as parameter. I expected it to call the method inside class A.

And this line of code is even more confusing because I have called the method inside class A and passed it a class B object. Class A does not inherit from B. In this case I expected the compiler to blow up.

I am utterly confused.
8 years ago
Well while we are on the subject, I typed up the code below and was wondering how overloaded methods are chosen with objects?



Lines 24, 26, and 28 all call the method in class A. I thought the methods had to be at or above the class in the inheritance structure? When I call aObj.m(bObj) the method m inside class A is still used?
8 years ago
Wow, I can't believe I didn't see that. It's the little things that trip me up the hardest.
8 years ago
Java Wizards:

In studying for an exam at school, I have been going over practice questions and encountered a disconnect. The question below does not compile. The compiler's wrath is directed at line 10. So I thought OK, NewClass must be out of scope.




And then the code below compiles just fine and it has the same situation (lines 10 & 11) where one class instantiates another at default access.



So now I do not understand the top question why it did not compile?
8 years ago
Java Ranch:

I am preparing for a test and as a preparation was given one of the following questions:



The answer I chose was correct, the program outputs 5. But I had a couple of questions for clarification.

If the main driver class was longer, am I right to assume that the anonymous class on line 4 becomes eligible for garbage collection starting on the next line, line 5?

Also, I am uncertain as to why the toString method prints anything at all since it was not called? I tested by adding another method below that one which returns an integer



And that printInt method did not print anything??
8 years ago

Jeanne Boyarsky wrote:They are just trying to show you can. It's rare to actually need to.

One reason is if you don't write all the code yourself. Maybe someone gives you a list of Objects and you know they are of a certain type.

There's also an instanceof operator so you can check the type before casting. For example, you give me a list of objects. I want to do math if they happen to be numbers of just print them otherwise. I can use instanceof to check the type. if it is a Number, I can cast it and then use methods available on number.

ps - You aren't required to post a link to the book. It's helpful though in case someone has a question on the context.



Mainly for illustrative purposes -- roger that!

How come casting is required at all if the method is chosen by the object stored in the reference variable? The casting is performed on the reference variable, if I understand it correctly, but the method is chosen by the object it points to.
8 years ago
Java Ranchers:

I've enrolled into a Java class for this summer and have been reading the online book that comes with the class. The link to the book is posted at the bottom in case it's necessary to include it.

I have been reading about polymorphism, type conversion, and casting. I am confused as to when and why a programmer would choose a specific reference variable for an object. To make this question more clear, I'll give example code from the book:



In the main driver method, an instance of class B is instantiated and stored into a reference of the Object class. As a result, casting is required to call the m() method in the B class.

Could someone please explain why a programmer would use an Object reference variable instead of a reference variable from either class A or B... which, if I am not mistaken, would eliminate the need for casting to call the overridden m() methods?

It is also confusing because I read that the method called is chosen at runtime depending on which variable is stored in the reference variable, and not the reference variable itself. So I am not sure why a cast is required.

Code came from online book:
http://cnx.org/contents/fb64661c-5b3f-4ea8-97c6-e48df112438a@178.3:67/Object-Oriented_Programming_%28O
8 years ago
Quick question... I was thinking about purchasing this book.

It was written in 2006, but it's one of the few with a great rating. Would this book still work well for me or would I encounter a lot of changes working through examples?

Does anyone know a good book written recently? Preferrably with a Kindle edition.. ?
8 years ago