The Artima Developer Community
Sponsored Link

Java Answers Forum
type casting

2 replies on 1 page. Most recent reply: Jun 23, 2003 2:22 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Tim Doyle

Posts: 3
Nickname: schumy
Registered: Jun, 2003

type casting Posted: Jun 23, 2003 10:54 AM
Reply to this message Reply
Advertisement
Hi,

I am using hash tables to store information of
type "long". However all information from hastables is returned as type "Object". I was wondering if anyone knew how I could convert from type Object to long.


Thanks very much for the help,
Tim.


Namita

Posts: 3
Nickname: nam
Registered: Jun, 2003

Re: type casting Posted: Jun 23, 2003 12:25 PM
Reply to this message Reply
Long is a subclass of Object, so u need not worry about converting Long to Object
Hashtable numbers = new Hashtable();
numbers.put("one", new Long(longvalue));

So dont bother about conversions


-Namita

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: type casting Posted: Jun 23, 2003 2:22 PM
Reply to this message Reply
You can cast the object type to Long as
Long lValue = (Long) your_object_ref_from_hashtable;

Then to get primitive long value you can use lValue.longValue()

The following code sample demostrate it in details.
import java.util.* ;
 
public class ABC {
	public static void main(String[] args) {
		Hashtable ht = new Hashtable() ;
 
		// Put a value
		Long value = new Long ( 142 ) ;
		ht.put ( "myvalue", value ) ;
 
		// Get the value
 
		Object obj =  ht.get ( "myvalue" ) ;
 
		if ( obj instanceof Long ) {
 
			// Cast object to Long type
			value = (Long) obj;
 
			// To get long value from value use its longValue method
			long longValue = value.longValue();
			System.out.println ( "Long value is :" + longValue ) ;
		}
 
 
 
	}
}

Flat View: This topic has 2 replies on 1 page
Topic: change the point: 3.388443E-4 --> 0,0003388 Previous Topic   Next Topic Topic: java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use