This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
None of these are performance questions; I'll move this topic to Java in General (Beginner), although personally, I'd prefer to see these asked one at a time, and I'd want to see you post your answers, so other people could critique them.
Ok, I can't help a whole lot with the JSP questions, I've only ever written a few JSP pages, and they were quite a long time ago. But the rest I might be able to help with.
The difference between String s = "abc" and String s = new String("abc")
This is a tricky one that a lot of people don't realize. Whenever you declare a string in Java using a string literal (i.e. you supply the value by enlcosing it in quotations marks directly in the code), the jvm automatically caches that string into a pool, so that every time you use the same string literal in your code, java replaces it with exactly the same instance of that String. This is why java will return true when you test"My String" == "My String" (remember that if they were not the same instance, even though they hold the same value, the == operator would return false, and the .equals() operator would return true.
However, when you declare a string as new String("My String"), it is guaranteed to return a new instance (hence the use of the new operator).
If you for some reason need to access the reference that is maintained in the pool without actually writting the string in the code, you can use the intern() method of java.lang.String. It returns a reference to the String in the pool that is equal to the string you provided.
So the following would lines of code would have the indicated results:
I don't recall off the top of my head exactly all of the implicit variables in a JSP, and what their names are. Sorry.
The Difference between a Server and a Container
Okay, I assume you're mean a Servlet Container. I'm definitely not the authority on this, but one thing you should remember is that not all servers can automatically run servlets. Most need a Servlet Container installed into the server to be able to run a Servlet or a JSP.
Abstract Classes without Abstract Methods
Okay, the interviewer seems to looking for a very specific answer here, and I presume it has something to do with servlets and jsp, but I can give you a more general answer that applies to all situations.
In java, the abstract key word provides Two functions. The first is to prevent[/p] instantiation of the class. The second is to [b]enforce implementation of abstract methods in any concrete implementation (non-abstract subclass).
So, an abstract class that does not have abstract methods is still gaining the benefit of the first function. There are several reasons why you may wish to enforce that a class cannot be instantiated. One is a form of self-documentation. When you do this, you indicate to all programmers using your class that this particular class should NOT be instantiated, but instead should be overridden. This is an indication that the class is NOT complete as it is now, even though all methods have been defined. Often, this is because you need to partially implement a method (i.e. you want any implementing class to call super.thisMethod in the method, for logging purposes, for example), therefore you can't make it abastract, however, you don't completely implement the method, so you don't want anybody to use the class as is. This sort of thinking can typically be found when using the Strategy design pattern (see Design Patterns [gang of four]).
Another reason preventing instantiation of a class is for security reasons.
Another alternative to abstract methods is sometimes used when writting applicaitons that rely heavily on reflection. If you want to specify a certain implementation for an object, but don't want to define the methods that need to be implemented. Instead, you will use reflection to call methods. This sort of approach can be used with code generation, and in many cases, the classes that are generated are typically subclasses of an abstract class, that doesn't necessarily define any abstract methods, it just provides a base type, with certain default methods, that might be fully implemented.
And there are lots of other reasons as well. If you start digging into design patterns, you will find that the abstract class is extremely important for most of them, but that there could be times that you will want to implement all methods in the abstract class, to provide default operations, or to partially define default methods (i.e. logging procedures of default methods).
Constructors in Abstract Classes
This is a pretty simple answer. Abstract classes provided inheritance of implementation (as opposed to the inheritance of interface provided by the Interface mechanism in java). And where there is implementation details, there may need to be code used to initialize those details. Basically, constructors can be provided so that when making a subclass of an abstract class, you can invoke super() in the constructors of the class. This is more important that it might seem on the surface. In java, EVERY constructor must (implicitly or explicitly) invoke a constructor from the super class before doing anything else. Therefore, any class that does not provide (implicitly or explicitly) a public constructor CANNOT be extended. Abstract classes are no different. In order for abstract classes to be extended, they must provided a constructor of some sort (implicilty or explicitly). So when the programmer writes a constructor, he is really just providing the default implementation of a constructor that would have been present anyway.
Also, consider a situation when an abstract class is not the root of the class heirarchy, but rather is an extension of a non-abstract class that happens to provide some abstract methods. You would not be able to chain to the constructor of the root class, if the abstract class did not provide a constructor that chained to the constructor of the root class.
This is pretty easy, actually. The code required for this is this simple:
There's really no need to explain that one any further, I don't think!
Hope that all helps!
- Adam [ July 23, 2006: Message edited by: Adam Nace ]
Joined: Apr 01, 2005
Thanks Adam, for the answers!
Joined: Jan 29, 2003
#3 is curious. A server is a program that waits for requests from clients. It can open a ServerSocket or an FTP host or listen for JMS messages or whatever. I'm not sure that "container" is so cleanly defined. It may just be the part of a server that "hosts" application code.