jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Rgarding datatypes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rgarding datatypes" Watch "Rgarding datatypes" New topic

Rgarding datatypes

Simrat Singh

Joined: Mar 31, 2007
Posts: 6
Dear friends,
I am a java newbee and while i was learning java i found somethings quite confusing for me. The first thing i want to ask is that, is it that in java all the datatypes are Clases i mean is int is treated as a class in java.
As per my knowledge i guess that yes all the datatypes are treated as classes in java but i want that if i am wrong then please clarify the things to me and if i am right then as per java, every class in java extends the Object class, so why i am not able to write the following piece of code

int a;

why do the java complier throws an error in the above statement as
toString() is the method of the Object class which is being extended by the int class.
While answering my question please keep this thing in mind that i am a newbee so please explain completely.

Thanking you
Simrat Singh
swapnil deo

Joined: May 30, 2007
Posts: 6
hey simrat!!
first thing a class is not a datatype..it is a collection of different datatypes..!!
by saying this i mean to say that :

class demo
int a=10;

the above example has a class named demo....and a is a variable of the "data type" int..which is same as that in c++

now object of a class is the instance of a class ie when you have a class with a method..which is known as function in c/c++ we need to create an object of that class and that object should be of the type "class"

class demo //this is the class

display() // method or the function


public static void main(string[] args)
demo d; // d is the object of the class demo
d.display(); // using this you will be able to access the class method
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
i guess that yes all the datatypes are treated as classes

No. Besides objects, there are so-called primitives: long, int, float, double and char, which behave differently than objects. In particular, primitives do not have methods (since that's an object thing).

In some circumstances, primitives and their corresponding wrapper classes (e.g., int and java.lang.Integer, or char and java.lang.Character) can be used interchangeably through a mechanism called auto-boxing, but not generally.

first thing a class is not a datatype..it is a collection of different datatypes..!![/QB]

This may be confusing. A class can very well be a datatype, e.g. Integer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Rgarding datatypes
It's not a secret anymore!