liv2hak
liv2hak

Reputation: 14990

java: simple type conversion

I have written my very first java program on eclipse on windows.I have recently started programming java on linux.

When I try to compile the above program on Linux it does work fine but when I try to do it on windows I get the following error.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Type mismatch: cannot convert from int to short
    Type mismatch: cannot convert from double to float

public class TypeDemo {

    public static void main (String args[])
    {
            byte b = 8;
            long a = 1000011334;
            int c = 76;
            short s = 99789;

            float f = 99.99;
            double d = 99979.9879;
            boolean bool = true;
            char ch = 'y';

            System.out.println ("b = "+b);
            System.out.println ("a = "+a);
            System.out.println ("c = "+c);
            System.out.println ("s = "+s);
            System.out.println ("f = "+f);
            System.out.println ("d = "+d);
            System.out.println ("bool = "+bool);
            System.out.println ("ch = "+ch);
    }
}

Upvotes: 0

Views: 2375

Answers (7)

user2059300
user2059300

Reputation: 361

The error is in:

short s = 99789;

The "short" data type has a maximum value of 32767.

99789 > 32767 which will result in a loss of precision.

Instead try:

int s = 99789;

Similarly:

float f = 99.99;

Should be a double:

double f = 99.99;

Hope that helps!

Upvotes: 0

Sachin Verma
Sachin Verma

Reputation: 3802

short s = 99789;

short is a 16 bit value while int is of 32 bit.99789 or any number is predefined an int in java. so you have to manually cast in you are narrowing data types.

 short s = (short) 99789;

Upvotes: 0

Govind Parmar
Govind Parmar

Reputation: 21542

A short is a 16 bit integer (0-65535 unsigned; −32768-32767 signed). Your value for s is too big so it's being treated as int instead.

Java is seeing 99.99 as a double by default. Simply append f to the end of that (float f = 99.99f) to fix that problem.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213261

When I try to compile the above program on Linux it does work fine

That's in fact quite surprising, and I can't buy it. Check it again, it shouldn't.

short is a 16-bit signed 2's complement integer. It's maximum value is 32767, and you're assigning 99789 to it. It's definitely out range. You need to explicitly typecast it to short:

short s = (short)99789;
short s1 = 100;   // this would however work

Although you would see strange output there. The extra bits will be truncated. It's better to use an int directly.

Now, in case of float, floating point literals are by default double. To get float, you need to append an F or f at the end. So, change that one to:

float f = 99.99f;

Upvotes: 4

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

        `float f = 99.99;`     //need to include f at last or need to type cast;

need to be

            float f = 99.99f;
            or
            float f = (float)99.99; 

       short s = 99789;      //exceeded the limit  

The limit should be

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.

More on java data types please refer here http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Upvotes: 0

ankit
ankit

Reputation: 5127

Use this code

in your code you are trying to assign bigger data type to smaller ones, which will end up in loss of precision, which is not allowed so you need to an explicit type cast

short s = (short) 99789; and for float you have two opetions

float f = 99.99f;

or

float f = (float)99.99;(because decimal values are double by default)

public static void main(String args[]) {
    byte b = 8;
    long a = 1000011334;
    int c = 76;
    short s = (short) 99789;

    float f = 99.99f;
    double d = 99979.9879;
    boolean bool = true;
    char ch = 'y';

    System.out.println("b = " + b);
    System.out.println("a = " + a);
    System.out.println("c = " + c);
    System.out.println("s = " + s);
    System.out.println("f = " + f);
    System.out.println("d = " + d);
    System.out.println("bool = " + bool);
    System.out.println("ch = " + ch);
}

remember only those conversions are allowed which will not result in loss of precision

Upvotes: 0

kamshi
kamshi

Reputation: 615

That's an easy one. You need to do a type cast:

float f = 99.99f;

or

float f = (float)99.99;  // This is a type cast

You are trying to store a "large" number into a short variable. short can only store values from -32.768 to 32.767.

Upvotes: 0

Related Questions