• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

two basic problem reg. valueOf amd Thread

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends.
Every time got a wonderful answers from javaranch friend.i am poting these two question hoping for same.
=================================================================
1.) char []a = null;
int []b = null;
System.out.println(b) //returns null String
System.out.println(a)//throws runtime Exception "NullpointerException"
Why ? Please Explaein It.
===============================================================
2.) i have got book from sun microsystem press "Advance java programming" volume - II
in the chapter of MultiThreading There is CAUTION:
"don't call run method directly on thread object please call start() method to start your Thread. otherwise your programe is not multithreaded"
but as far as i know start() method also call run() method to start the thread then why not we call run() method directly.
Please Explain it with too basic.
Hoping for warm reply.Thanx

 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishwas,
for your first problem it seems there may be some typo.
coz following program is running perfectly at my PC.
u also try it.


Now second problem
As u must have noticed that run() in class Thread/Runnable is like any other method in any other class of java.
so of course u can call it just like other method.and run will start executing but the thing to note is that the code in run method will be running in the same thread which called it. so no multithreading achieved.
now run method becomes special in threading because when it is called by the start() method of Thread class what happens is not similar to the ordinary call to run() method instead before calling it start method initiates a Thread (which may include allocating some local memory for the thred,setting up a program counter,and putting this thread in the pool of active threads.)and then whenever the scheduler schedules this thread to run then only the run method of this thread is called.
and that's the difference between
hope this is somewhat clear
otherwise do get back
regards
Deekasha
Deekasha
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//=======================sorry about that================
System.out.print("int : "+a);
System.out.print("char : "+b);
//======================repace it with this ====================
System.out.print("int : "+a);
System.out.print("char : "+b); //this line will give runtime error
//======================or with this ================
String.valueOf(a);
String.valueOf(b);
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deekasha,
I tried the code and it is Compiling perfectly NOT running. It is giving a run time exception NullPointerException.
I think some expert advise is required here.....
Kalidas.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to question 1.
The code that deekasha provided does work and it does run, however, if you take out the "chars " part and just do
System.out.println(b)
You get a runtime error.
I tried this code to see what the other primatives do:

And then just changed what I printed, and you only get the runtime error with chars. Now why is this so, I am not sure about, and will you ever run into this with a real program, I am not sure about that either becuase doing a toString call on an array doesn't print out the contents of the array, it just prints out a hash code of the object. But it is still interesting why the char will give you the error, while all the other primatives work. chars are different than the other primatives as they are the only unsigned byte, but really not sure of the answer.
Bill
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishwas,
I've found out the reason why the char[] printing behave s differently. before going in to that let me state a few things.
----------------------------------------------------------
Here are the overloaded version of println()
method in PrintStream class
----------------------------------------------------------
voidprintln()
voidprintln(boolean x)
voidprintln(char x)
voidprintln(char[] x)
voidprintln(double x)
voidprintln(float x)
voidprintln(int x)
voidprintln(long x)
voidprintln(Object x)
voidprintln(String x)
--------------------------------------------------------
Now if u go through the above list then u'll find that only char [] has got a separate println method.(There is no version of println that deals with other kinds of arrays.)
now the question arises then how are other arrays dealt with ..... And the answer is all other arrays are dealt by println(Object o) version.
Hence It's reasonalble that all arrays except char[] array may behave differently.
Ant now to the main question .. Why NullpointerException for char[].
The reason lies in the fact that the function that deals with char[] printing does not check if the char[] is null ,instead it directly access the charArray.length variable. and it gives rise to an exception if our array was null.
Now let's have a look for other arrays also. For all other arrays as I've stated earlier Object version of println is called and in this function Object.toString() function is called to get the String rep of the Object but before that it is ensured that the Object is not null and in case it is null then instead of calling toString() method a hardcoded "null" String is returned.and hence no exception.
hope this is clear now.
Thanx Vishwas for posting a good ques. I learned this because of ur ques. keep posting.
regards
deekasha


 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Gunwant.
thanx for your research work. but what i feel that still things are not clear technicaly.as you said
//---------------------------------------------------------------
"Hence It's reasonalble that all arrays except char[] array may behave differently."
//---------------------------------------------------------------
Same problem with me why char Array is different from others.

try some more technical stuff here i am trying my best
thanx for your such a nice response
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deekasha,
I did some more digging on your lines and found the following.
The calling stack for PrintStream.println(char []buf) will look something like this.
BufferedWriter.write(char []buf)
...
PrintStream.write(char []buf)
...
PrintStream.print(char []buf)
...
PrintStream.println(char []buf)
In BufferedWriter.write(char []buf) call, the length of the array is checked which will throw NullPointerException for null arrays.
For all other arrays the calling stack will be like this:
PrintStream.write(String.valueOf(Object o))
...
PrintStream.print(Object o)
...
PrintStream.println(Object o)
The String.valueOf(Object o) call will return the String literal "null" for all null objects which will be output to the out stream.
Thank You all.
Sanjib.
[This message has been edited by Sanjib Talukdar (edited December 19, 2000).]
[This message has been edited by Sanjib Talukdar (edited December 19, 2000).]
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjib,
yes exactly.
I did not mention all this stuff to keep things short but seems made it tooooooooooo short :-)
regards
Deekasha
 
reply
    Bookmark Topic Watch Topic
  • New Topic