Results 1 to 6 of 6

Thread: why null is assign to main and container in java code?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    why null is assign to main and container in java code?

    Hello friends,
    I am second year Computer Science student. I recently started learning java language. I find java language very interesting. In one of the java book I have seen following code, but I didn't understand it.
    Code:
    JComponent container = menu == null ? popupMenus : menus;
    I don't know why null is assign to main and container in java code? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: why null is assign to main and container in java code?

    In this code ternary operator is used. It is used to check for condition, if condition is right then you will get value before "?" and if condition is false then you will get value after "?". You can also write above code in the following ways.
    Code:
    JComponent containers;
    if (menus == null) {
      containers = popupMenus;
    } else {
      containers = menus;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: why null is assign to main and container in java code?

    Hey you have wrong code. You have to include following condition in parenthesis like "if (menu == null)" to fix this problem. In your code you have use ternary operator which is shorthand for an if-then-else statement, because it used three operands. If condition in If statement is right then first value is assigned to variable or if condition become false the second value assign to variable.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: why null is assign to main and container in java code?

    It is one of the most complex version of ternary operator. It is very easy to understand. You can also right above code in the following ways.
    Code:
    JComponent container;
      if (menus == null)
           containers= popupMenu;
      else
           containers = menus;
    Meaning of above code and your code is the same.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: why null is assign to main and container in java code?

    Code:
    JComponent containers = ((menus == null) ? popupMenus : menus);
    This code compares menu with null. If condition is right then popupMenu is assigned to containers and if condition is wrong then menu is assign to containers.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: why null is assign to main and container in java code?

    Code:
    JComponent container = menu == null ? popupMenus : menus;
    Meaning of above code is such that If menu is null then set container to popupMenu and if menu is something then set container to menu.

Similar Threads

  1. Problem with a null string in java
    By Rily in forum Software Development
    Replies: 4
    Last Post: 12-08-2010, 10:23 AM
  2. Unable to assign null value to an event
    By Migueel in forum Software Development
    Replies: 4
    Last Post: 03-03-2010, 10:45 PM
  3. How to write a Container Listener in Java?
    By Pratim in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:43 AM
  4. In Java Script how to Check Null Value?
    By taher in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:18 AM
  5. Concept of Scrollpane Container in Java
    By Adrina_g in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 09:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,134,721.63452 seconds with 17 queries