primitive

Using int as a type parameter for java.util.Dictionary

百般思念 提交于 2019-12-03 05:19:19
When I try to declare a Dictionary as such: private Dictionary<String, int> map; The compiler gives me the following error: Syntax error on token "int", Dimensions expected after this token But it works fine with Integer . I'm vaguely aware that Java treats int / Integer differently (I come from a .NET background), but I was hoping someone could give me a full explanation on why I can't use primitives in a Dictionary<> In Java primitives aren't objects, so you can't use them in place of objects. However Java will automatically box/unbox primitives (aka autoboxing ) into objects so you can do

Fastest way to check if a byte array is all zeros

假装没事ソ 提交于 2019-12-03 02:02:57
I have a byte[4096] and was wondering what the fastest way is to check if all values are zero? Is there any way faster than doing: byte[] b = new byte[4096]; b[4095] = 1; for(int i=0;i<b.length;i++) if(b[i] != 0) return false; // Not Empty I have rewritten this answer as I was first summing all bytes, this is however incorrect as Java has signed bytes, hence I need to or. Also I have changed the JVM warmup to be correct now. Your best bet really is to simply loop over all values. I suppose you have three major options available: Or all elements and check the sum. Do branchless comparisons. Do

What is the use of passing const references to primitive types?

霸气de小男生 提交于 2019-12-02 22:56:28
In a project I maintain, I see a lot of code like this for simple get / set methods const int & MyClass::getFoo() { return m_foo; } void MyClass::setFoo(const int & foo) { m_foo = foo; } What is the point in doing that instead of the following? int MyClass::getFoo() { return m_foo; } // Removed 'const' and '&' void MyClass::setFoo(const int foo) { m_foo = foo; } // Removed '&' Passing a reference to a primitive type should require the same (or more) effort as passing the type's value itself, right? It's just a number after all... Is this just some attempted micro-optimization or is there a

Why do object assignments refer to memory locations when primitive types don't? [duplicate]

痞子三分冷 提交于 2019-12-02 15:39:33
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 3 years ago . I have a bit of an odd question regarding how primitive types work in Java. When using Objects when you assign an ObjectA to be ObjectB be like such Rectangle ObjectB = new Rectangle(); ObjectA = ObjectB; Any calls to ObjectA refer now to ObjectB's memory location. However when using integers or other primitive types this is not the case. For example int x = 3; int y = x;

Why do object assignments refer to memory locations when primitive types don't? [duplicate]

蓝咒 提交于 2019-12-02 10:07:42
This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 84 answers I have a bit of an odd question regarding how primitive types work in Java. When using Objects when you assign an ObjectA to be ObjectB be like such Rectangle ObjectB = new Rectangle(); ObjectA = ObjectB; Any calls to ObjectA refer now to ObjectB's memory location. However when using integers or other primitive types this is not the case. For example int x = 3; int y = x; int x = 5; return y; y will return 3, the value of x when y was initialized. The question I have is why does assignment for

Question about Java Primitive Types methods

泪湿孤枕 提交于 2019-12-02 07:04:54
问题 I'm confused with primitive types in Java and the methods of converting one type to another. If, say, I have an integer and I want to convert it to a string, I need to use a static method of Integer or String, e.g. String.valueOf(some_integer); But if I want to convert a stirng to a char array I can use something like, some_string.toCharArray(); My question is why? Why do I need to use a static method for the first one? 回答1: Because the argument you pass - an int is a primitive, and

In Objective-C, are int variables that haven't been assigned a value nil?

血红的双手。 提交于 2019-12-01 23:46:41
If I have this in my .h file: int index; And then in the .m file I have: if (index == nil) and I haven't assigned a value to index , will that come up true? EDIT I guess nil is only used for objects. So what is the state of an int that hasn't been assigned a value? There is no such thing as "nil" for ints. That's an object value. As for what variables are initialized to by default: Non-static local variables are not initialized to any defined value (in practice, they will usually have whatever bit pattern was previously in the memory they're occupying) Static variables are initialized to 0

How do I make a class assignable to primitives? Or, how do I make a scalar class?

≡放荡痞女 提交于 2019-12-01 22:30:43
I was wondering if it's possible to make my class Time { public: Time(); explicit Time( const double& d); Time& operator=( const Time& time); Time& operator=( const double& d); }; assignable to the primitive double? I'm using Time as an IV a lot and need to do a lot of scalar operations on it, so it needs to "mingle" with DV's which are usually ordinary doubles. Adding a second assignment operator did the trick the other way around. A lot of operations still aren't possible with just this though. I've been writing operators outside of the Time class to allow for addition, substraction,

Why is assigning 'int constant -> byte variable' valid, but 'long constant -> int variable' is not?

孤者浪人 提交于 2019-12-01 19:29:56
I have this code snippet: int i = 5l; // not valid (compile error) byte b = 5; // valid What do you think about it? Why? This is defined in the JLS #5.2 (Assignment conversion) : If the expression is a constant expression (§15.28) of type byte, short, char, or int, a narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable. so: byte b = 5; //ok: b is a byte and 5 is an int between -128 and 127 byte b = 1000; //not ok: 1000 is an int but is not representable as a byte (>

selection sort with generics

元气小坏坏 提交于 2019-12-01 14:36:59
I did selection sort with integers and it was working, when I tried to modify the program to work with generics the compiler is complaining and I don't know how to fix it. If anyone can point some tips and constructive comments I would be grateful. Here is the code. public class SelelctionSort { public static void main(String[] args) { int[] list = {34, 17, 23, 35, 45, 9, 1}; System.out.println("Original Array: "); printArray(list); selectionSort(list); System.out.println("\nSelection sort:"); printArray(list); } //selection sort public static <E extends Comparable<E>> void selectionSort(E[]