Showing posts with label int. Show all posts
Showing posts with label int. Show all posts

Thursday, 1 January 2015

How to Increment a Variable in Java

There are a few ways to increment a variable in Java. You can see two in prog60:

andrew@UBUNTU:~/Java$ cat prog60.java
public class prog60
{
public static void main (String args[])
  {
  int a = 1;
  a = a + 1;
  System.out.println("a = " + a);
  int b = 1;
  b += 2;
  System.out.println("b = " + b);
  }
}
andrew@UBUNTU:~/Java$ javac prog60.java
andrew@UBUNTU:~/Java$ java prog60
a = 2
b = 3
andrew@UBUNTU:~/Java$


You can also use the ++ operator before or after the variable as shown in prog61: 

andrew@UBUNTU:~/Java$ cat prog61.java
public class prog61
{
public static void main (String args[])
  {
  int c = 1;
  c++;
  System.out.println("c = " + c);
  int d = 1;
  ++d;
  System.out.println("d = " + d);
  }
}
andrew@UBUNTU:~/Java$ javac prog61.java
andrew@UBUNTU:~/Java$ java prog61
c = 2
d = 2
andrew@UBUNTU:~/Java$


The two examples in prog61 produce the same result so you might ask what is the difference between putting the ++ operator before or after the variable. If you assign the result to a second variable, putting the ++ operator before the first variable increments it before assigning it to the second variable, as you can see in prog62 below:

andrew@UBUNTU:~/Java$ cat prog62.java
public class prog62
{
public static void main (String args[])
  {
  int e = 1;
  int f;
  f = ++e;
  System.out.println("e = " + e);
  System.out.println("f = " + f);
  }
}
andrew@UBUNTU:~/Java$ javac prog62.java
andrew@UBUNTU:~/Java$ java prog62
e = 2
f = 2
andrew@UBUNTU:~/Java$


However, if you put the ++ operator after the first variable, it is assigned to the second variable before it is incremented itself. You can see what I mean in prog63 below:

andrew@UBUNTU:~/Java$ cat prog63.java
public class prog63
{
public static void main (String args[])
  {
  int g = 1;
  int h;
  h = g++;
  System.out.println("g = " + g);
  System.out.println("h = " + h);
  }
}
andrew@UBUNTU:~/Java$ javac prog63.java
andrew@UBUNTU:~/Java$ java prog63
g = 2
h = 1
andrew@UBUNTU:~/Java$

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.

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.