Showing posts with label Data Type. Show all posts
Showing posts with label Data Type. Show all posts

Friday, October 30, 2009

Published 10/30/2009 by with 1 comment

incompatible types

1. Sometimes we may get the following error message in an 'if' (selection) structure.

incompatible types
found : int
required: boolean

This is because, we have not specified a boolean expression as the condition of the 'if'; instead we might have written such an expression which is may be int, float, String or other types but not boolean.

See the code below:

int a=10,b=10;

if(a=b)
{
System.out.println("a and b are equal");
}

Check the condition is a=b. Is it a boolean expression? By using a single = (equal sign), value of b is assigned to a which is an integer. That's not correct. We must use == (double equal sign) for comparing two variables for equality.

Correct code is the below:

int a=10,b=10;

if(a==b)
{
System.out.println("a and b are equal");
}


2. Sometimes it happens because of assigning one type of value to a variable of other types. See the code below:

String text='a';
char ch="a";
In this case the error message will be:

incompatible types
found : char
required: java.lang.String
String text='a';
^
incompatible types
found : java.lang.String
required: char
char ch="a";
^
Here we have not assigned correct value for the variables text and ch. In the first line, we have assigned a char value to a String variable. 'a' is a char value because it is written within ' ' (single quotation mark) . In the second line, we have assigned String value "a" to a char type variable. Note that String value is written within " " (double quotation mark). So we tried to assign char value to a String variable and a String value to a char variable. The correct code is:

String text="a";
char ch='a';

If you see this error, check that the correct type of value or variable is assigned.

Read More
    email this       edit

Sunday, November 9, 2008

Published 11/09/2008 by with 2 comments

possible loss of precision

You may see the error message 'possible loss of precision' for any of the following reasons:

1) You are trying to assign a fractional number into a float variable(why not?). Actually Java compiler considers the fractional number as double by default. You need to explicitly specify the fractional number as float by giving the character 'f' or 'F' after the the number, like float number = 65.37F;

2) Or you are trying to assign the value of a double variable into a float variable. Memory size of double is 8 byte and that of float is 4 byte. If you assign a double value into a float value data may be lost. So be sure about the data type. If you are sure you want to assign the double into float, you need to cast it appropriately, like float floatVariable = (float)doubleVariable;

An example is given below:

public class PossibleLossOfPrecision
{
public static void main(String args[])
{

float floatNumber1=65.37; // error
double doubleNumber=65.37;
float floatNumber2=doubleNumber; // error

System.out.println(floatNumber1);
System.out.println(doubleNumber);
System.out.println(floatNumber2);

}
}


The above code is wrong; write this code as below and the problem will be solved.

public class PossibleLossOfPrecisionSolution
{
public static void main(String args[])
{

float floatNumber1=65.37F; // error solved
double doubleNumber=65.37;
float floatNumber2=(float)doubleNumber; // error solved

System.out.println(floatNumber1);
System.out.println(doubleNumber);
System.out.println(floatNumber2);

}
}
Read More
    email this       edit
Published 11/09/2008 by with 1 comment

integer number too large

We know that the maximum value of an int is 2147483647. If we write any whole number more than 2147483647 in our Java program, we may get this error message 'integer number too large' but may be you are trying to store this large number in a long type variable, then what to do? The solution is to put the letter 'l' or 'L' after the number. This happens, because the compiler considers the whole number as int by default and we specify the large number by character 'l' or 'L' as long number. Now the compiler will consider the number as long.

For example, we are trying to compile the following Java code:
public class IntegerNumberTooLarge
{
public static void main(String args[])
{
long number=965478213245;
System.out.println(number);
}
}

We will get 'integer number too large' from the above program. Now write the program as below the problem will be solved.

public class IntegerNumberTooLargeSolution
{
public static void main(String args[])
{
long number=965478213245L;
System.out.println(number);
}
}
Read More
    email this       edit