Solved

check for datatype using instanceof

Posted on 2011-09-12
7
362 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 300 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

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

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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

687 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