primitive

Simple Variables in Java & C++

爷,独闯天下 提交于 2019-12-13 13:06:45
问题 I saw this sentence in some matrials: "In Java, simple data types such as int and char operate just as in C." I am wondering that actually they are different in Java & C++? In C++, simple variables like the primitives in Java are assigned a memory address as well, so these primitive types in C++ can have a pointer as well. However primitives in Java are not assigned a memory address like Objects are. Am I correct? Thanks! 回答1: Almost. In java primitives are assigned memory as well, but this

Java — Primitive Counterpart of Byte, Integer, Long, etc. in template

最后都变了- 提交于 2019-12-13 12:24:27
问题 BACKGROUND : I am trying to implement a tiny template, i.e. generic class, which would allow me to achieve a pass-by-reference functionality as follows. public static class Ref<T> { T value; public Ref(T InitValue) { this.set(InitValue); } public void set(T Value) { this.value = Value; } public T get() { return this.value; } } So, I could define a function that takes a 'Ref' where the value can actually be changed, e.g. public static void function(Ref<Byte> x) { x.set((byte)0x7E); } The

Drawing a circle segment [closed]

旧街凉风 提交于 2019-12-13 09:07:17
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . I am wondering if anyone here can help me with some pseudocode or at least point me in the right direction on how to draw a circle segment without anti-aliasing. 回答1: The formula for points on a circle are: x = xcenter + radius * sin(theta) y = ycenter + radius * cos(theta) where

How can we work with generic types and primitives in Java

余生长醉 提交于 2019-12-13 03:55:00
问题 I have an object that contains an array, but the type of the array will be diffent every time. I could do something like class MyObject<T> { public T [] data; } The thing is that this does not work with primitive types (int, double, ...) and it makes me work with Objects (Integer, Double...). Is there any way of avoiding this? Thanks in adavnce 回答1: You should be aware that autoboxing in Java may do exactly what you're looking for. See this code example from the link: // List adapter for

Objective-C classes, pointers to primitive types, etc

十年热恋 提交于 2019-12-13 03:36:58
问题 I'll cut a really long story short and give an example of my problem. Given a class that has a pointer to a primitive type as a property: @interface ClassOne : NSObject { int* aNumber } @property int* aNumber; The class is instantiated, and aNumber is allocated and assigned a value, accordingly: ClassOne* bob = [[ClassOne alloc] init]; bob.aNumber = malloc(sizeof(int)); *bob.aNumber = 5; It is then passed, by reference, to assign the aNumber value of a seperate instance of this type of class,

Confusion about prototype chain , primitives and objects

被刻印的时光 ゝ 提交于 2019-12-13 02:37:45
问题 in firebug console : >>> a=12 12 >>> a.__proto__ Number {} >>> (12).__proto__ Number {} >>> a.constructor.prototype === (12).__proto__ true >>> a.constructor.prototype.isPrototypeOf(a) false the final line causes me a great confusion as compared to the other lines. also see Constructor.prototype not in the prototype chain? 回答1: When you use the . operator with a primitive, the language auto-boxes it with the appropriate Object type (in this case, Number). That's because simple primitive types

Passing dynamic list of primitives to a Java method

只谈情不闲聊 提交于 2019-12-12 17:24:18
问题 I need to pass a dynamic list of primitives to a Java method. That could be (int, int, float) or (double, char) or whatever. I know that's not possible, so I was thinking of valid solutions to this problem. Since I am developing a game on Android, where I want to avoid garbage collection as much as possible, I do not want to use any objects (e.g. because of auto boxing), but solely primitive data types. Thus a collection or array of primitive class objects (e.g. Integer) is not an option in

XMLSerialization: The type of the argument object 'Sw' is not primitive

流过昼夜 提交于 2019-12-12 13:28:44
问题 I'm trying to serialize an object to an XML file, but am getting the above error. The problem seems to be with objects that contain a list of a base class but is populated by objects derived from the base class. Example code is as follows: public class myObject { public myObject() { this.list.Add(new Sw()); } public List<Units> list = new List<Units>(); } public class Units { public Units() { } } public class Sw : Units { public Sw(); { } public void main() { myObject myObject = new myObject(

What is the difference in usage of primitive and wrapper data type and what is the need of wrapper data type?

瘦欲@ 提交于 2019-12-12 10:53:22
问题 I searched it all over the web, but all the answers just consisted of the difference. I know the difference, but I don't understand the difference in their applications. For example, suppose we have to take two floating values, if we use double, we can easily compare using a==b , whereas if we use Double, we will have to use a.equals(b) . 回答1: There is more than that behind the scenes. One of the reasons is how the Collections API is developed in Java... Consider that you just can not do

Creating (boxed) primitive instance when the class is known

走远了吗. 提交于 2019-12-12 07:26:39
问题 I need a method that returns an instance of the supplied class type. Let's assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying String.class would return an empty String, supplying an Integer.class would return an Integer whose initial value is zero, and so on. But how do I create (boxed) primitive types on the fly? Like this? public Object newInstance(Class<?> type) { if (!type.isPrimitive()) { return type.newInstance();