Questions tagged [primitive]

A primitive type is a data type provided by a programming language as a basic building block.

The actual range of primitive data types that is available is dependent upon the specific programming language that is being used. For example, in C, strings are a composite but built-in data type, whereas in modern dialects of BASIC and in JavaScript, they are assimilated to a primitive data type that is both basic and built-in.

Classic basic primitive types may include:


  • (character, char)
  • (integer, int, short, long, byte) with a variety of precisions
  • number (float, double, real, double precision)
  • number (fixed) with a variety of precisions and a programmer-selected scale
  • , logical values (true, false)

Reference

977 questions
27
votes
4 answers

Are there primitive types in Ruby?

I'm a Java developer who is just starting to learn Ruby. Does Ruby have any primitive types? I can't seem to find a list of them. If not, why?
emily
  • 467
  • 2
  • 6
  • 12
26
votes
8 answers

What is the difference between String.Format and string.Format (and other static members of primitive data types)?

As far as I can tell, any static member of a class like String or Int32 can also be accessed from the related primitive data type. So, String.Format is the same as string.Format, and Int32.MaxValue is the same as int.MaxValue. Is there a difference…
Matthew
  • 28,056
  • 26
  • 104
  • 170
26
votes
7 answers

Initialize array of primitives

I am wondering, What's exactly the difference between these two ways of initializing an array of primitives: int[] arr1 = new int[]{3,2,5,4,1}; int[] arr2 = {3,2,5,4,1}; and which one is preferred ?
Muz
  • 267
  • 1
  • 3
  • 6
25
votes
2 answers

Why does `"foo".bar = 42;` throw `TypeError` in strict mode in ES6?

According to the ES5.1 spec, the program "use strict;" "foo".bar = 42; causes a String object to be created, assigns to a property on it, and then throws the object away, resulting in no observable effects - including any exceptions. (The absence…
cpcallen
  • 1,834
  • 1
  • 16
  • 27
25
votes
2 answers

Is it possible to use a primitive type (int) in as a generic type in Java?

Specifically, with a SortedMap, int> I get "dimensions expected after this (int) token." Help!
Alexander
  • 956
  • 2
  • 8
  • 15
24
votes
5 answers

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 integers and it seems wasteful to have to wrap them in…
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
23
votes
6 answers

Behind the scenes, what's happening with decimal value type in C#/.NET?

How is the decimal type implemented? Update It's a 128-bit value type (16 bytes) 1 sign bit 96 bits (12 bytes) for the mantissa 8 bits for the exponent remaining bits (23 of them!) set to 0 Thanks! I'm gonna stick with using a 64-bit long with my…
23
votes
1 answer

Why is this method overloading ambiguous?

public class Primitive { void m(Number b, Number ... a) {} // widening, autoboxing->widening->varargs void m(byte b, Number ... a) {} // unboxing, autoboxing->widening->varargs public static void main(String[] args) { Byte b =…
Aman
  • 979
  • 3
  • 10
  • 23
22
votes
2 answers

Java Arrays.asList on primitive array type produces unexpected List type

Possible Duplicate: Arrays.asList() not working as it should? Apparently the return type of Arrays.asList(new int[] { 1, 2, 3 }); is List. This seems totally broken to me. Does this have something to do with Java not autoboxing arrays of…
Finbarr
  • 31,350
  • 13
  • 63
  • 94
22
votes
1 answer

How to collect the results of a Stream in a primitive array?

I'm trying to convert 2D list to a 2D int array. However, it seems I can only collect objects, not primitives. When I do: data.stream().map(l -> l.stream().toArray(int[]::new)).toArray(int[][]::new); I get the compile-time error Cannot infer type…
ack
  • 1,181
  • 1
  • 17
  • 36
22
votes
6 answers

Is String a primitive or an Object in Android or Java?

In the Android API http://developer.android.com/guide/topics/data/data-storage.html#pref It says: Shared Preference allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any…
Krishna Prasad
  • 693
  • 1
  • 6
  • 19
21
votes
2 answers

Does == actually work the same or different when comparing two primitives vs two Objects in Java?

When searching for explanations of how logical equals == works in Java the answers are always something along the lines of: For primitives it returns whether the primitives have the same value (this includes comparing a primitive to its…
user7382368
  • 293
  • 1
  • 9
20
votes
2 answers

Custom primitives in C#?

Apart from the questionable usefulness of this, I'd like to ask if it is possible to do something along these lines. class MyPrimitive { String value; public String Value { get { return value; } set {…
Joshua_R
20
votes
3 answers

How are the "primitive" types defined non-recursively?

Since a struct in C# consists of the bits of its members, you cannot have a value type T which includes any T fields: // Struct member 'T.m_field' of type 'T' causes a cycle in the struct layout struct T { T m_field; } My understanding is that an…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
19
votes
3 answers

Why do primitive data types work without including the System namespace?

I read that all primitives fall under the System namespace. If I comment out using System I would expect there to be a build error in my program, however it is running successfully. Why is this?
Rajesh Pawde
  • 399
  • 2
  • 11