Im not understanding casting in java, char casting in particural. I'm not able to predict the outcome of this code,since I don't understand what the casting of char will "reproduce".Some explanation would be great! Thanks
public class test {
public static void main(String[] args) {
int u = 10;
double v = 22.105;
byte w = 100;
char x = 'a';
float y = 20.5f;
short z = 50;
double d_Value = (float) ((char) (u/v) + y);
Out.print(d_Value);
}}
char is an integer-based data type, so you'd lose the precision from the double result on u/v, giving you a 0 number (or the \0 char). Then it adds 20.5F for the final result: 20.5.
Casting is a higher precedence than most operators in the language, so that cast is relevant before the + operation.
Related
The following code is designed to factorize a number typed into the variable x.
public class testMod{
public static void main(String[]args){
double x = 11868681080091051216000;
StringBuilder output = new StringBuilder("1 * ");
for(double y = 2; y <= x; y++){
while (x % y == 0) {
System.out.print("Calculating... \n");
String printNumber = y + " * ";
x = x / y;
output.append(printNumber);
System.out.print(output.substring(0, output.length() - 2) + "\n");
}
}
}
}
The problem is that the compiler treats 11868681080091051216000 as an int, regardless of the attempt to assign it to a double. As such, it's out of range.
To specify a double literal, you can simply append D to the end – but do note that you'll lose precision this way:
double x = 11868681080091051216000D;
System.out.println(x); // prints 186868108009105E22
If you need the full precision, you can use a BigInteger instead, but you'll still need to specify that number in expressions that Java can handle, such as a product of its factors.
I'm trying to accomplish the following without any luck
int a = 1;
double b = 0.5;
int myInteger = (int) a*b;
You've declared b as an integer.
Try this:
int a = 1;
double b = 0.5d;
int myInteger = a*b;
Is your code right? you have two int primitives and no double. With the way you have written it, the b variable will be truncated to 0, as everything after the decimal will be discarded.
I'm guessing this is what you want. Use parens to force the multiplication then cast. Without the parens around a*b the compiler will throw a warning/error due to the loss of accuracy caused by truncation.
int a = 2;
double b = 2.5;
int result = (int) (a*b);
System.out.println(result);
gives 5 as result.
I am trying to figure out why the following is not correctly calculating
x = Math.pow(w,e);
When I calculate it in Java, I get 1075, but I am supposed to get 779.
int e = 17;
int w = 803;
int n = 2773;
double x = 0;
x = Math.pow(w,e) % n;
System.out.println(x);
double floating point numbers have 53 bits of precision, which is not enough to store the exact value of a huge number like 80317. To do modular exponentiation in Java, you can use the modPow method on BigIntegers.
As (803^17) is a very big number so you have to use the BigInteger datatype for the variables used here instead of int or double datatype. We cannot convert the integer or double variable to a BigInteger variable.
So, the program will be :
import java.math.BigInteger;
public class JavaPow {
public static void main(String[] args)
{
BigInteger e = new BigInteger("17");
BigInteger w = new BigInteger("803");
BigInteger n = new BigInteger("2773");
BigInteger x;
x = w.modPow(e,n);
System.out.println(x);
}
}
public static void main (String[] args)
{
char x = 'x';
char w = x-1;
System.out.println(w);
}
Whenever I try to run the following code, I got a loss of precision. The compiler tells me that the line char w = x-1 doesn't seem to work. How can I make the char value equal w?
You need to re-cast to char as you converted to int
char x = 'x';
char w = (char)(x - 1);
System.out.println(w);
That will output w.
Because 1 is an int. You need a cast. Like,
char w = (char) (x - 1);
I ran into this question in an interview and couldn't come up with a solution. I know the vice versa can be done as shown in What does the "+=" operator do in Java?
So the question was like below.
..... x = .....;
..... y = .....;
x += y; //compile error
x = x + y; //works properly
Try this code
Object x = 1;
String y = "";
x += y; //compile error
x = x + y; //works properly
not entirely sure why this works, but the compiler says
The operator += is undefined for the argument type(s) Object, String
and I assume that for the second line, toString is called on the Object.
EDIT:
It makes sense as the += operator is meaningless on a general Object. In my example I cast an int to an Object, but it only depends on x being of type Object:
Object x = new Object();
It only works if x is Object though, so I actually think it is more that String is a direct subclass of Object. This will fail for x + y:
Foo x = new Foo();
for other types that I have tried.
It is not possible.
X x = ...;
Y y = ...;
x += y; //1
//equivalent to
x = (X) (x+y); //2
x = x+y; //3
Suppose the type of x+y is Z. #2 requires a casting conversion from Z to X; #3 requires an assignment conversion from Z to X. "casting conversions are more inclusive than assignment conversions"(1). Therefore, as long as #3 is legal, #2 is legal, and #1 is legal.
On the reverse side, it is possible that #1 is legal, but #3 is illegal, for example
byte x = 0;
int y = 1;
x+=y; // ok, x=(byte)(x+y), cast int to byte is allowed.
x = x+y; // error, assign int to byte
This information is not useful whatsoever; it is a flaw of Java making such surprising differences.
(1) http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5
int i = 5;
String s = "a";
System.out.println(i+=s); //Error
System.out.println(i+s); // No error
Basically, works for Any object or any non-string primitive and String combination.
I wonder which company it was? :)
This thing will not always give you compilation error
If you are doing smoething like this :
class A{
public static void main(String args[]){
String x = "10";
String y = "s";
x += y;
System.out.println(x);
}
}
It will work fine
even if you do
class A{
public static void main(String args[]){
int x = 10;
float y = 11.5F;
x += y;
System.out.println(x);
}
}
it will work properly.
But if you take x and y two different type of variables like :
class X{
}
class A{
public static void main(String args[]){
X x = new X();
float y = 11.5F;
x += y;
System.out.println(x);
}
}
In such cases it will fail to compile.
*Even you can concatinate any int, float etc with String.