Questions tagged [primitive-types]

Built-in types in any programming language that are defined in an axiomatic way and cannot be divided into other types in the language.

primitive-types
Filter by
Sorted by
Tagged with
444 votes
20 answers
592k views

How to convert an ArrayList containing Integers to primitive int array?

I'm trying to convert an ArrayList containing Integer objects to primitive int[] with the following piece of code, but it is throwing compile time error. Is it possible to convert in Java? List<...
Snehal's user avatar
  • 7,406
73 votes
9 answers
45k views

Why must local variables, including primitives, always be initialized in Java?

Why must local variables, including primitives, always be initialized in Java? Why is the same not applicable in the case of instance variables?
Gourav's user avatar
  • 1,081
186 votes
21 answers
88k views

Why do people still use primitive types in Java?

Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth. I see a lot of new Java projects lately (that definitely require a ...
Naftuli Kay's user avatar
  • 89.6k
582 votes
18 answers
435k views

How to convert an NSString into an NSNumber

How can I convert a NSString containing a number of any primitive data type (e.g. int, float, char, unsigned int, etc.)? The problem is, I don't know which number type the string will contain at ...
Enyra's user avatar
  • 17.8k
36 votes
12 answers
29k views

Arithmetic operator overloading for a generic class in C#

Given a generic class definition like public class ConstrainedNumber<T> : IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T&...
Triynko's user avatar
  • 19k
146 votes
7 answers
68k views

Why can Java Collections not directly store Primitives types?

Java collections only store Objects, not primitive types; however we can store the wrapper classes. Why this constraint?
JavaUser's user avatar
  • 26k
120 votes
11 answers
96k views

When to use wrapper class and primitive type

When I should go for wrapper class over primitive types? Or On what circumstance I should choose between wrapper / Primitive types?
Gopi's user avatar
  • 5,809
52 votes
8 answers
40k views

How do I pass a primitive data type by reference?

How can I pass a primitive type by reference in java? For instance, how do I make an int passed to a method modifiable?
Neuquino's user avatar
  • 11.9k
85 votes
10 answers
59k views

Converting an array of objects to an array of their primitive types

If you have an array of Java objects which have a primitive type (for example Byte, Integer, Char, etc). Is there a neat way I can convert it into an array of the primitive type? In particular can ...
Il-Bhima's user avatar
  • 10.8k
62 votes
8 answers
90k views

Why can't primitive data types be "null" in Java?

When declaring any primitive type data like int or double they get initialized to 0 or 0.0. Why can we not set them to null?
MBMJ's user avatar
  • 5,411
101 votes
7 answers
53k views

Is a Java array of primitives stored in stack or heap?

I have an array declaration like this: int a[]; Here a is an array of primitive int type. Where is this array stored? Is it stored on heap or stack? This is a primitve type int, all primitive types ...
user241924's user avatar
  • 4,388
112 votes
7 answers
57k views

In java, is it more efficient to use byte or short instead of int and float instead of double?

I've noticed I've always used int and doubles no matter how small or big the number needs to be. So in java, is it more efficient to use byte or short instead of int and float instead of double? So ...
DisibioAaron's user avatar
  • 1,362
200 votes
13 answers
117k views

How To Test if Type is Primitive

I have a block of code that serializes a type into a Html tag. Type t = typeof(T); // I pass <T> in as a paramter, where myObj is of type T tagBuilder.Attributes.Add("class", t.Name); foreach (...
DaveDev's user avatar
  • 41.7k
54 votes
13 answers
51k views

Is it possible to write swap method in Java? [duplicate]

Here is the question: write a method that swaps two variables. These two variables should be primitives. It doesn't need to be generic e.g. two int variables. Is there a way?!
Khaled Alshaya's user avatar
79 votes
10 answers
65k views

Is String a primitive type?

I am curious about the string and primitive types. Article like this says string is primitive type. However second article on MSDN does not list string as primitive type. However when I ran the code ...
Ram's user avatar
  • 11.5k
21 votes
1 answer
26k views

Casting rules for primitive types in java

In java, There are integral types(char/short/int/long/byte) There are floating types(float/double) There is boolean type(boolean), not integral type, unlike C language. Questions: Is there a generic ...
overexchange's user avatar
  • 16.4k
56 votes
4 answers
60k views

How to determine the primitive type of a primitive variable?

Is there a "typeof" like function in Java that returns the type of a primitive data type (PDT) variable or an expression of operands PDTs? instanceof seems to work for class types only.
ashley's user avatar
  • 1,177
17 votes
1 answer
3k views

Pseudo-destructor call does not destroy an object

Consider the following code: #include <iostream> typedef int t; t a=42; int main() { a.t::~t(); std::cout << a; //42 } I'm expected that a will be destroyed. But it is not true,...
user avatar
155 votes
4 answers
182k views

Setting Short Value Java

I am writing a little code in J2ME. I have a class with a method setTableId(Short tableId). Now when I try to write setTableId(100) it gives compile time error. How can I set the short value without ...
Mubashar's user avatar
  • 12.5k
38 votes
3 answers
21k views

Most efficient Java primitive collections library [closed]

What is the best Java primitive collections library? (most memory and time efficient) I've found Trove and FastUtil to be the most used ones, but haven't found much comparison between them (or ...
Sarmun's user avatar
  • 2,398
14 votes
3 answers
13k views

Java: Why can't I cast int to Long

All numbers in Java are supposed to be of int type. The following line is legal in Java>1.5 Short s = 1; // Will compile to Short s = Short.valueOf((short)1) - thus you can't exceed short max value i....
SirVaulterScoff's user avatar
39 votes
6 answers
87k views

Is it valid to compare a double with an int in java?

Utilities.getDistance(uni, enemyuni) <= uni.getAttackRange() Utilities.getDistance returns double and getAttackRange returns int. The above code is part of an if statement and it needs to be true. ...
Tasos's user avatar
  • 1,625
34 votes
4 answers
26k views

Does Swift support implicit conversion?

For example, I have the following code: let numberOfBlocks = 3 let blockWidth = SKSpriteNode(imageNamed: "image.png").size.width let padding = 20.0 let offsetX : Float = (self.frame....
Bagusflyer's user avatar
  • 12.8k
34 votes
11 answers
20k views

When to use primitive and when reference types in Java

In which case should you use primitive types(int) or reference types (Integer)? This question sparked my curiosity.
Gandalf StormCrow's user avatar
11 votes
5 answers
6k views

Is it possible to create a type in c++ that takes less than one byte of memory?

For my computation I only need to use 7-bit space so I am using a char type. However I wonder if it is possible to declare my own type that uses less than one byte of memory?
ElConrado's user avatar
  • 1,556
3 votes
4 answers
5k views

Overflowing Short in java

I have one question about the short data type in Java. I know that the range for short is between -32768 to 32767. So, if I tried to add two short values that exceed the range, the result ends up ...
ScoutBlade's user avatar
79 votes
8 answers
49k views

PHP type-hinting to primitive values?

I'd like to know whether one can type-hint a method to expect primitive types? Something like this: public function someMethod(string $str) //^^^^^^ Or: private function ...
Felipe's user avatar
  • 11.8k
20 votes
3 answers
21k views

Compare two arrays of primitives in Java?

I know about Arrays.deepEquals(Object[], Object[]) but this doesn't work for primitive types (due limitations of arrays and autoboxing, see this related post). With that in mind, is this the most ...
Mark Renouf's user avatar
  • 30.9k
13 votes
2 answers
5k views

JavaScript primitive types and corresponding objects

In JavaScript not every data is an object. There exist a few primitive types, like strings, numbers and Boolean which are not objects. For each of these types there exists a constructor which outputs ...
Andrea's user avatar
  • 20.4k
12 votes
3 answers
6k views

Reasoning behind having to specify L for long, F,D for float, double

A few related questions here. As per the title, why is it a requirement if we are specifying the variable type as long or float, double? Doesn't the compiler evaluate the variable's type at compile ...
wulfgarpro's user avatar
  • 6,846
8 votes
3 answers
9k views

Why is string a reference type?

Why is string a reference type, even though it's normally primitive data type such as int, float, or double.
selvaraj's user avatar
  • 889
191 votes
8 answers
82k views

Scalar vs. primitive data type - are they the same thing?

In various articles I have read, there are sometimes references to primitive data types and sometimes there are references to scalars. My understanding of each is that they are data types of ...
Ben Pearson's user avatar
  • 7,642
62 votes
5 answers
61k views

What's the difference between NSNumber and NSInteger?

What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats?
mk12's user avatar
  • 26.1k
45 votes
4 answers
11k views

Do STL maps initialize primitive types on insert?

I have a std::map like this: map<wstring,int> Scores; It stores names of players and scores. When someone gets a score I would simply do: Scores[wstrPlayerName]++; When there is no element ...
Calmarius's user avatar
  • 19k
27 votes
9 answers
30k views

Why are Java wrapper classes immutable?

I know the usual reasons that apply to general immutable classes, viz can not change as a side effect easy to reason about their state inherently thread safe no need to provide clone/copy ...
shrini1000's user avatar
  • 7,126
24 votes
2 answers
32k views

Does int.class equal Integer.class or Integer.TYPE in Java?

Let's imagine one retrieves the declaring type of a Field using reflection. Which of the following tests will correctly indicate whether one is dealing with an int or an Integer? Field f = ... Class&...
Jérôme Verstrynge's user avatar
16 votes
9 answers
39k views

Converting String to Number in Java

How can i convert a string to a abstract number, provided string is any valid number in java (say int, long, double etc). I will not know the type of number in the string, so i can't use specific ...
Lakshan Prabhu's user avatar
11 votes
3 answers
10k views

Java - short and casting

I have the following code snippet. public static void main(String[] args) { short a = 4; short b = 5; short c = 5 + 4; short d = a; short e = a + b; // does not compile (expression treated as ...
chr1s's user avatar
  • 135
9 votes
5 answers
3k views

Widening and Boxing Java primitives

Widening and Boxing Java primitives. I know it is not possible to widen a wrapper class from one to another as they are not from the same inheritence tree. Why though is it not possible to widen a ...
ziggy's user avatar
  • 15.8k
37 votes
3 answers
36k views

Why is there no primitive type for String? [duplicate]

Why doesn't Java have a primitive type for String when most of the other data types do?
user241924's user avatar
  • 4,388
29 votes
4 answers
101k views

Char into byte? (Java)

How come this happens: char a = '\uffff'; //Highest value that char can take - 65535 byte b = (byte)a; //Casting a 16-bit value into 8-bit data type...! Isn't data lost here? char c = (char)b; //Let'...
wakachamo's user avatar
  • 1,763
24 votes
5 answers
20k views

Java Vector or ArrayList for Primitives

Is there an expandable array class in the Java API equivalent to the Vector or ArrayList class that can be used with primitives (int, char, double, etc)? I need a quick, expandable array for ...
Daniel Bingham's user avatar
22 votes
5 answers
19k views

How to instantiate Class class for a primitive type?

I'm trying to do this, but doesn't work: public static Class loadIt(String name) throws Throwable { return Class.forName(name); } assert foo.loadIt("int") == int.class; // exception here How ...
yegor256's user avatar
  • 103k
12 votes
5 answers
21k views

Change boolean Values?

I have a question about boolean values in Java. Let's say I have a program like this: boolean test = false; ... foo(test) foo2(test) foo(Boolean test){ test = true; } foo2(Boolean test){ if(test)...
user1871869's user avatar
  • 3,337
12 votes
1 answer
12k views

What is the "symbol" primitive data type in JavaScript [duplicate]

The new primitive type comes with ES6 which is Symbol type.The short definition says : A symbol is a unique and immutable data type and may be used as an identifier for object properties. The symbol ...
BCRK's user avatar
  • 161
6 votes
2 answers
2k views

ActionScript - Difference Between Primitive / Non-Primitive Objects for Memory Management?

my understanding is that primitive types (uint, string, Number, etc.) of a class do not need to be set to null for garbage collection. for example, i am not required to write this dispose() method in ...
Chunky Chunk's user avatar
6 votes
4 answers
4k views

In Java, do primitive types and arrays have a containing package?

In Java, do primitive types and arrays have a containing package? Probably not, but just want to be certain.
John Assymptoth's user avatar
4 votes
1 answer
1k views

Why isn't "is" comparison used in place of "==" for primitive types?

When I'm using Pytest for Python formatting, it complains about doing something like: >>> assert some_function_ret_val() == True E712 comparison to True should be 'if cond is True:' or 'if ...
Dave Liu's user avatar
  • 1,055
2 votes
4 answers
4k views

Parameter For All Primitive Types of Java?

I have this function; static public void Print(Object[] arr) { for(int i = 0; i < arr.length; i++) System.out.println(i + " => " + arr[i]); } I want to use that for every primitive ...
previous_developer's user avatar
55 votes
5 answers
8k views

Is it better to remove "const" in front of "primitive" types used as function parameters in the header?

In the code review process, one of my coworkers mentioned to me that "const"s in front of "primitive types" used as a function parameter in a header is meaningless, and he recommended to remove these "...
chanwcom's user avatar
  • 4,550