First lab is all about printing some lines and general formatting. In this lab the main things you will need is understanding of System.out.println("");. What this commands does is simply prints a line. If you pass a text in it as System.our.println("This blog is awesome"); it will print This blog is awesome with a new empty line below the text and if you use print command inspite of println command it will only print the text without the new line below it.
**Creating a Program**
=================
//Example.java
public class Example
{
public static void main (String args[])
{
System.out.println("Hi! world, I am a Computer");
}
}
In this above program the file name is Example.java so it has the class name "Example". public class Example is the code that makes a class named Example. We put {} to start and end the body of any class or methods.
public static void main (String args[]) is the main method. This line of code makes the program compilable. We cannot run any java file without main method.
System.out.println("Hi! world, I am a Computer"); prints the line inside the double quotes. The common mistake will be the ; at the end which is needed to end the code.
We can use many parameters with System command, commonly used may be:
System.err.something(); //prints the error
System.exit(0); // exits the program
System.in(); //Starts input Stream
System.out.something(); // prints something
**Formatting**
============
public
class
Example
{
public static void main (String args[])
{
System.out.println("Hi! world, I am a Computer");
}
}
The above style of programming for java will not be a problem because java is smart enough to tackle with the spaces but the spaces in the output printing line will make a difference
System.out.println("Hi! world, I am a Computer")
Above command will print the spaces between the words so there will be a long space between world and I, this can be also handy in formatting but it usually doesn't helps. Common formatting can be done by:
\n // new line
\t //tab
/n will give a new line
System.out.println("Hi! world, \nI am a Computer");
The above code will print the line as
Hi! world,
I am a Computer
System.out.println("Hi! world, \tI am a Computer");
Hi! world, I am a Computer
You can also combine \t and \n as \n\t or \t\n for formatting.
First Lab
Monday, June 2, 2008
Posted by Admin at 7:38 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment