Why is this code not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why is this code not working?

I learned from "Learn" section that ojs, arrays and Strings are reference type values. So, when we pass them as arguments then if the method wants then it may change their original value. https://code.sololearn.com/cPhz3o2i6iqt/?ref=app

18th Aug 2017, 10:09 AM
Gammaburst
Gammaburst - avatar
7 Answers
+ 3
bro , in this program method "celebratebirthday" does not return any value ....thats why u hv used keyword "void" before it but in the other program , setString method returns a value ,,, so u must not use void in front of that method and also make use of return statement in it
18th Aug 2017, 10:41 AM
Changed
Changed - avatar
+ 5
Probably because there was no return value to your statement. Also, when you call a function, unless ypu replave the value, the function does nothing. Oh you could probably use globals
18th Aug 2017, 10:21 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
Ok, Gaurav. I think I understand it now. Thanks for your help.😊
18th Aug 2017, 1:36 PM
Gammaburst
Gammaburst - avatar
+ 5
David, actually that's not what I wanted to do. I wanted to change the actual String "s" by passing it as an argument to a method. Thanks for your time though.😁👍
18th Aug 2017, 1:37 PM
Gammaburst
Gammaburst - avatar
+ 1
If I HAVE to use return in these kind of codes then how come this program is working:? https://code.sololearn.com/cz5Ws94wTrmU/?ref=app
18th Aug 2017, 10:32 AM
Gammaburst
Gammaburst - avatar
+ 1
Im not 100% sure what your intending to do so i fixed a few bits maby you can work of this... public class Program{ static void changeString(String st){System.out.println(st);} public static void main(String[] args) { Program pro = new Program(); String s = "Java"; System.out.println (s); if(s.equals("Java")){ pro.changeString("Guava");} } }
18th Aug 2017, 10:47 AM
D_Stark
D_Stark - avatar
0
ah right i see. did you manage to do it?
18th Aug 2017, 1:54 PM
D_Stark
D_Stark - avatar