Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Not just another Primitive Data Type Question ? Why Priimitive ? Varargs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not just another Primitive Data Type Question ? Why Priimitive ? Varargs " Watch "Not just another Primitive Data Type Question ? Why Priimitive ? Varargs " New topic
Author

Not just another Primitive Data Type Question ? Why Priimitive ? Varargs

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124


Question 1:
So the 8 primitive types are as i read...
byte,int,char,short,long,float,double,boolean

I was reading the book Core Java (An excellent book.. especially for C++ Developers)
anyways is it safe to assume that Primitive data types are those data types whose base class is not an Object class. (those which have not been derived from object class)?
Since the Superclass of every other class is Object class.

Question 2:
While reading on varargs..
consider the signature of a function
public myfunction(String... Something)
is the above signature equivalent to
public myfunction(String[] Something) ?
The reason why i asked is because in for each loops such as this i occasionaly come across
for(String element: Something)
{....} ..... Now if Something was not a collection the above for wouldnt have been valid... ??

Any suggestions,or response that might further clarify my concepts on the above topic would be appreciated thanks...


Don’t look where you fall, but where you slipped
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

anyways is it safe to assume that Primitive data types are those data types whose base class is not an Object class. (those which have not been derived from object class)?
Since the Superclass of every other class is Object class.


Java Classes are different from C++ classes:

In C++ when you declare an object for a class for example: A objA = new A(); then objA variable actually holds the content of objA in memory. (If I am not forgetting C++ :P)

But in java if you write A objA = new A(); then objA variable actually holds a kind of pointer to the content of objA in memory.

so you can think of it like this: that A objA = new A(); in java is "somewhat" similar to A* objA, in C++. (But in java these are called references not pointers)


Primitives: Primitive data types are byte,int,char,short,long,float,double,boolean. But These are "Not" classes in java.

If you declare a primitive variable like int a = 1; then variable a will hold the content of a in memory (not the reference to the content).


Piyush
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
1) A general rule in Java is "whatever is not an object must be a primitive".

2) Both the arguments are equivalent. If both are present in a class as overloaded methods/constructors, if myfunction(String [] something) is called, the one with array argument will be selected before the var args.

Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207


Varargs and Array as method parameters are certainly not equivalent.

If i have a method with varargs: void checkVarargs(String... list){} then I can pass 0 or many arguments to this method.
Like checkVarargs();checkVarargs("1");checkVarargs("1","2");checkVarargs("1","2","3"); etc.

But if you have void checkArray(String[] list){} then you surely can not call checkArray();
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Piyush Joshi wrote:
Varargs and Array as method parameters are certainly not equivalent.

If i have a method with varargs: void checkVarargs(String... list){} then I can pass 0 or many arguments to this method.
Like checkVarargs();checkVarargs("1");checkVarargs("1","2");checkVarargs("1","2","3"); etc.

But if you have void checkArray(String[] list){} then you surely can not call checkArray();



thanks for pointing that out.. I didnt say that varargs and array methods are the same thing .
I am aware of the fact that when using varargs the compiler wraps the parameters of the calling function into an array
example checkVarargs("A","B","C") the compiler passes it as checkVarargs(new String[]{"A","B","C"})
What i wanted to confirm is that when inside the function
public checkVarargs(String... list)
{ ..... the list here is treated and is supposed to be considered as a collection}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Adam Zedan wrote:
thanks for pointing that out.. I didnt say that varargs and array methods are the same thing .
I am aware of the fact that when using varargs the compiler wraps the parameters of the calling function into an array
example checkVarargs("A","B","C") the compiler passes it as checkVarargs(new String[]{"A","B","C"})
What i wanted to confirm is that when inside the function
public checkVarargs(String... list)
{ ..... the list here is treated and is supposed to be considered as a collection}


As you mentioned, that you are aware, that var-args are accomplished via arrays. I guess the Java designers could have prevented the usage of the parameter as an array but decided not to -- arguably there is no reason to prevent it, as it is really is an array.

I just consider it just a quirk, and try not to use var-args as arrays when possible.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Thansk for the tip will keep this in mind
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Adam Zedan wrote:
Question 1:
So the 8 primitive types are as i read...
byte,int,char,short,long,float,double,boolean

I was reading the book Core Java (An excellent book.. especially for C++ Developers)
anyways is it safe to assume that Primitive data types are those data types whose base class is not an Object class. (those which have not been derived from object class)?
Since the Superclass of every other class is Object class.



Primitive tyoes are not classes. So, there is no super class or base class to check. It is simply not a class.

Henry
 
GeeCON Prague 2014
 
subject: Not just another Primitive Data Type Question ? Why Priimitive ? Varargs