Lecture 2 (Chapter 2) Introduction to Java Programming PDF

Summary

This document is a set of lecture notes about Java programming, focusing on fundamental concepts, including data items, methods, main method, case sensitivity, and console windows. It also explains string concepts, including escape sequences, and introduces concepts of the Java programming language such as identifiers and syntax.

Full Transcript

Programming 1 Lecture 2 Chapter 2: Introduction to Java Programming Dr/ Mahmoud Gamal 1 A Java Program: Output: 2 A Java Program: Package: Group of classes. Class: 1. Data items 2. Methods (...

Programming 1 Lecture 2 Chapter 2: Introduction to Java Programming Dr/ Mahmoud Gamal 1 A Java Program: Output: 2 A Java Program: Package: Group of classes. Class: 1. Data items 2. Methods ( or functions or operations) The main method is the entry point to the program. Java is case sensitive language. Console window: Text box into which the program's output is printed. 3 Structure of a Java program class: a program public class name { public static void main(String[] args) { Body of statement; statement; main method: a named group method... statement; of statements } } statement: a command to be executed Every executable Java program consists of a class, that contains a method named main, that contains the statements (commands) to be executed. System.out.println Statement: A statement that prints a line of output on the console. Two ways to use System.out.println : System.out.println("text"); Prints the given message as output. System.out.println(); Prints a blank line of output. 5 Example: public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } } Output: Hello, world! This program produces four lines of output 6 Compile/run a program 1. Write it. code or source code: The set of instructions in a program. 2. Compile it. javac: translates the program from Java to bytecode bytecode: runs on many computer types (any computer with JVM) 3. Run (execute) it. output: whatever the programmer instructs the program to do source code byte code output compile run Hello, World! javac Hello.java java Hello Identifier: A name given to an item in your program. Must start with a letter or _ or $ Do not use space or any special character. Do not use any reserved name like (void, class, int) Do not start with a number. legal: _myName TheCure ANSWER_IS_42 $bling$ illegal: me+u 49ers side-swipe Ph.D’s Example: public class MyClass { 8 Syntax: Syntax: The set of legal structures and commands that can be used in a particular language. Every basic Java statement ends with a semicolon ; The contents of a class or method occur between { and } 9 Programming Errors: 1- Syntax Errors: Detected by the compiler. 2- Runtime Errors: Causes the program to abort. 10 Programming Errors: 3- Logic Errors: Produces incorrect results. 11 Strings: string: A sequence of characters to be printed. Starts and ends with a " quote " character. The quotes do not appear in the output. Examples: "hello" "This is a string. It's very long!" Restrictions: May not span multiple lines. "This is not a legal String." May not contain a " character. "This is not a "legal" String either."12 Escape sequence: Escape Sequence: A special sequence of characters used to represent certain special characters in a string. \t tab character \n new line character \" quotation mark character \\ backslash character Example: System.out.println("\\hello\nhow\tare \"you\"?\\\\"); Output: \hello how are "you"?\\ 13 Questions What is the output of the following println statements? System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral"); Answer: a b c \\ ' """ C: in he downward spiral Questions Write a println statement to produce this output: / \ // \\ /// \\\ Answer: System.out.println("/ \\ // \\\\ /// \\\\\\"); Comments: Comment: A note written in source code by the programmer to describe or clarify the code. Comments are not executed when your program runs. Syntax: // comment text, on one line or, Examples: // This is a one-line comment. Using comments Where to place comments: at the top of each file (a "comment header") at the start of every method (seen later) to explain complex pieces of code Comments are useful for: Understanding larger, more complex programs. Multiple programmers working together, who must understand each other's code. Comments Example public class BaWitDaBa { public static void main(String[] args) { // first verse System.out.println("Bawitdaba"); System.out.println("da bang a dang diggy diggy"); System.out.println(); // second verse System.out.println("diggy said the boogy"); System.out.println("said up jump the boogy"); } } print vs println: println print

Use Quizgecko on...
Browser
Browser