casting

Cast void pointer to integer array

戏子无情 提交于 2020-01-09 19:24:24
问题 I have a problem where I have a pointer to an area in memory. I would like to use this pointer to create an integer array. Essentially this is what I have, a pointer to a memory address of size 100*300*2 = 60000 bytes unsigned char *ptr = 0x00000000; // fictional point in memory goes up to 0x0000EA60 What i would like to achieve is to examine this memory as an integer array of size 100*150 = 15000 ints = 60000 bytes, like this: unsigned int array[ 100 ][ 150 ]; I'm assuming it involves some

(String) or .toString()?

强颜欢笑 提交于 2020-01-09 12:20:48
问题 I have a method with an Object o parameter. In this method, I exactly know there is a String in "o" which is not null. There is no need to check, or do something else. I have to treat it exactly like a String object. Just curious - what is cheaper - cast it to String , or use Object.toString() ? Or is it same by time-/cpu-/mem- price? Update: The method accepts Object because it's the implementation of an interface. There is no way to change the parameter type. And it can't be null at all. I

Creating generic array in Java via unchecked type-cast

我的梦境 提交于 2020-01-09 06:48:07
问题 If I have a generic class Foo<Bar> , I am not allowed to create an array as follows: Bar[] bars = new Bar[]; (This will cause the error "Cannot create a generic array of Bar"). But, as suggested by dimo414 in an answer to this question (Java how to: Generic Array creation), I can do the following: Bar[] bars = (Bar[]) new Object[]; (This will "only" generate a warning: "Type safety: Unchecked cast from Object[] to Bar[]"). In the comments responding to dimo414 's answer, some people claim

Difference between casting to String and String.valueOf

前提是你 提交于 2020-01-09 04:16:23
问题 What is the difference between Object foo = "something"; String bar = String.valueOf(foo); and Object foo = "something"; String bar = (String) foo; 回答1: Casting to string only works when the object actually is a string: Object reallyAString = "foo"; String str = (String) reallyAString; // works. It won't work when the object is something else: Object notAString = new Integer(42); String str = (String) notAString; // will throw a ClassCastException String.valueOf() however will try to convert

Java Buy method

怎甘沉沦 提交于 2020-01-07 08:08:34
问题 This is my home work. I am trying to write a method 'buy' that allows some shares of the stock to be bought at a given price. The method takes two arguments: a number of shares as an int, and a price per share as a double. For example: Stock myStock = new Stock("FCS"); myStock.buy(20, 3.50); // buys 20 shares at $3.50 // myStock now has 20 shares at total cost $70.00 myStock.buy(10, 2.00); // buys 10 shares at $2.00 // myStock now has 30 shares at total cost $90.00 My Code : public static

Floating Point Casting in Java

烂漫一生 提交于 2020-01-06 21:29:50
问题 Casting for integers is very straightforward, the extra bits simply disappear. But, is it important to understand what is happening under the hood for casting floating point? I've tried to read information on how floating point is calculated, but I have yet to find one that explains it well. At least that's my excuse. I get the basic idea although the calculation of the mantissa is a bit difficult. At least up to Java 7, I understand that floating points cannot be used in bitwise operations.

Floating Point Casting in Java

瘦欲@ 提交于 2020-01-06 21:28:28
问题 Casting for integers is very straightforward, the extra bits simply disappear. But, is it important to understand what is happening under the hood for casting floating point? I've tried to read information on how floating point is calculated, but I have yet to find one that explains it well. At least that's my excuse. I get the basic idea although the calculation of the mantissa is a bit difficult. At least up to Java 7, I understand that floating points cannot be used in bitwise operations.

java insert Blob as ByteArrayOutputStream get ClassCastException

淺唱寂寞╮ 提交于 2020-01-06 12:36:14
问题 I've to save a pdf file represented as a ByteArrayOutputStream into a Blob SQL field of a table, here's my code: public boolean savePDF(int version, ByteArrayOutputStream baos) throws Exception{ boolean completed = false; ConnectionManager conn = new ConnectionManager(); try { PreparedStatement statement = conn.getConnection().prepareStatement(INSERT_PDF); statement.setLong(1, version); statement.setBlob(2, (Blob)baos); statement.execute(); conn.commit(); completed = true; } catch

how to convert a CNPhoneNumber to string in swift4?

痴心易碎 提交于 2020-01-06 11:22:58
问题 I am using this code for getting contact number from contacts app but when I want to show the number in label I get this warning and doesn't work: Cast from 'CNPhoneNumber' to unrelated type 'String' always fails func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) { contacts.forEach {(contact) in for number in contact.phoneNumbers{ let phone = number.value print(phone) numberLabel.text = phone as! String } } } 回答1: TRY : if let phone = number.value as?

how to convert a CNPhoneNumber to string in swift4?

六月ゝ 毕业季﹏ 提交于 2020-01-06 11:22:29
问题 I am using this code for getting contact number from contacts app but when I want to show the number in label I get this warning and doesn't work: Cast from 'CNPhoneNumber' to unrelated type 'String' always fails func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) { contacts.forEach {(contact) in for number in contact.phoneNumbers{ let phone = number.value print(phone) numberLabel.text = phone as! String } } } 回答1: TRY : if let phone = number.value as?