Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

check for datatype using instanceof

Posted on 2011-09-12
7
Medium Priority
?
370 Views
Last Modified: 2012-05-12
hi


i am checking for datatype using instanceof. is that a good practice?

my code
Object escFreq = (Object)rowData.get("FREQ");  //not sure what the datatype here is , so using Object.is that good practise?
if(escFreq != null){
if (escFreq instanceof Long){  //using instanceof here, is that good practise?
Long escaFreq = (Long)rowData.get("FREQ");            
caladminEditVO.setFreq(escaFreq.intValue());
}else{
Sring escaFreq = (String)rowData.get("FREQ");
int escalaFreq = (int)Integer.parseInt(escaFreq);
caladminEditVO.setFreq(escalaFreq);
}
}

thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 36524351

normally instanceof is a good practice,


you don't need need to cast to Object

just Object escFreg = row.get(...); should be OK

Integer.parseInt(escaFreq);
 returtbns int, so you don't need tp cast to int


 
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36524382

So you meant it could contain either Long or String and you want tio distinguish between these two using instanceof
that's quite OK

It is usually not the best parctice to put in one Map ovbject eithert Long's or String's but if someone did it you can
distinguish between the two using instanceof - that's fine
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36524411
Also consider catcjhing the exception - if this is String - either it represents a number or not
I you know fior sure it does - then it's fine, otherwise Integer.parseInt() may throw execption

I guess also if you may have too big long numbers you may also have problems with method intVlaue()
If you knoe the values are reasonable then you may not need to worruy about it
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 36524922
>>i am checking for datatype using instanceof. is that a good practice?

Well it's not bad practice, but if you can, avoid that, as type-checking brings overhead to your code
0
 

Author Comment

by:royjayd
ID: 36526044
So How will I be able to avoid that
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36526054

In this case you probably would not be able to avoid it, and it is realy not necessary.
In general if you crearte your own Maps, undless it is absolutely not other way don't compbine String and Long types in the values

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 36528045
>>So How will I be able to avoid that

That would be down to the design of your 'rowData' class, which maybe something over which you have no influence. If so, don't worry about it
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question