What does return do when used inside an if statement?

后端 未结 8 1568
醉梦人生
醉梦人生 2020-12-31 02:16

What does the return inside the if statements do in the following code?

public void startElement(String namespaceURI, String localName,String qName, 
               


        
相关标签:
8条回答
  • 2020-12-31 02:36

    The return here is probably used in order to "improve" the performance of the method, so that other comparisons are not executed, once the needed scenario is performed.

    However, it's not good practice to have multiple return points in a method.

    As stated in my comments I'd try a different approach to achieve the flow of the code in question.

    0 讨论(0)
  • 2020-12-31 02:41

    It finishes the method so the code below it, is not executed.

    0 讨论(0)
  • 2020-12-31 02:43

    Does it takes us out of the if statement and proceeds to next statement or it takes us out of the method startElement?

    It takes you out of the method.. The return statement terminates the execution of a function

    0 讨论(0)
  • 2020-12-31 02:45

    it will return what you declared in the method head (here void = nothing = it will just end the method)

    0 讨论(0)
  • 2020-12-31 02:45

    The return will end the flow of the method, and is functionally identical to using a shorter else if chain like

    /* if (localName.equals("channel")) {
        currentstate = 0; // This can be removed because it's the default below.
    } else */ if (localName.equals("image")) {
        // record our feed data - you temporarily stored it in the item :)
        _feed.setTitle(_item.getTitle());
        _feed.setPubDate(_item.getPubDate());
    } else if (localName.equals("item")) {
        // create a new item
        _item = new RSSItem();
    } else if (localName.equals("title")) {
        currentstate = RSS_TITLE;
    } else if (localName.equals("description")) {
        currentstate = RSS_DESCRIPTION;
    } else if (localName.equals("link")) {
        currentstate = RSS_LINK;
    } else  if (localName.equals("category")) {
        currentstate = RSS_CATEGORY;
    } else if (localName.equals("pubDate")) {
        currentstate = RSS_PUBDATE;
    } else {
        currentstate = 0;
    }
    
    0 讨论(0)
  • 2020-12-31 02:50

    return always takes control out of calling method.

    0 讨论(0)
提交回复
热议问题