Questions tagged [int]

A data type that represents an integer. An integer is a whole number that can be negative, positive, or zero. (i.e. ...-2, -1, 0, 1, 2...) Use this tag for questions about using, storing, or manipulating integers.

int is a data type that represents an integer (a whole number - not a fraction).

int data types may vary based on:

  • the number of bits used to represent them - 16, 32, etc.
  • whether they allow both negative and positive values (signed integers) or only positive (unsigned integers).

In many (but not all) languages, integers are limited to a specific range due to the way they are stored - for instance, an ISO C 32-bit signed integer can store values from −2,147,483,648 to +2,147,483,647 (-2³¹ to 2³¹-1).

This keyword is often used in C-like languages (including C, C++, Golang, etc.)

9665 questions
346
votes
9 answers

Determine if a String is an Integer in Java

I'm trying to determine if a particular item in an Array of strings is an integer or not. I am .split(" ")'ing an infix expression in String form, and then trying to split the resultant array into two arrays; one for integers, one for operators,…
Nick Coelius
  • 4,668
  • 3
  • 24
  • 30
300
votes
11 answers

What is the difference between Integer and int in Java?

For example why can you do: int n = 9; But not: Integer n = 9; And you can do: Integer.parseInt("1"); But not: int.parseInt("1");
mino
  • 6,978
  • 21
  • 62
  • 75
296
votes
14 answers

How to convert an int to a hex string?

I want to take an integer (that will be <= 255), to a hex string representation e.g.: I want to pass in 65 and get out '\x41', or 255 and get '\xff'. I've tried doing this with the struct.pack('c',65), but that chokes on anything above 9 since it…
pynoob
  • 2,963
  • 2
  • 16
  • 4
269
votes
26 answers

What is the difference between an int and an Integer in Java and C#?

I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int and an Integer in Java/C# (Object-Oriented Programming Languages). So, what is the…
CodingWithoutComments
  • 35,598
  • 21
  • 73
  • 86
263
votes
13 answers

How do I convert a decimal to an int in C#?

How do I convert a decimal to an int?
Neil P
259
votes
10 answers

How can I read inputs as numbers?

Why are x and y strings instead of ints in the below code? (Note: in Python 2.x use raw_input(). In Python 3.x use input(). raw_input() was renamed to input() in Python 3.x) play = True while play: x = input("Enter a number: ") y =…
user2509848
256
votes
14 answers

How do I convert from int to Long in Java?

I keep finding both on here and Google people having troubles going from long to int and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int to Long. The only other answers I've found…
Ghosty
  • 3,203
  • 2
  • 18
  • 13
251
votes
10 answers

Java: parse int value from a char

I just want to know if there's a better solution to parse a number from a character in a string (assuming that we know that the character at index n is a number). String element = "el5"; String s; s = ""+element.charAt(2); int x =…
Noya
  • 3,879
  • 3
  • 26
  • 32
251
votes
40 answers

What's the best way to check if a String represents an integer in Java?

I normally use the following idiom to check if a String can be converted to an integer. public boolean isInteger( String input ) { try { Integer.parseInt( input ); return true; } catch( Exception e ) { return…
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
242
votes
4 answers

Haskell: Converting Int to String

I know you can convert a String to an number with read: Prelude> read "3" :: Int 3 Prelude> read "3" :: Double 3.0 But how do you grab the String representation of an Int value?
Squirrelsama
  • 5,480
  • 4
  • 28
  • 38
227
votes
20 answers

Convert char to int in C#

I have a char in c#: char foo = '2'; Now I want to get the 2 into an int. I find that Convert.ToInt32 returns the actual decimal value of the char and not the number 2. The following will work: int bar = Convert.ToInt32(new string(foo,…
KeithA
  • 2,525
  • 3
  • 18
  • 15
224
votes
10 answers

NumPy or Pandas: Keeping array type as integer while having a NaN value

Is there a preferred way to keep the data type of a numpy array fixed as int (or int64 or whatever), while still having an element inside listed as numpy.NaN? In particular, I am converting an in-house data structure to a Pandas DataFrame. In our…
ely
  • 74,674
  • 34
  • 147
  • 228
221
votes
12 answers

Converting an int to std::string

What is the shortest way, preferably inline-able, to convert an int to a string? Answers using stl and boost will be welcomed.
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
220
votes
5 answers

What's the difference between size_t and int in C++?

In several C++ examples I see a use of the type size_t where I would have used a simple int. What's the difference, and why size_t should be better?
tunnuz
  • 23,338
  • 31
  • 90
  • 128
218
votes
13 answers

Is the size of C "int" 2 bytes or 4 bytes?

Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on? Most of the textbooks say integer variables occupy 2 bytes. But when I run a program printing the successive addresses of an array of integers it shows…
Rajiv Prathap
  • 2,219
  • 2
  • 14
  • 8