aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.lang.Void  and void ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java.lang.Void  and void ??" Watch "java.lang.Void  and void ??" New topic
Author

java.lang.Void and void ??

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi
as per API:
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.
Que 1. if "uninstantiable" then what is the use of this class ???


again from API:
The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
Que 2. What is "void"? what is it use? Is void primitive type?(As per JLS it should not be primitive type !! and it is also not reserved word)
Thanks in adv.

------------------
Regards
Ravish


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

void is a keyword. Check out the JLS Keyword list.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040


Que 1. if "uninstantiable" then what is the use of this class ???

There are some classes in Java that you don't have to instantiate to use them. Like the Math class.
The adv is that you can use its methods without instantiating
an object of that class. In general, a lot of the classes in
the util package are like this.
As the JSL says Void is a placeholder. Currently,
it maynot be doing much but maybe Sun wanted to use this name
for a std class so that others don't accidentally use it or
to make it consistent with the other classes for primitives.
regds.
- satya
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Madhav Lakkapragada:

void is a keyword. Check out the JLS Keyword list.
regds.
- satya

Agreed 'void' is keyword.
My question is "what is the use of void and Void?"
And API at one place(java.lang.Void) says void is primitive type and then at other(java.lang.Class)diffrentiate it from primitive types and says that it is keyword.
I repeat my que:
"what is the use of void and Void?"
Thanks in adv

------------------
Regards
Ravish
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
hi Madhav
thanks for Que 1.
now Que 2 still remains:
is void primitive type or not?
Void class has public member TYPE of type Class.
from API:
static Class TYPE
The Class object representing the primitive Java type void.
but JLS does not confirms it.
AW Thanks for que 1.


------------------
Regards
Ravish
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

now Que 2 still remains:
is void primitive type or not?
Void class has public member TYPE of type Class

I see your confusion and agree with you.
But then, personally, I wouldn't pay much attention.
Or contact JSL @ jls@java.sun.com
I will try sending this discussion to them and
see what they have to say. (I don't expect any positive answer though)....
regds.
- satya
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Thanks Madhav,
I have send a mail to them ..
let us see..
AW thanks a lot ....
will you plz visit this link and give answer: http://www.javaranch.com/ubb/Forum24/HTML/011944.html
TIA (Thanks In Adv)

------------------
Regards
Ravish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
void is not a primitive type since it does not represent any type of value. "void" just states that no value is returned from a method and nothing else. There is no variable of type void (like it is possible to do in C). "void" is just a placeholder.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
void is not a primitive type since ...
in which case the API for the class Void
needs some attention.
- satya
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I agree with that but if you take 2 secs to think about what a primitive data type is you'll quickly find out that void cannot be a primitive type and that the guy who wrote the docs in the API had a hangover from the night before ...
Seriously, neither The Java tutorial nor some Java books I have here mention void as being a primitive type. The only place where I saw that is in the API and it doesn't make any sense.
Anyway we are playing on the words here, but void is not even mentioned in the section 4 of the JLS which deals with data type, primitives and their values...
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
tricky topic...
If you read some documents about the Java language (tutorial...) they only mention three different types of primitive data types, namely:
- numeric types (byte,short,int,float,long,double)
- character type (char)
- boolean type (boolean)
I couldn't find any mention of a "void" primitive data type except in the API. I think the JVM treats void as being a primitive type to make things easier but we all know that a primitive type is for handling a specific type of data which void is not.

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I have found a reason for the keyword void being wrapped as a Class object. In java.lang.reflect.Method there is a method as follows

public Class getReturnType()
Returns a Class object that represents the formal return type of the method represented by this Method object.


SCJP2. Please Indent your code using UBB Code
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
this is Void.java:
<PRE>
public final
class Void {
/**
* The Class object representing the primitive Java type void.
*/
public static final Class TYPE = Class.getPrimitiveClass("void");
/*
* The Void class cannot be instantiated.
*/
private Void() {}
}
</pre>
Plz any comment ??
TIA

------------------
Regards
Ravish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Again the guy who developed those classes had a hangover from the night before... You know that copy-pasting can be at the origin of that problem, they wrote Integer, Double,... and finally Void and didn't really care to give a appropriate definition of void.
the comment should be
The Class object representing the placeholder void used to indicate that a method does not return any value.
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 18, 2001).]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
If I agree with you ... then
Hey SUN guys .... are you listening ??
plz do not confuse us with such complexities....
I wasted/invested(God knows) 2-3 hrs in this....
now going to sleep(11:30pm IST).... home is also one hr form office...
AW thanks a lot guys ....

------------------
Regards
Ravish
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Jose Botella:
I have found a reason for the keyword void being wrapped as a Class object. In java.lang.reflect.Method there is a method as follows

public Class getReturnType()
Returns a Class object that represents the formal return type of the method represented by this Method object.

I think You are right
thnx a lot

------------------
Regards
Ravish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
good explanation Jose
and good night Ravish
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There is still a problem though, I can not find Class.getPrimitiveClass in the API
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
it is a method with default accessibility in the class Class and thus does not show up in the APi since you can't use friendly methods of the java core packages...


------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
At 08:06 AM 12/18/01, satya@javaranch.com wrote:
>Hi:
>
>During a recent discussion of the keyword "void" we ran into this
>confusion and though we will bring it to your attention.
>
>http://www.javaranch.com/ubb/Forum24/HTML/013732.html
>
>
>In the API for class Void, "void" is mentioned as a primitive.
>However, in the JLS section 4.2 of the Second Edition
>(http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#85587)
>"void" is NOT identified as a prmitive type. This leads to the confusion
>among some Java developers.
>
>Please let us know if this is a typo.
The Java Language Specification is correct in stating that void is not a
type, primitive or otherwise. You cannot use void as the type of a variable
of any kind, whether it be a method argument, a class variable, instance
variable, local variable, array component etc. The keyword void can be used
in the position normally reserved for the return type of a method, to
indicate that the method does not return a value.
The reflection APIs provide allow you to reflect this syntactic entity, so
that one can reflectively construct signatures of methods that use void in
their signature.
There is a constant temptation to use terms like "method with return type
void" etc., but this should not be taken too literally. Maybe we should
rework the terminology.


Cheers, Gilad
*********************************************
Gilad Bracha
Computational Theologist
Sun Java Software http://java.sun.com/people/gbracha/
------------------
Regards
Ravish
[This message has been edited by ravish kumar (edited December 19, 2001).]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
>>but this should not be taken too literally.
I forgot to take literally the "checked Exception" word and kept in mind that overridden method can not throw any new Exception.
Then one day in JavaRanch mock test I was bowled for not taking it literally as over ridden method can throw unchecked exception.
After that I try to give value to EACH word. At least in APIs and JLS.
I wish this ambiguity will be removed ASAP.

------------------
Regards
Ravish
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Thanks, ravish.
I was thinking of posting the Sun reply here.
- satya
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Well, I'm glad that things are cleared up
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
good news ..
In JDK1.4.0 they have changed Void class documentation.
Bad news Field summary still has effect of hangover
[ April 09, 2002: Message edited by: Ravish Kumar ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Amazing they didn't change that field documentation :roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.Void and void ??