Saturday 25 May 2013

User I/O in Java

This example does console input/output in Java. It asks the user to type his / her name then says Hello.

At the start of the program, the java.io package is imported. This contains code to do system input / output. The asterisk which follows means that the program may want to use any of the classes in java.io.

The program creates an InputStreamReader class variable called isr to read what the user types. It also creates a BufferedReader class variable called br to store it temporarily.

The try block reads the user's input and the catch block handles exceptions: 

UBUNTU > cat prog31.java
import java.io.*;
public class prog31
{
public static void main(String[] args)
  {
  System.out.print("What is your name? ");
  InputStreamReader isr = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(isr);
  try
    {
    String your_name = br.readLine();
    System.out.println("Hello " + your_name);
    }
  catch (IOException test)
    {
    System.out.println("I/O error");
    }
  }
}
UBUNTU > javac prog31.java
UBUNTU > java prog31
What is your name? Andrew
Hello Andrew
UBUNTU >

Thursday 16 May 2013

Java System.out.println and System.out.print

What is the difference between these two?
 
System.out.println outputs a newline character afterwards:
 
UNIX > cat prog28.java
public class prog28
{
public static void main (String args[])
  {
  System.out.println("International DBA");
  }
}
UNIX > javac prog28.java
UNIX > java prog28
International DBA
UNIX >
 
… but System.out.print doesn’t:
 
UNIX > cat prog29.java
public class prog29
{
public static void main (String args[])
  {
  System.out.print("International DBA");
  }
}
UNIX > javac prog29.java
UNIX > java prog29
International DBAUNIX >
 
… although you can add one yourself if you wish:
 
UNIX > cat prog30.java
public class prog30
{
public static void main (String args[])
  {
  System.out.print("International DBA\n");
  }
}
UNIX > javac prog30.java
UNIX > java prog30
International DBA
UNIX >

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 13 May 2013

Operator Precedence in Java

Operator precedence is important in any programming language. This determines which of the arithmetical operators i.e. addition, subtraction, multiplication, division etc is done first.

In ICL's System Control Language (SCL), if I remember correctly, arithmetical operators all had the same precedence and were evaluated from left to right so 1 + 2 * 3 would come to 9. This is quite unusual.

In Java, multiplication and division have a higher priority than addition and subtraction but you can use brackets to override this behaviour. This is similar to a wide variety of other programming languages. You can see what I mean in the example below:

UBUNTU > cat prog27.java
public class prog27
{
public static void main (String args[])
  {
  System.out.println
  ("1 + 2 * 3 = " + (1 + 2 * 3));
  System.out.println
  ("(1 + 2) * 3 = " + ((1 + 2) * 3));
  }
}
UBUNTU > javac prog27.java
UBUNTU > java prog27
1 + 2 * 3 = 7
(1 + 2) * 3 = 9
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.

Saturday 11 May 2013

Java Modulus Operator

Many programming languages have a modulus operator, which allows you to calculate the remainder left over after a division. If I remember correctly, COBOL actually used the word REMAINDER to allow you to do this. In Java, you use the percent symbol as shown below:

UBUNTU > cat prog26.java
public class prog26
{
public static void main (String args[])
  {
  System.out.println
  ("17/5 = " + (int) 17/5 + " remainder " + 17%3);
  System.out.println
  ("15/2 = " + (int) 15/2 + " remainder " + 15%2);
  }
}
UBUNTU > javac prog26.java
UBUNTU > java prog26
17/5 = 3 remainder 2
15/2 = 7 remainder 1
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.

Friday 10 May 2013

How Will I Manage Without Javablogs?

Javablogs has closed. If anybody knows how I might get people to view my blog posts now, please add a comment below. As usual, click on the image to enlarge it and bring it into focus:


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.

Java Newline and Carriage Return Characters

A newline is represented by \n and a carriage return by \r. UNIX only seems to recognize the newline:
 
UNIX > cat prog24.java
public class prog24
{
public static void main (String args[])
  {
  System.out.println("Newline->\n");
  System.out.println("Carriage return->\r");
  }
}
UNIX > javac prog24.java
UNIX > java prog24
Newline->
 
Carriage return->
UNIX >
 
If you copy the program to a Windows environment and run it in a command prompt, both characters are recognized:
 
C:\Users\J0294094\Java>java prog24
Newline->
 
Carriage return->
 
C:\Users\J0294094\Java>

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.

Java Tab Character

You can use \t to tab across in Java. Tab positions appear to be every 8 characters on the UNIX system below:

UNIX > cat prog25.java
public class prog25
{
public static void main (String args[])
  {
  System.out.println("         1         2");
  System.out.println("12345678901234567890");
  System.out.println("Andrew\tReid");
  System.out.println("Andrew\t\tReid");
  }
}
UNIX > javac prog25.java
UNIX > java prog25
         1         2
12345678901234567890
Andrew  Reid
Andrew          Reid
UNIX >

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 9 May 2013

How to Use Hexadecimal Values in Java

You can tell Java that a number is in hexadecimal (base 16) by prefixing it with 0x.
In the example below, the variable hexadecimal_1F is assigned the value 0x1F i.e. hexadecimal 1F. The program then displays the value in decimal (base 10).
 
UNIX > cat prog23.java
public class prog23
{
public static void main (String args[])
  {
  int hexadecimal_1F = 0x1F;
  System.out.println
  ("hexadecimal 1F = " + hexadecimal_1F);
  }
}
UNIX > javac prog23.java
UNIX > java prog23
hexadecimal 1F = 31
UNIX >
 
You can check the result yourself by converting hexadecimal 1F to decimal (base 10) like this:
 
Hexadecimal 1F = (in decimal or base 10) (1 x 16) + 15 = 31
 
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 7 May 2013

How to Use Octal Values in Java

You can tell Java that a number is in octal (base 8) by prefixing it with a zero.

In the example below, the variable octal_77 is assigned the value 077 i.e. octal 77. The program then displays the value in decimal (base 10).

UNIX > cat prog21.java
public class prog21
{
public static void main (String args[])
  {
  int octal_77 = 077;
  System.out.println
  ("octal 77 = " + octal_77);
  }
}
UNIX > javac prog21.java
UNIX > java prog21
octal 77 = 63
UNIX > 

You can check the result yourself by converting octal 77 to decimal (base 10) like this:

octal 77 = (in decimal or base 10) (7 x 8) + 7 = 63

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.

Sunday 5 May 2013

Java boolean Variables

These store the values true or false. There are two in the example below.

The first is called one_greater_than_two and stores the value obtained by evaluating the condition 1 > 2, which is clearly false.

The second is called two_greater_than_one and stores the value obtained by evaluating the condition 2 > 1, which is clearly true.

The tests at the end of the program check that the two booleans have been assigned the correct values:

UBUNTU > cat prog20.java
public class prog20
{
public static void main (String args[])
  {
  boolean one_greater_than_two = (1 > 2);
  boolean two_greater_than_one = (2 > 1);

  if (one_greater_than_two)
    {System.out.println ("1 > 2");}

  if (two_greater_than_one)
    {System.out.println ("2 > 1");}
  }
}
UBUNTU > javac prog20.java
UBUNTU > java prog20
2 > 1
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.

Java char Variables

A Java char variable can store one Unicode value. You can see this in the example below:

UBUNTU > cat prog19.java
public class prog19
{
public static void main (String args[])
  {
  char A = 'A';
  System.out.println ("A = " + A);
  }
}
UBUNTU > javac prog19.java
UBUNTU > java prog19
A = A
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.

Wednesday 1 May 2013

Java Escape Character

If you wanted a Java program to display My name is “Andrew”, or whatever your name happens to be, you might try to do it like this:
 
UNIX > cat prog16.java
public class prog16
{
public static void main (String args[])
  {
  System.out.println
  ("My name is "Andrew"");
  }
}
UNIX > javac prog16.java
prog16.java:6: ')' expected
  ("My name is "Andrew"");
                ^
1 error
UNIX >
 
… but it does not work as the compiler treats the double quote before Andrew as the end of the literal then sees Andrew as an error. You can get round this by placing an escape character before the double quotes at the start and end of Andrew. Java then treats these double quotes just like any other character rather than literal delimiters. The escape character to use is a backwards slash as shown below:
 
UNIX > cat prog17.java
public class prog17
{
public static void main (String args[])
  {
  System.out.println
  ("My name is \"Andrew\"");
  }
}
UNIX > javac prog17.java
UNIX > java prog17
My name is "Andrew"
UNIX >

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 30 April 2013

Java float and double Variables

If you have a number with digits to the right of the decimal point, you can store it in a float or a double variable. However, if you try to assign it directly, it doesn't work:

UBUNTU > cat prog12.java
public class prog12
{
public static void main (String args[])
  {
  float f1 = 1/5;
  double d1 = 1/5;
  System.out.println ("f1 = " + f1);
  System.out.println ("d1 = " + d1);
  }
}
UBUNTU > javac prog12.java
UBUNTU > java prog12
f1 = 0.0
d1 = 0.0
UBUNTU > 


You can add an f (or F) at the end of the number before assigning it to a float variable and you can add a d (or D) at the end before assigning it to a double. Notice how the double has greater precision:

UBUNTU > cat prog13.java
public class prog13
{
public static void main (String args[])
  {
  float f2 = 1/7f;
  double d2 = 1/7d;
  System.out.println ("f2 = " + f2);
  System.out.println ("d2 = " + d2);
  f2 = 1/9F;
  d2 = 1/9D;
  System.out.println ("f2 = " + f2);
  System.out.println ("d2 = " + d2);
  }
}
UBUNTU > javac prog13.java
UBUNTU > java prog13
f2 = 0.14285715
d2 = 0.14285714285714285
f2 = 0.11111111
d2 = 0.1111111111111111
UBUNTU >

Alternatively, you can add (float) before a float variable or (double) before a double. This is called casting:

UBUNTU > cat prog14.java
public class prog14
{
public static void main (String args[])
  {
  float f3 = (float) 1/13;
  double d3 = (double) 1/13;
  System.out.println ("f3 = " + f3);
  System.out.println ("d3 = " + d3);
  }
}
UBUNTU > javac prog14.java
UBUNTU > java prog14
f3 = 0.07692308
d3 = 0.07692307692307693
UBUNTU >

As well as giving greater precision, a double can also store much larger numbers than a float:

UBUNTU > cat prog15.java
public class prog15
{
public static void main (String args[])
  {
  System.out.println
  ("Largest float = " + Float.MAX_VALUE);
  System.out.println
  ("Largest double = " + Double.MAX_VALUE);
  }
}
UBUNTU > javac prog15.java
UBUNTU > java prog15
Largest float = 3.4028235E38
Largest double = 1.7976931348623157E308
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 29 April 2013

Compile Once - Run Anywhere

In the previous example, I compiled and ran prog11 on a Solaris machine. Java is designed to be platform independent or portable. To show that it could be run on a different platform, I copied the prog11.class file to a Windows 7 machine in binary mode. Then I ran it there as follows:
 
C:\Java>dir
Volume in drive C is System
Volume Serial Number is B068-444A
 
Directory of C:\Java
 
04/29/2013  05:37 PM    <DIR>          .
04/29/2013  05:37 PM    <DIR>          ..
04/29/2013  05:36 PM               717 prog11.class
               1 File(s)            717 bytes
               2 Dir(s)  204,152,512,512 bytes free
 
C:\Java>java prog11
a = 100
b = 100
 
C:\Java>

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.

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.

Saturday 27 April 2013

How Big (or Small) Can a Java Long Integer Be?

Java long variables let you store even larger numbers. The program below shows how you can display the maximum and minimum values allowed:

UBUNTU > cat prog9.java
public class prog9
{
public static void main (String args[])
  {
  System.out.println
  ("The largest long you can have is " + Long.MAX_VALUE );
  System.out.println
  ("The smallest long you can have is " + Long.MIN_VALUE );
  }
}
UBUNTU > javac prog9.java
UBUNTU > java prog9
The largest long you can have is 9223372036854775807
The smallest long you can have is -9223372036854775808
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.

 

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.