Why I cannot parse "+1" to int? [duplicate] - java

This question already has answers here:
Parse number with positive/negative prefix from string in java
(2 answers)
Closed 8 years ago.
I am trying to parse +1 value to an int value. In my code +1 is a String and i am trying to convert it to Integer using Integer.parseInt,
String num = "+1";
int convertedNum = Integer.parseInt(num);
but I am getting error, why Java is not treating +1 as an integer value ?

because you must be using older version of JDK (JDK < 7). Before Java 7,
+1 wasn't considered as a valid integer.
Prior to JDK 1.7, what was the result of the following code segment?
double x = Double.parseDouble("+1.0");
int n = Integer.parseInt("+1");
Pat yourself on the back if you knew the
answer: +1.0 has always been a valid floating-point number, but
until Java 7, +1 was not a valid integer. This has now been fixed
for all the various methods that construct int, long, short, byte,
and BigInteger values from strings. There are more of them than you
may think. In addition to parse (Int|Long|Short|Byte), there are
decode methods that work with hexadecimal and octal inputs, and
valueOf methods that yield wrapper objects. The BigInteger(String)
constructor is also updated.

Related

Is there a data type in Java for binary integer literals (that distinguishes decimal from binary)? [duplicate]

This question already has answers here:
Are there binary literals in Java?
(2 answers)
Closed 4 years ago.
I know int is a data type used to declare variables, but it is in the decimal system. What if I want to specify that my integer is in binary?
Edit: I am sorry if this question is being confused with the one that it was marked with as a duplicate. To clarify:
There is a data type int in java that specifies integer literals. Integer literals come in various number systems, including decimal, hexadecimal, octal, binary. When we declare a variable to be an integer, we write:
int var;
We can give var a binary integer value by doing an assignment like:
var = 0b1101
However, I was asking is it possible to specify the integer to be in binary system (i.e. a more specific data type of integer) without later having to put 0b in front of the binary number we assign later to var?
Yes. To write a binary literal you write a number zero and then a letter b like
int i = 0b10;
System.out.printf("There are %d kinds of people...%n", i);

Double value is getting floor-ed when parameter is an Integer [duplicate]

This question already has answers here:
Rounding a double to turn it into an int (java)
(8 answers)
Closed 4 years ago.
If i have {"myparam": 12.8} and my request class has
Integer myparam;
myparam gets set to 12 instead of 13.
Why is it a floor instead of a rounding up?
This is the expected behaviour of requesting an int value from a Double (as seen in the Double API):
public abstract int intValue()
Returns the value of the specified number as an int. This may involve
rounding or truncation.
Returns:
the numeric value represented by this object after conversion to type int.
The simplest way to explain it - when getting an int from a double, everything right of and including the '.' is chopped of.

Can I store a big hexadecimal value in Long datatype in Java? [duplicate]

This question already has answers here:
Convert from one base to another in Java
(10 answers)
Closed 5 years ago.
Mac address is 6 bytes long(12 digit or 48bits in length).
Long can hold 8 bytes of data but still I am not able to store following mac addresss in a long data variable?
Long mac=(long) 0xffffffffffff;
Why is it happening?
What is the best datatype to store mac address data type(I want to store in base10 format)?
You have to define it as Long value by adding a L at the end:
Long mac = 0xffffffffffffL;
You can find more information of the L in the Java SE Spec (3.10.1. Integer Literals).
You are not using a long value but an int. Just add L to your number
Long mac= 0xffffffffffffL;
To quote Oracle
An integer literal is of type long if it ends with the letter L or l;
otherwise it is of type int. It is recommended that you use the upper
case letter L because the lower case letter l is hard to distinguish
from the digit 1.
As for your question about which datatype to chose, any can be good as long as it holds the necessary 48bits.
Noteworthy, java returns a byte array in order to get the harware address in NetworkInterface#getHardwareAddress, but states that it is 'usually' a mac address.

Given a int suppose 5, whose binary representation is 101, we need to flip the bit which will be 010, which will be 2 [duplicate]

This question already has answers here:
Bitwise operator for simply flipping all bits in an integer?
(16 answers)
Closed 5 years ago.
I am converting the given int to binary represention which is string, and then traversing the string and changing each char/bit in string and then converting back to required int.
Myquestion is, is there a mathematical or better way to do this?
Using the bitwise operator ~ (not) you can do it without iteration. Example method:
public static int flipBits(int n) {
return ~n;
}
You can also do it without a method, but for a general use using a method may be easier.
Okay I got one more solution in which we find the maximum number that can be formed by digit's length bit, i.e for 5 `101' we can get 111 which is 7, so we subtract 7-5(given number) = 2, required solution

Why can't I assign a 'long' a value of 4 billion? [duplicate]

This question already has answers here:
The literal xyz of type int is out of range
(5 answers)
Closed 1 year ago.
I'm trying to declare a long value in Java, which unfortunately does not work.
This is my code. It results in the following error message: "The literal 4294967296 of type int is out of range".
long bytes = 4294967296;
I need this value to make a file filter that filters out files that are bigger than 4294967296 bytes (4GB). The other way round works without any issues (long size = file.length()) with every file size, which is why I can't figure out why my declaration is not working.
Add L to the end of the number:
long bytes = 4294967296L;
To answer your question title, the maximum value of a long can be obtained via the constant:
Long.MAX_VALUE
To solve your problem - add the l (L) literal after the number.
long literals are followed by the letter L or l (see: JLS 3.10.1). Uppercase is better because it's more legible, lowercase l looks too similar to 1.
For your particular number, it's probably easier to write:
long bytes = (1L << 32);
This way, someone who reads the code can quickly tell that bytes is exactly 2 to the power of 32.
try long bytes = 4294967296L; to indicate to the compiler that you are using a long.
The answer to your question "why" is because of 4294967296 is not a long. By default java look on any number as on int or double type (depending on if it has or hasn't dot). And only then convert this number to specified type (long in your case). So the error that you see means that your number is bigger then max value fot int. Adding literal attribute at the end let compiller know which type to use (b - bytes, s - short, l - long, f - float)
Soufiane is correct. Here is a doc that shows how to declare literals of the various types of numbers in Java:
http://www.janeg.ca/scjp/lang/literals.html

Categories