Showing posts with label cat. Show all posts
Showing posts with label cat. Show all posts

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

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.

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

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.

 

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.

 

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 >