Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...

 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thansk for the tip will keep this in mind
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic