Showing posts with label possible loss of precision. Show all posts
Showing posts with label possible loss of precision. Show all posts

Monday, 29 April 2013

How to Copy a long Java Variable Into an int

You might think that you could copy a long variable into an int variable like this:
 
Solaris > cat prog10.java
public class prog10
{
public static void main (String args[])
  {
  int a;
  long b = 100;
  a = b;
  System.out.println ("a = " + a);
  System.out.println ("b = " + b);
  }
}
Solaris >
 
… but you get a compilation error if you try:
 
Solaris > javac prog10.java
prog10.java:7: possible loss of precision
found   : long
required: int
  a = b;
      ^
1 error
Solaris >
 
You have to do it like this instead:
 
Solaris > cat prog11.java
public class prog11
{
public static void main (String args[])
  {
  int a;
  long b = 100;
  a = (int) b;
  System.out.println ("a = " + a);
  System.out.println ("b = " + b);
  }
}
Solaris > javac prog11.java
Solaris > java prog11
a = 100
b = 100
Solaris >

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

Thursday, 25 April 2013

Java Short Integers

These are 16-bit signed integers. They can store values between -32768 and +32767 both inclusive. If you try to assign numbers outside this range, you get a compilation error: 

UBUNTU > cat prog5.java
public class prog5
{
public static void main (String args[])
  {
  // Declare a short integer:
  short num1;

  // Try to assign some invalid values:
  num1 = -32769;
  num1 = 32768;
  }
}
UBUNTU > javac prog5.java
prog5.java:9: possible loss of precision
found   : int
required: short
  num1 = -32769;
          ^
prog5.java:10: possible loss of precision
found   : int
required: short
  num1 = 32768;
         ^
2 errors
UBUNTU >

...but numbers within the range are accepted:

UBUNTU > cat prog6.java
public class prog6
{
public static void main (String args[])
  {
  // Declare a short integer:
  short num1;

  // Assign the minimum valid value:
  num1 = -32768;

  // Display the value:
  System.out.println("num1 = " + num1);

  // Assign the maximum valid value:
  num1 = 32767;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog6.java
UBUNTU > java prog6
num1 = -32768
num1 = 32767
UBUNTU >


If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

 

Tuesday, 23 April 2013

Java Byte-Size Integers

This post checks what you can and cannot store in a Java byte variable. In prog2, I tried to store -129 in a Java byte variable:

UBUNTU > cat prog2.java
public class prog2
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = -129;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU >


... but I was unable to compile it:

UBUNTU > javac prog2.java
prog2.java:9: possible loss of precision
found   : int
required: byte
  num1 = -129;
          ^
1 error
UBUNTU >


In prog3, I tried to use a Java byte variable to store -128 and this worked:

UBUNTU > cat prog3.java
public class prog3
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = -128;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog3.java
UBUNTU > java prog3
num1 = -128
UBUNTU >
 


In prog4, I tried to use a Java byte variable to store 127 and this worked too:

UBUNTU > cat prog4.java
public class prog4
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = 127;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog4.java
UBUNTU > java prog4
num1 = 127
UBUNTU >

In prog5, I tried to store 128 in a Java byte variable but this produced a compilation error:

UBUNTU > cat prog5.java
public class prog5
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = 128;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog5.java
prog5.java:9: possible loss of precision
found   : int
required: byte
  num1 = 128;
         ^
1 error
UBUNTU >

So it seems that Java byte variables can be used to store integers from -128 to +127, both inclusive.


If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.