Solved

check for datatype using instanceof

Posted on 2011-09-12
7
358 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
MySQL  on Tomcat 8 70
jsp insert to database example 2 47
What is the use of Forwarding Class in java 1 13
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

829 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