C datatypes in Java - java

I have an application in C and need to re-write it to Java. I have a problem with data types. I am not sure which data type is equal to the C ones in Java.
There are those I need to find out equal datatypes in java programming language for.
unsigned char
unsigned short
short int
unsigned long

I'd pick something based on the ranges you need
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31 - 1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32 - 1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2^63 and a maximum value of 2^63 - 1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2^64 - 1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
ref: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Java doesn't have unsigned types (apart from char, which pretends not to be a number)
byte is one byte, in range -128 to 127
char is two bytes (unsigned), but only generally used for characters, not numbers
short is also two bytes, in range -32768 to 32767
int is four bytes
long is eight bytes
Edit -- char is unsigned (thanks to Hot Licks in comments)

Related

Why not use long for all integer values

In my Java class we have just learned about of each of the following primitive data types:
byte
short
int
long
Since the long data type contains the most bits, wouldn't it make sense to exclusively use the long data type to avoid restrictions?
Questions
Is there a particular downside to only using the long data type?
Does it make sense to use for example, an int data type, instead of a long data type?
Does it make sense to use for example, an int data type, instead of a long data type?
ABSOLUTELY YES.
MEMORY / DISK USAGE
Using only one variable or two you won't see difference of performance, but when apps grow it will increase your app speed.
Check this question for further info.
Also looking to Oracle primitive type documentation you can see some advices and the memory usage:
type memory usage recommended for
------- --------------- ---------------------------------------------------
byte 8-bit signed The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
short 16-bit signed same as byte
int 32-bit signed
long 64-bit Use this data type when you need a range of values wider than those provided by int
float Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
byte:
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
short:
The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
int:
By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2³¹ and a maximum value of 2³¹-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2³²-1.
long:
The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2⁶³ and a maximum value of 2⁶³-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2⁶⁴-1. Use this data type when you need a range of values wider than those provided by int.
float:
The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
CODE READABILITY
Also, it will clarify your mind and your code, lets say, you have a variable that represents the ID of an object, this object ID will never use decimals, so, if you see in your code:
int id;
you will now for sure how this ID will look, otherwise
double id;
wont.
Also, if you see:
int quantity;
double price;
you will know quantity won't allow decimals (only full objects) but price will do... That makes your job (and others programmers will read your code) easier.
Apart from the Range (the min and max value that can be stored in any particular data type) there is another aspect and that is size of the variable.
You must be aware of the following too:
byte = 1 byte
short = 2 bytes
int = 4 bytes
long = 8 bytes
So using long variable means you are allocating 8 bytes of memory to it.
Something like,
long var = 1000L
does not show efficient use of memory. What if you got GBs of RAM these days does not mean we should waste it.
Simple point I want to make is, more efficient use of memory, faster will be the app.
Performance in memory need and speed would make long expensive.
However there is one other advantage of int: one too easily mixes with int subexpressions, and may loose long info when combining operations wrong. A bit shift left of 50 actually doing 18 and so on. Using only numbers with an L postfix would be one measure. Also long may serve as an overflow capture for int multiplication and other such operations, where for long detecting an overflow is all you can do.
Note that for int "all" operations of byte and short are propagating to int.

Store signed value in Byte

I was trying to store byte value in a variable and trying to perform some logic based upon this calculation.
byte mByteValue = -129; // Holding byte value
Problem is I am always getting value 127, due to which my logic fails everytime.
Any specific reason behind this, why its behaving strange in my case?
A byte in java is a signed 8-bit value. 8 bits gives you 256 possible values, but since a byte is signed and can represent positive and negative values, those 256 values must be roughly split in half across the possible positive and negative values. So you can't store negative values past -128; in particular don't expect to be able to store -256.
What you're actually observing when your byte has the value 127 is known as overflow (see this wiki article)
If you need to manipulate values outside this range, as in your example code, or e.g. an unsigned byte, at some point you'll need to make use of a wider integer type, like short.
The standard libraries provide these limits as Byte.MIN_VALUE and Byte.MAX_VALUE (docs here and here).
The range of byte is from -128 to 127. You can not store any value beyond these range.
This is because byte is 8 bits. So the maximum positive number stored at byte is -
2^7 -1 = 127. // since the first bit is sing bit; 0 for positive
And minimum negative number stored at byte is-
2^7 = -128 //since the first bit is sign bit; 1 for negative.
And if you use unsigned byte the it would be 255.
To correctly convert a byte to an int use mByteValue & 0xFF. You can read more about the Two's complement here: https://en.wikipedia.org/wiki/Two%27s_complement.

Non-negative integer data type in java

Modulo-2 have a CARDINAL type which can hold any whole number from 0 to 65535 in a 16-bit microcomputer. Is there any data type in java that closely resembles CARDINAL? I know custom data type can be created in java for this purpose.
I have heard that there is going to be unsigned integer arithmetic API in java 8. But I want to know if there exists one before java 8.
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Read about primitive data types here.

Java - explicit conversion to char/short

can anyone tell me why this explicit conversion gives different results, even if size of short/char is both 16bits?
package jh;
public class Main {
public static void main(String[] args) {
byte b = (byte)255;
System.out.println("Size of short: " + Short.SIZE);
System.out.println("Size of char: " + Character.SIZE);
System.out.println((int)((short)b));
System.out.println((int)((char)b));
}
}
Output:
Size of short: 16
Size of char: 16
-1
65535
From Java datatypes doc
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
So in short (pardon the pun), bitwise they are the same. But char represents a different numeric value for the same bit pattern as short.
This is also accompanied by the sign extension feature: (byte) 255 is going to mean a byte value, with all bits set (0b11111111), which is -1, in twos complement. When converting upwards, Java does a sign extension operation, so if the sign bit is 0, all the higher bits will be 0 too, but when the sign bit is 1, all higher bits will be 1 too. This now means that -1 will mean -1 in all signed, integer datatypes (0b1111111111111111 for short in this example). But not char - which when has all bits set, equals the positive maximum value - 65535.
byte is signed and only holds values -128..127. When you assign 255 it wraps around and becomes -1.
(short)b is -1.
char however is the only unsigned type in Java, it has values 0..65535, so (char)-1 wraps around again and becomes 65535.
byte b = (byte)255; this assigns the value -1 to b since byte is signed. When you cast -1 to short you get -1. When you cast -1 to char you get 65535 since the range of char lies between 0 and 65535

max value of integer

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767.
In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647.
I do not understand how the range is different in Java, even though the number of bits is the same. Can someone explain this?
In C, the language itself does not determine the representation of certain datatypes. It can vary from machine to machine, on embedded systems the int can be 16 bit wide, though usually it is 32 bit.
The only requirement is that short int <= int <= long int by size. Also, there is a recommendation that int should represent the native capacity of the processor.
All types are signed. The unsigned modifier allows you to use the highest bit as part of the value (otherwise it is reserved for the sign bit).
Here's a short table of the possible values for the possible data types:
width minimum maximum
signed 8 bit -128 +127
signed 16 bit -32 768 +32 767
signed 32 bit -2 147 483 648 +2 147 483 647
signed 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
unsigned 8 bit 0 +255
unsigned 16 bit 0 +65 535
unsigned 32 bit 0 +4 294 967 295
unsigned 64 bit 0 +18 446 744 073 709 551 615
In Java, the Java Language Specification determines the representation of the data types.
The order is: byte 8 bits, short 16 bits, int 32 bits, long 64 bits. All of these types are signed, there are no unsigned versions. However, bit manipulations treat the numbers as they were unsigned (that is, handling all bits correctly).
The character data type char is 16 bits wide, unsigned, and holds characters using UTF-16 encoding (however, it is possible to assign a char an arbitrary unsigned 16 bit integer that represents an invalid character codepoint)
width minimum maximum
SIGNED
byte: 8 bit -128 +127
short: 16 bit -32 768 +32 767
int: 32 bit -2 147 483 648 +2 147 483 647
long: 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
UNSIGNED
char 16 bit 0 +65 535
In C, the integer(for 32 bit machine) is 32 bit and it ranges from -32768 to +32767.
Wrong. 32-bit signed integer in 2's complement representation has the range -231 to 231-1 which is equal to -2,147,483,648 to 2,147,483,647.
A 32 bit integer ranges from -2,147,483,648 to 2,147,483,647. However the fact that you are on a 32-bit machine does not mean your C compiler uses 32-bit integers.
The C language definition specifies minimum ranges for various data types. For int, this minimum range is -32767 to 32767, meaning an int must be at least 16 bits wide. An implementation is free to provide a wider int type with a correspondingly wider range. For example, on the SLES 10 development server I work on, the range is -2147483647 to 2137483647.
There are still some systems out there that use 16-bit int types (All The World Is Not A VAX x86), but there are plenty that use 32-bit int types, and maybe a few that use 64-bit.
The C language was designed to run on different architectures. Java was designed to run in a virtual machine that hides those architectural differences.
The strict equivalent of the java int is long int in C.
Edit:
If int32_t is defined, then it is the equivalent in terms of precision. long int guarantee the precision of the java int, because it is guarantee to be at least 32 bits in size.
The poster has their java types mixed up.
in java, his C in is a short:
short (16 bit) = -32768 to 32767
int (32 bit) = -2,147,483,648 to 2,147,483,647
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
That's because in C - integer on 32 bit machine doesn't mean that 32 bits are used for storing it, it may be 16 bits as well. It depends on the machine (implementation-dependent).
Actually the size in bits of the int, short, long depends on the compiler implementation.
E.g. on my Ubuntu 64 bit I have short in 32 bits, when on another one 32bit Ubuntu version it is 16 bit.
It is actually really simple to understand, you can even compute it with the google calculator:
you have 32 bits for an int and computers are binary, therefore you can have 2 values per bit (spot).
if you compute 2^32 you will get the 4,294,967,296. so if you divide this number by 2, (because half of them are negative integers and the other half are positive), then you get 2,147,483,648. and this number is the biggest int that can be represented by 32 bits, although if you pay attention you will notice that 2,147,483,648 is greater than 2,147,483,647 by 1, this is because one of the numbers represents 0 which is right in the middle unfortunately 2^32 is not an odd number therefore you dont have only one number in the middle, so the possitive integers have one less cipher while the negatives get the complete half 2,147,483,648.
And thats it. It depends on the machine not on the language.
In C range for __int32 is –2147483648 to 2147483647. See here for full ranges.
unsigned short 0 to 65535
signed short –32768 to 32767
unsigned long 0 to 4294967295
signed long –2147483648 to 2147483647
There are no guarantees that an 'int' will be 32 bits, if you want to use variables of a specific size, particularly when writing code that involves bit manipulations, you should use the 'Standard Integer Types'.
In Java
The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
in standard C, you can use INT_MAX as the maximum 'int' value, this constant must be defined in "limits.h". Similar constants are defined for other types (http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.5.html), as stated, these constant are implementation-dependent but have a minimum value according to the minimum bits for each type, as specified in the standard.

Categories

Resources