• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a Question from Jtips.net

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
static int x = 9;
static int y = 'V';

public static void processInt(int i)
{
i = 18;
process(y);
}

public static void process(long l)
{
l = 27;
}

public static void process(short s)
{
s = 36;
}

public static void main(String[] args)
{
processInt(x);
System.out.println(x);
System.out.println((char)y);
}
}
can some one please explae the answer to me.
Correct answer is 9 and 'V'.
Thanks in advance
Amit
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of variables x and y never change and so when you print x and y you get the original values (char value for y because of the cast).
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit,
To be clear, primitives passed to methods in Java can not be changed because Java passes by value. Therefore in the case of process( long l ) method changing the value of l does not affect the reference y used as parameter to the method. That is because a copy of 'y' was passed and called 'l' in the method!
Manfred.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the variable x is just used as a passing argument, no one manipulate it thats why it have is original value. Regarding y , first it is converted to int giving y=86 then when you are calling process(y) it calls
public static void process(long l)
{
l = 27;
}
but in this method you are not using y's value so after returning when you are using
System.out.println((char)y);
it explicit cast 86 to character V again.
if change your code :
public class Test2
{
static int x = 9;
static int y = 'V';
public static void processInt(int i)
{
i = 18;
process(y);
System.out.println(y);
}
public static void process(long l)
{
l = 27;
System.out.println(y);
System.out.println(l);
}
public static void process(short s)
{
s = 36;
System.out.println(y);
System.out.println(s);

}
public static void main(String[] args)
{
processInt(x);
System.out.println(x);
System.out.println((char)y);
}
}
you get output as
86
27
86
9
V
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was wrong. Actually when you are passing some argument, you are actually passing copies not actual so in above example orignal values are not changed so output is 9 V
thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static keyword is given to confuse us. Really I myself got confused at first time.
 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic