Solved

check for datatype using instanceof

Posted on 2011-09-12
7
354 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven example error 3 52
HTTPSessionBindingListernter not configured in deployment descriptor 2 32
print map entry 34 53
diffSum example 4 20
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

932 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

11 Experts available now in Live!

Get 1:1 Help Now