This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What will be the output....Please explain

 
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 myclass extends Thread {
static String a="Test";
public static void main(String[] args){
myclass m=new myclass();
m.amethod(a);
System.out.println(a);
}
public void amethod(String a){
a = a+" thread";
start();
}
public void run(){
for(int i=0;i<4;i++){
a = a+" "+i;
}
}
}
------------------
Cheers
Tamanna :-)
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The output of this program will be Test.
When we pass String a to amethod, I think it is considered as a local variable in that method.
if you change your code like this, then the value of a is assigned to Test Thread.


Vanitha.


 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i got it. Also I am confused that in the run method when we are accessing the static variable directly. Still it is showing the initaial value "test thread' and not the int values added to the string why is it so? can somebody explain it to me please.
 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also vanitha
if i compile and run the under given code it gives me a different output. but the only difference here is that i have added 2 print statements in the original code.
public class myclass1 extends Thread {
static String a="Test";
public static void main(String[] args){
myclass1 m=new myclass1();
m.amethod(a);
System.out.println(a + " from main");
}
public void amethod(String s){
a = s+" thread";
System.out.println(a + " from amethod");
start();
System.out.println(a);//1 new print statement.
}
public void run(){
for(int i=0;i<4;i++){
a = a+" "+i;
}
System.out.println(a);//2 new print statement.
}
}
Output:
Test thread from amethod
Test thread 0 1 2 3
Test thread 0 1 2 3
Test thread 0 1 2 3 from main
Y is it so that just by adding 2 print statements the output in the main function is changing.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tamanna,
I also added 2 lines, but I got different outputs,
Test thread from amethod
Test threadfrom amethod after start
Test thread from main
Test thread 0 1 2 3
The reason is , main() is a seperate thread, and we start another another thread from amethod, so there are 2 threads with same priority.
which thread will run first depends on the system. if amethod thread starts first, the output will be like your output.
In my case I think the main thread got invoked first so I got the this output.(a 's value didn't have 0..3 from main)
If I yield() from the main the output is like this,
Test thread from amethod
Test threadfrom amethod after start
Test thread 0 1 2 3 from run
Test thread 0 1 2 3 from main

Correct me, if I am wrong,
Vanitha.
 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nopes i ma not getting the point Y u made another instance of thread class. Can u please explain it to me again coz., i am getting the same output as u even after hiding those lines which u have entered. this is really confusing.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, We can just add yield() method, we don't have to get the current thread.
if I comment the yield() my o/p is
Test thread from amethod
Test threadfrom amethod after start
Test thread from main
Test thread 0 1 2 3 from run
if I add yield() my o/p is
Test thread from amethod
Test threadfrom amethod after start
Test thread 0 1 2 3 from run
Test thread 0 1 2 3 from main
Vanitha.
 
What's gotten into you? Could it be this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic