aspose file tools
The moose likes Beginning Java and the fly likes Method that take all primitive values..? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Method that take all primitive values..?" Watch "Method that take all primitive values..?" New topic
Author

Method that take all primitive values..?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all i want to know the method like that takes all primitive values as parameter and it should print that value inside the method...Please let me know


Creativity is nothing but Breaking Rules
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 536
Well taking all primitive values.....you can use var-args but this will allow on one type of primitives and print it using the index of the value like


I hope this is what you are talking about.....


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Nitish wrote
I hope this is what you are talking about.....

Please read this carefully
want to know the method like that takes all primitive values as parameter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
The original question is not clear, but I think you ought to be showing us what you think you ought to write. Please have a quick look at some of our FAQ, Santosh_R Gowda.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
The original question is not clear

Dear Sir i was about to ask you that i want to design my method that it should take all primitive data types as parameters and it should be of that type only inside the method.this concepts is used in C++ as templates
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8439

santhosh.R gowda wrote:
Please read this carefully
want to know the method like that takes all primitive values as parameter


Santosh,
You really need to work on your communication skills.

Like Campbell said, your original post is very confusing.
Nitish posted a probable solution based on what he could make of it. He was also kind enough to ask you, if he understood your problem correctly.
If it was clear he you had not managed to convey the meaning properly, re-quoting your original post hardly helps clear out the meaning does it?

Also, "please read this carefully" implies that the fault is on Nitish's part, of giving your post a cursory glance, which is not the case.
Please make sure people understand your posts and are not offended.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19


The easiest option is probably with Method Overloading -- but technically, that isn't "one method". Another option is to have the method take an Object parameter, and rely on autoboxing to allow the method to be called with primatives.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Another option is to have the method take an Object parameter, and rely on autoboxing to allow the method to be called with primatives.

Ya i agree with you but taking as Object as a parameter will convert primitive to object .
i want exactly like this in primitive
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Ya i agree with you but taking as Object as a parameter will convert primitive to object .
i want exactly like this in primitive


Nope. Won't work. There is no option to have a single method take multiple primative types -- as primatives. If you want to provide this option to your callers, you have to do some work -- either create a bunch of overloaded methods, or type and unbox the object back to primative. There is nothing in the current specification that will allow you to do it without doing some work.


As examples, (1) take a look at the printstream println() methods to see a bunch of overload methods, or (2) take a look at the printf() method to see the autoboxing option. Both provide the "feel" of a single method taking any primatives.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method that take all primitive values..?
 
Similar Threads
Wrapper classes constructors
heap memory
Output or Compilation error? Surprising!
question on reflection
Where to declare method-level variables