Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: Don't understand this Java syntax.

  1. #1
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Don't understand this Java syntax.

    Hi forum members,

    Currently, I'm trying to make sense out of a shopping cart code.

    This is part of the servlet:

    Object objCartBean = session.getAttribute("cart");

    My question is this Object is is part of Java API just like String which is part of Java API?

    How does one know when to use Object preceding to objCartBean?

    Another part of the code which I can't fathom is the "cart". I only managed to find this variable under the Shopping.jsp which has a jsp:useBean id="cart".

    Hope someone can explain the above questions to me.

    Thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Don't understand this Java syntax.

    "cart" is not a variable.
    It's a String.
    'session' is, presumably, the HttpSession.
    This is (amongst other things) a place for storing session information, which maps a String to an Object, so it can be retrieved between requests to the server.
    That code is retrieving the Object stored inthe session using the key "cart".

    Object is the base class of all classes in Java.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Re: Don't understand this Java syntax.

    Quote Originally Posted by Tolls View Post
    "cart" is not a variable.
    It's a String.
    'session' is, presumably, the HttpSession.
    This is (amongst other things) a place for storing session information, which maps a String to an Object, so it can be retrieved between requests to the server.
    That code is retrieving the Object stored inthe session using the key "cart".

    Object is the base class of all classes in Java.
    I'm still quite confused.
    If "cart" is a String I can't find it being defined at all in the entire code.
    The only time "cart" appeared in the code is at this line: <jsp:useBean id="cart" scope="session" type="beans.CartBean"/>

    So, how can "cart" be a String?

    Pardon me my stupidity, I just can't understand why they are using the word "Object" in Object objCartBean ?

    At another part of the code, there's this line CartItemBean cartItem = null.

    CartItemBean.java is a class that appear in another part of the code, so is the CartItemBean an Object also or is it a casting that is being used?

    Yet another line :

    cartItem = (CartItemBean)allCartItems.get(ItemIndex-1);

    Is the CartItemBean here a cast from the CartItemBean.java class?

    Really hope to understand all these intricacies. Thanks.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Don't understand this Java syntax.

    how can "cart" be a String?
    The enclosing "s make it a String. Text inside of a pair of "s is a String.

    CartItemBean cartItem = null.
    Defines a variable named: cartItem of type: CartItemBean and gives that variable a value of null;

    Object objCartBean
    Defines a variable named: objCartBean of type: Object
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Re: Don't understand this Java syntax.

    Quote Originally Posted by Norm View Post
    The enclosing "s make it a String. Text inside of a pair of "s is a String.


    Defines a variable named: cartItem of type: CartItemBean and gives that variable a value of null;


    Defines a variable named: objCartBean of type: Object
    Hello Norm,

    I refer to your explanation :

    cartItem of type: CartItemBean

    Do you mean CartItemBean is a type just like Object?
    If it is so, how can I know what are the other types out there ? So far, I've learnt simple things like int, Boolean, instanceOf, compareTo, remove, get, set.

    So, it's confirmed that the method of casting is not being used on this code below?

    cartItem = (CartItemBean)allCartItems.get(ItemIndex-1);

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Don't understand this Java syntax.

    All classes are types. Java SE has defined lots of them. Any programmer can define lots more. You can define classes and they are all considered types. Any type/class used in a program must have a definition the compiler can find.
    Java Code:
      cartItem = (CartItemBean)allCartItems.get(ItemIndex-1);
    That is an example of casting.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Don't understand this Java syntax.

    Can I suggest you start from the basics of Java before even attempting to try and work on web server code?
    Because if you don't understand what a String is, or how classes work, then you are not going to be able to write anything meaningful when it comes to servlets and JSPs.

    Here's the Java tutorial...start from the beginning.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Re: Don't understand this Java syntax.

    Quote Originally Posted by Norm View Post
    All classes are types. Java SE has defined lots of them. Any programmer can define lots more. You can define classes and they are all considered types. Any type/class used in a program must have a definition the compiler can find.
    Java Code:
      cartItem = (CartItemBean)allCartItems.get(ItemIndex-1);
    That is an example of casting.
    OK. Now, I've read that the servlet create and store data as follows:

    ValueObject value = new ValueObject(....);
    and then follow by request.setAttribute("key", value);

    So what's the difference between the above codes and the one below which is more succinct?
    Object objCartBean = session.getAttribute("cart");

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Don't understand this Java syntax.

    The difference is that you have posted different pieces of code that do different things with different methods.

    Check out post #7
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Don't understand this Java syntax.

    Quote Originally Posted by Kareng View Post
    OK. Now, I've read that the servlet create and store data as follows:

    ValueObject value = new ValueObject(....);
    and then follow by request.setAttribute("key", value);

    So what's the difference between the above codes and the one below which is more succinct?
    Object objCartBean = session.getAttribute("cart");
    For that you would need to know the structure of a web application and how it processes requests, and what an HttpRequest holds, and what an HttpSession holds, and where you should use the different facilities provided by each one.

    Seriously. You need to go through a tutorial, book, something other than posting here. We aren't some sort of cheap training facility.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. trying to understand generic Syntax
    By cris9288 in forum New To Java
    Replies: 1
    Last Post: 02-17-2012, 06:47 PM
  2. Strange java syntax
    By weps in forum New To Java
    Replies: 3
    Last Post: 09-07-2011, 05:27 PM
  3. What type of java syntax is this?
    By castiel in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 06:59 PM
  4. Java Syntax If statement
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 09-01-2010, 12:21 PM
  5. Java syntax I don't understand - assignment
    By poet in forum New To Java
    Replies: 2
    Last Post: 09-19-2009, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •