wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rgarding datatypes" Watch "Rgarding datatypes" New topic
Author

Rgarding datatypes

Simrat Singh
Greenhorn

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;
System.out.println(a.toString());

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
Greenhorn

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;
system.out.println(a);
}

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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rgarding datatypes
 
Similar Threads
Why arrays are object ?
Hibernate Update ?
Some issues related with interfaces
HttpSessionEvent.class is a pre-defined class or what?
DB app: complex text structure