+ 4
i think something is wrong. result is coming as 0.I would appreciate any help.
public class Program { public static void main(String[] args) { res vg = new res(); vg.y=5; res gr = new res(); gr.x =10; res h = new res(); h.u=30; res q=new res(); q.hello(); } } class res { int y; int x; int u; void hello(){ System.out.println((y+x)-u); } }
1 Odpowiedź
+ 10
when you create any new integer veriable in java ,its value is zero by default untill you change it..
in above code you are creating 4 diffrent objects of the class "res" , and as object is an instance of the class new memory is allocated for each object i.e
in your code.
objects "vg, gr , h & q " are different and different memory is allocated for each object.
hence
vg.y , vg.x, vg.u are different than gr.y, gr.x, gr.u and so on..
you have assigned values for
vg.y =5;
gr.x=10;
h.u=30;
where q.y , q.x , q.u are not assigned with any value hence thair value is 0 by default. thats the reason you are getting result as 0 "System.out.println((0+0)-0);"