Lecture 1 Java
Lecture 1 Java
But the invention of the microprocessor reduced their sizes to the present day desktop
computers, laptop computers and palmtop computers.
What is a Program?
- A set of instructions to be carried out by a computer.
What is program execution?
- It is the act of carrying out the instructions contained in a program.
What is programming Language?
- It is a systematic set of rules used to describe computations in a format that is editable
by humans.
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 1
History of Programming Languages
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 2
2. functional programming: functions map inputs to outputs
Lisp (1958) / Scheme (1975), ML (1973), Haskell (1990)
3. object-oriented languages: programs use interacting "objects"
Smalltalk (1980): first major object-oriented language
C++ (1985): "object-oriented" improvements to C
successful in industry; used to build major OSes such as Windows
Java (1995): designed for embedded systems, web apps/servers
Runs on many platforms (Windows, Mac, Linux, cell phones...)
What is Java?
- Java is a complete programming language developed by Sun
- Can be used to develop either web based or stand-alone software
Some editors pop up the console as an external window, and others contain
their own console window.
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 3
Compiling/running a program
Class activity 1: Type the above hello world program on your computer, compile it, execute it
and display the result.
Class activity 2: Write a simple Java program to display your name on the console.
Class activity 3: Write a simple Java program to display hello world, your name.
How can you modify your program to display the same output on different lines?
The code in this program instructs the computer to print four lines of messages on the
screen.
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 4
Its output:
1. Hello, world!
2.
3. This program produces
4. four lines of output
The above two programs have only one class (hello) and one method (main). Now let us
understand what their meanings and structure of Java programming.
Syntax: The set of legal structures and commands that can be used in a
particular programming language.
Some Java syntax:
- every basic Java statement ends with a semicolon ;
- The contents of a class or method occur between { and }
Class activity 4: Write the class name and method name in the above basic java program 1
Class name ____________________
Method name___________________
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 5
Class activity 5: Write the class name and method name in the above basic java program 2
System.out.println
i. System.out.println("<Message>");
- Prints the given message as a line of text on the console.
ii. System.out.println();
- Prints a blank line on the console.
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 6
- \t tab character
- \n new line character
- \" quotation mark character
- \\ backslash character
1. Program using \t in string of the println statement
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, \tworld!");
}
}
2. Program using \n in string of the println statement
5. Observe the following program. Just type it on your computer, compile and run it to
see the output. What output did you notice? Is there any problem?
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 7
byte code
Hello.class
Syntax error or compiler error:
We'd have preferred a friendly message such as, "You misspelled public"
The compiler does tell us the line number on which it found the error...
But it is not always the true source of the problem.
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 8
public class MissingSemicolon {
Advanced Programming teaching material – Prepared by Mrs. Krishna Kumari Ganga Page 9