Summary

This document is a lecture on programming concepts focusing on Java. It includes various topics such as characteristics of Java, anatomy of a Java program, programming style and documentation, and programming errors, with examples to illustrate each.

Full Transcript

Programming Lecture 2: Introduction to Java School of Computing and Engineering University of West London, UK Dr. Ikram Ur Rehman Overview Java Characteristics Anatomy of a Java Program Programming Styling and Documentation Types of Errors Variables and Data Types Arithmetic Ope...

Programming Lecture 2: Introduction to Java School of Computing and Engineering University of West London, UK Dr. Ikram Ur Rehman Overview Java Characteristics Anatomy of a Java Program Programming Styling and Documentation Types of Errors Variables and Data Types Arithmetic Operators Why Java? Java is a general purpose programming language. Java is the Internet programming language. More than 3 billion devices run Java. Java is used to develop apps for Google's Android OS (handheld devices), various Desktop Applications, such as media players, antivirus programs, Web Applications, Enterprise Applications (i.e. banking), and many more! The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. Characteristics of Java Java is Simple Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer negative aspects. Java is Object-Oriented Java was designed from the start to be object-oriented. Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. One of the central issues in software development is how to reuse code. Object- oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism. Characteristics of Java Java is Robust Java compilers can detect many problems that would first show up at execution time in other languages Java implements several security Java is Secure mechanisms to protect your system against harm caused by stray programs Java is Portable Java programs can be run on any platform The list continues……………. Popular Java IDEs NetBeans (The one we are using) Eclipse Compiling Java Source Code With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode The bytecode can then run on any computer with a Java Virtual Machine Your 1st Java Program public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } This program prints Welcome to Java! ❖ In Java, every line of code that can actually run needs to be inside a class. ❖ In Java, each application has an entry/starting point, which is a method called main. Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words Comments Blocks Anatomy of a Java Program Class Name Every Java program must have at least one class Each class has a name. By convention, class names start with an uppercase letter. For Example: public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Anatomy of a Java Program Main Method In order to run a class, the class must contain a method named main. The program is executed from the main method. For Example: // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Anatomy of a Java Program Statement A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") to display the greeting "Welcome to Java!“. For Example: public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Anatomy of a Java Program Statement Terminator Every statement in Java ends with a semicolon (;) Do not use semicolons after method and class declarations that follow with the body defined using the curly braces. For Example: public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Anatomy of a Java Program Reserved Words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For instance, when the compiler sees the word class, it understands that the word after class is the name for the class. For Example: public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Anatomy of a Java Program Blocks A pair of braces in a program forms a block that groups components of a program. For Example: public class Test { public static void main(String[] args) { Class block System.out.println("Welcome to Java!"); Method block } } Anatomy of a Java Program Special Symbols Character Name Description {} Opening and closing Denotes a block to enclose statements. braces () Opening and closing Used with methods. parentheses [] Opening and closing Denotes an array. brackets // Double slashes Precedes a comment line. " " Opening and closing Enclosing a string (i.e., sequence of characters). quotation marks ; Semicolon Marks the end of a statement. Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles Programming Style and Documentation Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. Java supports both single and multi-line comments. Remember: All characters that appear within a comment are ignored by the Java compiler. Programming Style and Documentation Single-Line Comments A single-line comment starts with two forward slashes and continues until it reaches the end of the line. For Example: // this is a single-line comment x = 5; // a single-line comment after code Adding comments as you write code is a good practice, when you need to refer back to it, as well as for others who might need to read it. Programming Style and Documentation Multi-Line Comments Java also supports comments that span multiple lines. For Example: Programming Style and Documentation Naming Conventions Choose meaningful and descriptive names. Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeExpression. Programming Style and Documentation Proper Indentation and Spacing Indentation Indent two spaces. Spacing Use blank line to separate segments of the code. Programming Style and Documentation Block Styles Use end-of-line style for braces. For Example: Next-line public class Test style { public static void main(String[] args) { System.out.println("Block Styles"); } } End-of-line style public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } Programming Errors Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic Errors Produces incorrect result Variables Variables store data for processing. A variable is given a name (or identifier), such as area, age, height etc. You can assign a value to the variable and retrieving the value stored. You have created a variable called For example: String name = “Joe”; name, which is type string and assigned it a value Joe Note that a variable is associated with a type, and is only capable of storing values of that particular type. Variables Integer Double Char String Another type is the Boolean type, which has only two possible values: true and false (conditions). Example: Boolean attendance = true; Note: You can use a comma to declare more than one variable of the specified type. Example: int x = 10, y = 12; Arithmetic Operators Java provides a rich set of operators. See the list below: + addition - subtraction * multiplication / division % modulo A value used on either side of an operator is called an operand. For example: int x = 2 + 4; The numbers 2 and 4 are operands of the plus operator Addition and Subtraction The (+)(-) operator adds and subtracts two values, such as: two constants a constant and a variable a variable and a variable. For example: Similar to algebra, you can use both of the operations in a single line. For example: int x = 5+ 4 - 3; Multiplication and Division The * operator multiplies two values, such as: The / operators divides one value by another In the example above, the result of the division equation will be a whole number, as int is used as the data type. You can use double to retrieve a value with a decimal point. Modulo The modulo (or remainder) returns the remainder of the division. The operator is denoted by the (%) sign. For Example:

Use Quizgecko on...
Browser
Browser