Showing posts with label Herbert Schildt. Show all posts
Showing posts with label Herbert Schildt. Show all posts

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.

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.