• 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

Queries.......Need help

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Pls chck the code below:

public class FirstThread
{

public static void main(String[] args){
MyThread t=new MyThread();
t.start();
}

}

class MyThread extends Thread
{
public void run()
{
for (int x=0; x<5;x++)
System.out.println("In MyThread");
}


}

I tried run() method of MyThread from within itself using t.start().
But it didn’t work. Why this happened? Pls see the code below.

class MyThread extends Thread
{
public void run()
{
for (int x=0; x<5;x++)
System.out.println("In MyThread");
}
public static void main(String[] args){
MyThread t=new MyThread();
t.start();
}


}
Why it cant create new call stack within itself.

2)
Shd program handle NullPointerException using try/catch block.
If no why. And what happens if we try to handle it as its unchecked runtime exception.

3) why cant we use SOP like this:
class TrySOP{
int i;
System.out.println(“hi I m inside class” ;
public static void main(String[] args){
}

}
4) why wait(), notify() and notifyAll() are included as part of
java.lang.Object and not in java.lang.Thread class

5) how does throws clause work.
If we invoke a method from main and suppose it throws NumberFormatException
is it necessary for main method to handle/declare to be thrown.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Both of the pieces of code are OK. What do you mean with "it didn't work"? Did you get an error message? What was the error message?

2) It depends. For most applications, doesn't really make sense to catch an exception from which the program can't recover in a meaningful way. A NullPointerException usually an unexpected error that indicates that there is a serious problem, a bug, in the code. The program most likely can't do anything about it if a NullPointerException happens, so it is most likely not very useful to try and catch it. Catching and handling unchecked exceptions works in the same way as with checked exceptions.

3) Because statements must be inside methods. When would you expect that SOP statement to be executed if you write it like that?

4) Because you can synchronize on any Object, not just on a Thread object.

5) Only for checked exceptions it is mandatory to either catch the exception or declare it in a throws-clause, to indicate that your method lets the exception pass higher up the call stack. For unchecked exceptions this is not necessary. NumberFormatException is a subclass of RuntimeException, so it is an unchecked exception.
 
Shaily Sharma
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jesper.
For the first Q, the second code snippet compiled well n no runtime exception. But the code written in run() method didnt execute.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure? I tried it out and the code was executed as expected.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic