Title: Introduction to Java Applications
1Introduction to Java Applications
2- // Fig. 2.1 Welcome1.java
- // Text-printing program.
- public class Welcome1
-
- // main method begins execution of Java
application - public static void main( String args )
-
- System.out.println( "Welcome to Java
Programming!" ) - // end method main
- // end class Welcome1
- Welcome to Java Programming!
3Comments
- // single-line comment
- / / multi-line comment
- / / Javadoc comment
- Javadoc comments can be read by the javadoc
utility to produce HTML documentation.
4Case-sensitive
- Java is case sensitive.
- Using system.out.println instead of
System.out.println is an error. - By convention, class names should begin with an
uppercase letter.
5Filenames
- The filename must match the name of the public
class defined inside the file. - Error if not.
public class Car
Filename Car.java
6Filenames
- Java source files must have the .java extension.
Wont compile if not.
public class Car
Filename Car.java
7Filenames
public class Car
Filename Car.java
javac Car.java // compile
bytecode
Filename Car.class
java Car // execute
8Main
- The main method is the starting point of a Java
application - public static void main(String args )
-
-
-
9Printing
- System.out is the standard output object.
- System.out.println(..) // prints a line
- System.out.print(.) // prints on same line
10- // Fig. 2.3 Welcome2.java
- // Printing one line of text with multiple
statements. - public class Welcome2
-
- // main method begins execution of Java
application - public static void main( String args )
-
- System.out.print( "Welcome to " )
- System.out.println( "Java Programming!" )
- // end method main
- // end class Welcome2
- Welcome to Java Programming!
11- // Fig. 2.4 Welcome3.java
- // Printing multiple lines with a single
statement. - public class Welcome3
-
- // main method begins execution of Java
application - public static void main( String args )
-
- System.out.println( "Welcome\nto\nJava\nProg
ramming!" ) - // end method main
- // end class Welcome3
- Welcome
- to
- Java
- Programming!
12Escape Characters
- \n newline
- \t tab
- \r carriage return
- \\ backslash
- \ quote
13Printf
- The C language used the printf function to print
text. - printf made use of a string containing
placeholders. - Examples are s for strings, d for numbers.
- A new feature in J2SE 5.0 is the printf method of
the standard output stream.
14- // Fig. 2.6 Welcome4.java
- // Printing multiple lines in a dialog box.
- public class Welcome4
-
- // main method begins execution of Java
application - public static void main( String args )
-
- System.out.printf( "s\ns\n",
- "Welcome to", "Java Programming!" )
- // end method main
- // end class Welcome4
- Welcome to
- Java Programming!
15Inputting
- System.in is the standard input stream.
- The Scanner class is used to read from the
standard input stream. - To use the Scanner class, we must import its
package at the top of our program. - Once a Scanner object is created, its nextInt()
method can be called to find the next integer of
input.
16import java.util.Scanner // program uses class
Scanner public class Addition // Fig. 2.7
Addition.java, displays the sum of two
numbers. // main method begins execution of
Java application public static void main(
String args ) // create Scanner to
obtain input from command window Scanner
input new Scanner( System.in ) int
number1 // first number to add int
number2 // second number to add int sum
// sum of number1 and number2
System.out.print( "Enter first integer " ) //
prompt number1 input.nextInt() // read
first number from user System.out.print(
"Enter second integer " ) // prompt
number2 input.nextInt() // read second number
from user sum number1 number2 // add
numbers System.out.printf( "Sum is d\n",
sum ) // display sum // end method main
// end class Addition
17Arithmetic
- Example
- z p r q w/x y
- 6 1 2 4 3 5 // order of
evaluation
18Equality and Relational Operators
- Equality operators , !
- Relational operators gt,lt,gt,lt
- These operators associate left to right.
- Equality operators have lower precedence than the
relational operators.
19 int number1 777 // first number to
compare int number2 777 // second number
to compare if ( number1 number2 )
System.out.printf( "d d\n", number1,
number2 ) if ( number1 ! number2 )
System.out.printf( "d ! d\n", number1,
number2 ) if ( number1 lt number2 )
System.out.printf( "d lt d\n", number1,
number2 ) if ( number1 gt number2 )
System.out.printf( "d gt d\n", number1,
number2 ) if ( number1 lt number2 )
System.out.printf( "d lt d\n", number1,
number2 ) if ( number1 gt number2 )
System.out.printf( "d gt d\n", number1,
number2 )
777 777 777 lt 777 777 gt 777
20End of Slides