02 Programming Building Blocks - mazawi/Teaching-Java GitHub Wiki
- All programs consist of at least one class.
package labexe2;
public class Labexe2 {
public static void main(String[] args) {
int num1 =125;
int num2=28;
int num3=-25;
int avg=(num1+num2+num3)/3;
System.out.println("Num1 = "+ num1);
System.out.println("Num2 = "+ num2);
System.out.println("Num3 = "+ num3);
System.out.println("Average = "+ avg);
}
}
- Java source code file must have same name as class with .java extension.
- Identifiers are used to name classes, variables, and methods
- Identifier Rules:
- Must start with a Character
- A - Z, a - z, _, $, and Unicode letters
- Can contain essentially any number of letters and digits, but no spaces
- Case sensitive
- Number1 and number1 are different
- Cannot be keywords or reserved words
- Must start with a Character
- Are these identifiers valid?
taxRate
- Yes.
char
- No. char is a keyword
intNumber
- Yes, The keyword int is embedded, but the identifier is not identical to a keyword.
2008Taxrate
- No. The identifier starts with a digit
public class Hello
{
public static void main(String\[] args)
{
System.out.println("Hello World!");
}
}
public class <name>
{
public static void main(String[] args)
{
<statement(s)>;
}
}
Every executable Java program consists of a class... that contains a method named main... that contains the statements to be executed The previous program is a class named Hello, whose main method executes one statement named System.out.println
System.out.println
- Java programs use a statement called System.out.println to instruct the computer to print a line of output on the console
- Two ways to use System.out.println :
System.out.println("<Message>");
Prints the given message as a line of text on the console.
System.out.println();
Prints a blank line on the console.
import java.util.Scanner;
public class Aaa
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int rollno=sc.nextInt();
System.out.println ("you inter: " + rollno);
}
}
- Performs some action
- Terminates with a semicolon (;)
- Can span multiple lines
System.out.println( “Programming is ” + “not a spectator sport” );
- 0, 1, or more statements
- Begins and ends with curly braces { }
- Can be used anywhere a statement is allowed
public static void main( String [ ] args )
{
System.out.println( “Hello” );
}
- Space, tab, newline are white space characters
- At least one white space character is required between a keyword and identifier
- Any amount of white space characters are permitted between identifiers, keywords, operators, and literals
int a
1 + 2
public static void main( String [] args)
- Comments explain the program to yourself and others
- Block comments
- Can span several lines
- Begin with /*
- End with */
- Compiler ignores all text between /* and */
/* This program will print a message
Anderson, Franceschi
*/
- Line comments
- Start with //
- Compiler ignores all text from // to the end of the line
System.out.println( “Hello” ); // print Hello