Invoke function from ternary operator


Joined
Feb 3, 2022
Messages
3
Reaction score
0
Hi all, can anybody help me please. Have three questions.
1. how function private String addHttp(String site) knows where to save/to assign output from return?
2.Whats the meaning of variable (site) in brackets...at the end of this line.. site = site.startsWith("http") ? site : addHttp(site);
3. String site and then site is the same , right? (Line 2 and line 3 of code)
Thanks for helping

Code:
public void moreTernary(){
String site= "www.eviltester.com";
site = site.startsWith("http") ? site : addHttp(site);   // ?
}
private String addHttp(String site)
return "http://" + site;
 
Ad

Advertisements

Joined
Apr 25, 2017
Messages
208
Reaction score
26
1. In Java programming, the return statement is used for returning a value. So "https://"+site will be returned from addHttp method and then assigned to site variable.

2. You are passing site value to addHttp method.

3. No. If the site doesn't start with http, it will overwrite by the value returned from addHttp method.
 
Joined
Feb 3, 2022
Messages
3
Reaction score
0
1. In Java programming, the return statement is used for returning a value. So "https://"+site will be returned from addHttp method and then assigned to site variable.

2. You are passing site value to addHttp method.

3. No. If the site doesn't start with http, it will overwrite by the value returned from addHttp method.
a) Well output from function private String addHttp(String site) will be returned to here: site = site.startsWith("http") ? site : addHttp(site);
meaning (highlighted variable site )?? or rewrites String site?

b) : addHttp(site); (3th line) well if here I am passing value of site variable to addHttp method what am I doiing here: private String addHttp(String site) (5th line of code)

Thanks for help a lot and sorry for maybe silly questions for you
 
Last edited:
Joined
Feb 3, 2022
Messages
3
Reaction score
0
1. In Java programming, the return statement is used for returning a value. So "https://"+site will be returned from addHttp method and then assigned to site variable.

2. You are passing site value to addHttp method.

3. No. If the site doesn't start with http, it will overwrite by the value returned from addHttp method.
Basically addHttp(site); value of variable (site) is www.eviltester.com and this value is passed to method or function private String addHttp(String site), ok?
 
Joined
Apr 25, 2017
Messages
208
Reaction score
26
a) Well output from function private String addHttp(String site) will be returned to here: site = site.startsWith("http") ? site : addHttp(site);
meaning (highlighted variable site )?? or rewrites String site?

site = site.startsWith("http") ? site : addHttp(site);

Output from addHttp will returned to green color site. The black color site and blue color site value is depends on String site= "www.eviltester.com";

b) : addHttp(site); (3th line) well if here I am passing value of site variable to addHttp method what am I doiing here: private String addHttp(String site) (5th line of code)
If you are passing site to addHttp, the site will be added "http" in front.

 
Ad

Advertisements

Joined
Apr 25, 2017
Messages
208
Reaction score
26
Try to run the program by printing out the site value. It is easy for you to understand.

Java:
public class MyClass {
    public static void main(String args[]) {
        MyClass myClass = new MyClass();
        myClass.moreTernary();
    }


    public void moreTernary() {
        String site = "http.eviltester.com";
        site = site.startsWith("http") ? site : addHttp(site);
        System.out.print(site);
    }
    private String addHttp(String site) {
        return "http://" + site;
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top