why float and double showing different behaviour in java? [duplicate] - java

This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
case 1
float a=033.0 //shows compilation problem
case 2
double a=033.0 //works fine
Why case 1 is showing error but not case 2 or vice-versa?

By default java uses double type to store floating values. since you are storing double in float (down casting) java will throw an error. it can be resolved by two ways
float a=033.0f
float a= (float)033.0

case 1----float a=033.0 //shows compilation problem
case 2----double a=033.0 //works fine
In Java, decimal number is interpreted as a double ,so converting from double to float cannot be performed automatically ,so you need to give like this :
float a= 033.0f;

Its simply the understanding of Java Syntax.
You can read the Primitive data types of java.
You'll get it anywhere...
Link : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Direct to this link, do ctrl+F, & paste this "Floating-Point Literals".
You wont't waste time wandering...
For your own convenience now, you can prefer this.
float fractionNumber = 25.24F;
fractionNumber = 25.24f;
double biggerFractionNum = 56.65555D;
biggerFractionNum = 56.65555;
but generally its like this all over... later you'll get used to it.
float foo = 34.4F;
double doo = 34.4;
IMPLEMENT it right away ! ...experience it, understand it...& you'll never forget it. :)

Related

using ! on int in JAVA [duplicate]

This question already has answers here:
how to use NOT operator for integers in JAVA
(2 answers)
Closed 5 years ago.
I was working on a program in java where I was creating a truth table of a full subtractor using a 2 dimensional array. While doing so, I wanted to compute the borrow out with the following formula:
B(out) = !(X).Y + (!(X ^ Y))B(in)
So, I wrote it like:
table[i][4] = ((!(table[i][2]))&table[i][1])+((!(table[i][2]^table[i][1]))*table[i][0]);
(here: table[i][4] = cell to store borrow out;
table[i][2] = cell storing X;
table[i][1] = cell storing Y;
table[i][0] = cell storing borrow in B(in) )
Whereas XOR(^) and AND(&) and or(|) worked fine, I got the following error for NOT(!):
operator ! cannot be applied to int
How can I correct this? Is there any other way to write this formula as a JAVA code? Please help.
The bitwise complement operator that can be applied to integer values is the ~, not !

Can I declare and define variables with generated names and values? [duplicate]

This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
An example:
int x1 = 1;
int x2 = 2;
int x3 = 3;
...
int xn = n;
As you can see the name and value of the variables are generated - in this case by a counter. Is this possible?How?
I do not know how, but this is probably possible with some crazy Reflection hacking - do not try to do it, as it will produce code that is hard to maintain, hard to understand and vulnerable to hard-to-trace bugs.
Instead use Collections (do not use arrays if not absolutely needed), for example ArrayList.
Yes, it is valid Java code, but in this particular case you would just use the numbers :D

Checking for NaN and using it in an If [duplicate]

This question already has answers here:
How do you test to see if a double is equal to NaN?
(7 answers)
Closed 8 years ago.
I am collecting some data from a database and adding them together to get some statistics, but since I backdate some of my data then the calculated sum will sometime come up as NaN (not a number) I want to create an if sentence that says if(not a number) then exclude this data from my table.
How do I test if the data (in this case double) is NaN?
There are static methods Float.isNaN(float) and Double.isNaN(double) that you can use.
double x = ... // whatever calculation you do
if (Double.isNaN(x)) {
...
}
You can test for NaN two ways. You can use the built in function
Double.isNaN(x)
or perform the check this does which is
if (x != x)
provided x is a double or a float
This would work for you.
if(number == Float.NaN)

variable/parameterized width for printf/format in Java (using * or something else?) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java printf using variable field size?
I haven't worked with Java in a while so I was looking for a way to
specify variable width in format/printf when formatting/printing output. My example shows the use with an integer, but of course I'd like this to work for other types too.
E.g., something along the lines of
int val = 8;
int wid = 5;
System.out.printf("%"*d\n", wid, val);
I could use this work-around, which is ugly:
System.out.printf("%"+wid+"d\n", val);
Was the * variable field width specifier removed from Java? This old'ish
page, section 1.3.1,
shows the use (like it would be used in C), but I can't get it to
work, resulting in:
java.util.UnknownFormatConversionException: Conversion = '*'
nor have I been able to find more recent references that this
does work.
Is there an easier way to do this other than my work-around above?
I did look around before posting and came across this about 2-year old SO question Java printf using variable field size? but is that the final word on this?
The general syntax of a format specifier is
%[parameter][flags][width][.precision][length]type
Instead of printf, you can also use
String.format("%"+wid+"d",val);
And yes, these are the only ways in case you are using a Java Formatter.

Conversion calculation from string do double [duplicate]

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
i'm working in android and I need your help. In string in values I have this
<item>(1 / 1024)</item>
I need to parse this string to double this way
outputDouble = Double.parseDouble(unitsValues[outputPosition]);
so it means this
outputDouble = Double.parseDouble((1 / 1024));
This second code mean, that I find the line by index I need and it tries to convert it from string to double but it is impossible because it can recognize this string (1 / 1024) to double. Do you have any ideas?
Thank you
There is no built in method to do that ... However you could do that with an external library like BeanShell :
Interpreter interpreter = new Interpreter();
interpreter.eval("(1 / 1024)");
To use Beanshell with Android, download the bsh-core.jar file, put it in a /lib folder in your project, and adjust your Eclipse settings or Ant script to reference that JAR during compilation and packaging.
Maybe this will illustrate you how to do the parsing using the String#split. There is no direct method to do the conversion.
String representation = getString(R.string.my_repr);
String [] splits = representation.split("/");
Double num = Double.valueOf(splits[0].trim()) / Double.valueOf(splits[1].trim());
EDIT I also added trimming to the strings, because of the danger of spurious interval symbols.
Using regex parse (1 / 1024) into two Double values 1f and 1024f and then divide them to get a Double value.

Categories