CS110: PROGRAMMING LANGUAGE I Computer Science Department
Lecture 2: Java Basics (I)
Lecture Contents:
A java program Java language Basics
Comments
Reserved words
Identifiers
The main method
Text Output
Strings
Arithmetic Operators, Expressions, and Operator Precedence dr. Amal Khalifa,Fall14
A java Program Class in file .java class keyword braces { , } delimit a class body main Method // indicates a comment. white space Java is case sensitive
“Everything must be in a class” There are no global functions or global data. dr. Amal Khalifa,Fall14
Java Basics : comments
Compiler ignores comments.
Used to document programs and improve their readability.
Comments // Fig. 2.1: Welcome1.java
A comment that begins with // is an end-of-line comment—it terminates at the end of the line on which it appears.
Traditional comment, can be spread over several lines as in /* This is a traditional comment. It can be split over multiple lines */
Javadoc comments Delimited by /** and */. Enable you to embed program documentation directly in your programs. dr. Amal Khalifa,Fall14
Be careful!!
dr. Amal Khalifa,Fall14
Java Basics : reserved words
Keywords (Appendix C) are reserved for use by Java and are always spelled with all lowercase letters public static class
void
dr. Amal Khalifa,Fall14
Java Basics : identifiers A class
name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces. Java is case sensitive—uppercase and lowercase letters are distinct—so a1 and A1 are different (but both valid) identifiers. By convention, begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). dr. Amal Khalifa,Fall14
Java basics: main method
Declaring the main Method public static void main( String[] args )
Starting point of every Java application. Parentheses after the identifier main indicate that it’s a program building block called a method. Java class declarations normally contain one or more methods. main must be defined as shown; otherwise, the JVM will not execute the application. Methods perform tasks and can return information when they complete their tasks. Keyword void indicates that this method will not return any information.
dr. Amal Khalifa,Fall14
Java Basics: Text output
Statement System.out.println("Welcome to Java Programming!");
Instructs
the computer to perform an action
Print
the string of characters contained between the double quotation marks.
A string
is sometimes called a character string or a string literal. White-space characters in strings are not ignored by the compiler. Strings cannot span multiple lines of code. dr. Amal Khalifa,Fall14
Example System.out object Multiple line O/P Formatting output statements end with a semicolon
dr. Amal Khalifa,Fall14
Escape Sequences
"Extend" character set Backslash, \ preceding a character Instructs
compiler: a special "escape character" is coming Following character treated as "escape sequence char"
dr. Amal Khalifa,Fall14
Common escape sequences
dr. Amal Khalifa,Fall14
Example
dr. Amal Khalifa,Fall14
String Literals A literal is a constant value that appears directly in the program. A String literal consists of a group of characters. Ex: "Welcome to Java"
dr. Amal Khalifa,Fall14
dr. Amal Khalifa,Fall14
String concatenation // Three strings are concatenated "Welcome " + "to " + "Java"
// String Chapter is concatenated with number 2 "Chapter" + 2 // String Supplement is concatenated with character B "Supplement" + “b” dr. Amal Khalifa,Fall14
Public class Test1 { public static void main(String args[]){ String msg; String msg2= + msg; System.out.print ("message1 =“ + "I" + "Like“); System.out.println ("\n message 2 =" + "Java“ + "\t"+ “end”); } }
Example : program output
dr. Amal Khalifa,Fall14
Displaying Text with printf
System.out.printf method
Multiple method arguments are placed in a comma-separated list. Java allows large statements to be split over many lines. Method printf’s first argument is a format string
f means “formatted” displays formatted data
May consist of fixed text and format specifiers. Fixed text is output as it would be by print or println. Each format specifier is a placeholder for a value and specifies the type of data to output.
Format specifiers begin with a percent sign (%) and are followed by a character that represents the data type. Format specifier %s is a placeholder for a string. dr. Amal Khalifa,Fall14
Example
dr. Amal Khalifa,Fall14
The arithmetic operators are binary operators because they each operate on two operands.
Java basics : Arithmatic operators The asterisk (*) indicates multiplication The percent sign (%) is the remainder operator The remainder operator, %, yields after division. dr. the Amalremainder Khalifa,Fall14
Java Basics: Expressions
Examples: 5
/ 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division)
Remainder is very useful in programming. an
even number % 2 is always 0 and an odd number % 2 is always 1.
So you can use this property to determine whether a number is even or odd dr. Amal Khalifa,Fall14
Operator precedence
Rules
Multiplication, division and remainder operations are applied first. If an expression contains several such operations, they are applied from left to right. Multiplication, division and remainder operators have the same level of precedence. Addition and subtraction operations are applied next. If an expression contains several such operations, the operators are applied from left to right. Addition and subtraction operators have the same level of precedence. Parentheses are used to group terms in expressions in the same manner as in algebraic expressions. If an expression contains nested parentheses, the expression in the innermost set of parentheses is evaluated first. dr. Amal Khalifa,Fall14
Operator precedence
dr. Amal Khalifa,Fall14
dr. Amal Khalifa,Fall14
More examples
What is the order of evaluation in the following expressions? 3 + 4 + 5 + 6 + 2 1 2 3 4
3 + 4 * 5 - 6 / 2 3 1 4 2
3 / (4 + 5) - 6 % 2 2 1 4 3 3 / (4 * (5 + (6 - 2))) 4 3 2 1 dr. Amal Khalifa,Fall14
Case study: Writing Expressions
Write a program that computes and displays the result of the following expressions 3 + 4 + 5 + 6 + 2 3 + 4 * 5 - 6 / 2 3 / (4 + 5) - 6 % 2 3 / (4 * (5 + (6 - 2)))
dr. Amal Khalifa,Fall14
Individual Arithmetic Precision
Calculations done "one-by-one"
1 / 2 / 3.0 / 4 performs 3 separate divisions.
First 1 / 2 equals 0 Then 0 / 3.0 equals 0.0 Then 0.0 / 4 equals 0.0!
So not necessarily sufficient to change just "one operand" in a large expression
Must keep in mind all individual calculations that will be performed during evaluation!
dr. Amal Khalifa,Fall14
Debugging
errors are called bugs. When you type a program, typos and unintentional syntax errors are likely to occur. Therefore, when you compile a program, the compiler will identify the syntax errors. Debugging means to identify and fix syntax errors.
dr. Amal Khalifa,Fall14
class Test2 { public static void main (String[] args) { System.out.println("*\n java \n*”); System.out.println("I" * ‘Love’); System.out.println(3 + 2) }
Example Find and fix error!!
dr. Amal Khalifa,Fall14
That’s all for this week!! Text Book: [2] Chapter 2: 2.1 2.7
dr. Amal Khalifa,Fall14