Showing posts with label integer. Show all posts
Showing posts with label integer. Show all posts

Friday, 26 April 2013

Multiplication in Java

For many situations, the int data type will be adequate for storing integer variables. It can hold values between (roughly) + or - 2 billion. You can multiply two integers as follows:

UBUNTU > cat prog7.java
public class prog7
{
public static void main (String args[])
  {
  // Declare 2 integers:
  int num1 = 1234;
  int num2 = 4321;

  // Multiply them together:
  int num3 = num1 * num2;

  // Display the result:
  System.out.println("num1 = " + num1);
  System.out.println("num2 = " + num2);
  System.out.println("num1 * num2 = " + num3);
  }
}
UBUNTU > javac prog7.java
UBUNTU > java prog7
num1 = 1234
num2 = 4321
num1 * num2 = 5332114
UBUNTU >


... but what happens if the result is too big to store in an int variable?

UBUNTU > cat prog8.java
public class prog8
{
public static void main (String args[])
  {
  // Declare 2 integers:
  int num1 = 123456;
  int num2 = 654321;

  // Multiply them together:
  int num3 = num1 * num2;

  // Display the result:
  System.out.println("num1 = " + num1);
  System.out.println("num2 = " + num2);
  System.out.println("num1 * num2 = " + num3);
  }
}
UBUNTU > javac prog8.java
UBUNTU > java prog8
num1 = 123456
num2 = 654321
num1 * num2 = -824525248
UBUNTU >

According to my calculator, the result should be 80,779,853,376 so some arithmetic overflow must have taken place. As soon as I find out how to handle this, I will do a worked example to demonstrate.

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.

 

Monday, 22 April 2013

How to Define Integer Variables in Java

The program below declares 3 integer variables, num1, num2 and num3. It assigns values to num1 and num2 then adds them together and puts the result in num3

UBUNTU >cat prog1.java
public class prog1
{
public static void main (String args[])
  {
  // Declare 3 integer variables:
  int num1, num2, num3;

  // Give them values:
  num1 = 1;
  num2 = 2;
  num3 = num1 + num2;
 
  // Display the result:
  System.out.println
  (num1 + " + " + num2 + " = " + num3);
  }
}
UBUNTU >


I compiled it then ran it and it produced the output below:

UBUNTU >javac prog1.java
UBUNTU >java prog1
1 + 2 = 3
UBUNTU >


Here is another example:

UBUNTU > cat prog25.java
public class prog25
{
public static void main (String args[])
  {
  int a = 10, b = 20, c = 30;
  System.out.println ("a = " + a);
  System.out.println ("b = " + b);
  System.out.println ("c = " + c);
  int x, y, z;
  x = y = z = 40;
  System.out.println ("x = " + x);
  System.out.println ("y = " + y);
  System.out.println ("z = " + z);
  }
}
UBUNTU > javac prog25.java
UBUNTU > java prog25
a = 10
b = 20
c = 30
x = 40
y = 40
z = 40
UBUNTU >