File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion with return type of getAttribute()

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In chapter 5 of head first servlets and Jsp I am not able to understand the below code
getServletContext().setAttribute("foo","22");
getServletContext().setAttribute("bar","42");
out.println(getServletContext().getAttribute("foo");
out.println(getServletContext().getAttribute("bar");
As the return type of getAttribute()method is Object so why we there is no casting done.
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily answer this yourself: What type of parameter does the println() method expect?
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Println method expect string so if I am not wrong tostring method of string class will be called
Verify me if I am correct
Thanks
 
German Gonzalez-Morris
Ranch Hand
Posts: 233
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right.

For any doubt in method is always useful to check the javadoc:

http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#println%28java.lang.Object%29

[javadoc] println

public void println(Object x)

Prints an Object and then terminates the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

Parameters:
x - The Object to be printed.
[/javadoc]

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
German Gonzalez-Morris wrote:Yes, that's right.

Well, no it's not; as the javadoc you linked to points out.

upanshu vaid wrote:Println method expect string so if I am not wrong tostring method of string class will be called
Verify me if I am correct

You are not correct. Don't guess -- check the documentation as German has suggested.
 
German Gonzalez-Morris
Ranch Hand
Posts: 233
Eclipse IDE Firefox Browser Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
German Gonzalez-Morris wrote:Yes, that's right.

Well, no it's not; as the javadoc you linked to points out.


yeah, it is wrong my first comment line...
Fortunately I copy-paste the javadoc where it is verbatim explained
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic