Solved

check for datatype using instanceof

Posted on 2011-09-12
7
353 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now