Java Programming Lesson 1
Java Programming Lesson 1
Introduction to JAVA
KIIT 2014
Objectives
After completing this lesson, you should be able
to do the following:
• Define JAVA language
• Features of JAVA
• History of JAVA
• Hardware & Software Requirement
• JAVA Environment Setup
• Write Simple JAVA Program
• Define Identifiers
• Types of Variables
• Java Comments
KIIT 2014
Introduction to JAVA
• JAVA was developed by Sun
Microsystems Inc in 1991, later
acquired by Oracle Corporation.
• It was conceived by James Gosling and
Patrick Naughton. It is a simple
programming language. Writing,
compiling and debugging a program is
easy in java. It helps to create modular
programs and reusable code.
KIIT 2014
Features of JAVA
• JAVA is popular, as it has following
features:
– Platform independent.
– Object Oriented Programming
– Simple
– Robust
– Secure
– Java is distributed
– Multithreading
– Dynamic
KIIT 2014
History of JAVA
• James Gosling initiated the Java
language project in June 1991 for use in
one of his many set-top box projects.
• Sun released the first public
implementation as Java 1.0 in 1995. It
promised Write Once, Run, Anywhere
(WORA), providing no-cost run-times on
popular platforms.
KIIT 2014
History of JAVA
• On 13 November 2006, Sun released
much of Java as free and open source
software under the terms of the General
Public License (GPL).
• On 8 May 2007, Sun finished the process,
making all of Java's core code free and
open-source, aside from a small portion of
code to which Sun did not hold the
copyright.
KIIT 2014
HW / SW requirement
• Hardware:
– Pentium 200-MHz computer with a minimum of
64 MB of RAM (128 MB of RAM recommended).
• Software:
– Linux 7.1 or Windows 95/98/2000/XP operating
system.
– Java JDK 5
– Microsoft Notepad or any other text editor
KIIT 2014
JAVA Environment Setup
• It is important that we set up the Java
environment correctly
• Setting up the path
– c:\Program Files\java\jdk directory
– C:\WINDOWS\SYSTEM32;c:\Program
Files\java\jdk\bin
• Popular Java Editors
KIIT 2014
JAVA Basics
• When we consider a Java program, it can
be defined as a collection of objects that
communicate via invoking each other's
methods
– Object
– Class
– Methods
– Instance Variables
KIIT 2014
First JAVA Program
• Simple code example that would print the
words Hello World.
public class MyFirstJavaProgram{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");// prints
Hello World
}
}
KIIT 2014
Software Development
Process
• For Java program, all source code is first
written in plain text files ending with
the .java extension.
• Those source files are then compiled
into .class files by the javac compiler.
A .class file does not contain code that is
native to your processor; it instead
contains bytecodes — the machine language
of the Java Virtual Machine (Java VM).
The java launcher tool then runs your
application with an instance of the Java
Virtual Machine.
KIIT 2014
JAVA Basic Syntax
• For Java program, remember following
point.
– Case Sensitivity
– Class Names
– Method Names
– Program File Name
– public static void main(String args[])
KIIT 2014
• Identifiers are used for:
JAVA Identifiers
– Naming a variable, methods, class
– Providing a convention for variable
names:
• Must start with a letter
• Can include letters or numbers
• Can include special characters such as
dollar sign, underscore, and pound
sign
• Are case sensitive
• Must not be reserved words
KIIT 2014
JAVA Modifiers
• Like other languages, it is possible to
modify classes, methods, etc., by using
modifiers. There are two categories of
modifiers:
– Access Modifiers
– Non-access Modifiers
KIIT 2014
JAVA Variables
• JAVA has following type of variables
– Local Variables
– Class Variables (Static Variables)
– Instance Variables (Non-static variables)
KIIT 2014
Java Keywords
KIIT 2014
Comments in JAVA
• Java supports single-line and multi-line
comments very similar to c and c++. All
characters available inside any comment are
ignored by Java
public class compiler.
MyFirstJavaProgram{
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public static void main(String[]args){
// This is an example of single line comment
/* This is also an example of single line
comment. */
System.out.println("Hello World");
}}
KIIT 2014
Practice 1
Question 1: When you compile a program written in the Java
programming language, the compiler converts the human-
readable source file into platform-independent code that a Java
Virtual Machine can understand. What is this platform-
independent code called?
Question 2: Which of the following is not a valid comment:
a. /** comment */
b. /* comment */
c. /* comment
d. // comment
Question 3: What is the first thing you should check if you see
the following error at runtime:
Exception in thread "main"
java.lang.NoClassDefFoundError: HelloWorldApp.java.
KIIT 2014
Practice 1
Question 4: What is the correct signature of the main method?
Question 5: When declaring the main method, which modifier
must come first, public or static?
Question 6: What parameters does the main method define?
Question 7: Change the HelloWorldApp.java program so that it
displays Hola Mundo! instead of Hello World!.
Question 8: You can find a slightly modified version
of HelloWorldApp here: HelloWorldApp2.java
The program has an error. Fix the error so that the program
successfully compiles and runs. What was the error?
KIIT 2014
Solution 1
Answer 1: Bytecode.
Answer 2: c is an invalid comment.
Answer 3: Check your classpath. Your class cannot be found.
Answer 4: The correct signature is
public static void main(String[] args) Or
public static void main(String... args)
Answer 5: They can be in either order, but the convention
is public static.
Answer 6: The main method defines a single parameter,
usually named args, whose type is an array of String objects.
Answer 7: This is the only line of code that must change:
System.out.println("Hola Mundo!"); //Display
the string.
KIIT 2014
Solution 1
Answer 8: Here's the error you get when you try to compile the program:
HelloWorldApp2.java:7: unclosed string literal
System.out.println("Hello World!); //Display the string.
^
HelloWorldApp2.java:7: ')' expected
System.out.println("Hello World!); //Display the string.
^
2 errors
To fix this mistake, you need to close the quotation marks around the string.
Here is the correct line of code:
System.out.println("Hello World!"); //Display the string.
KIIT 2014
Summary
In this lesson, you should have learned how to:
• Define JAVA language
• Features of JAVA
• History of JAVA
• Hardware & Software Requirement
• JAVA Environment Setup
• Write Simple JAVA Program
• Define Identifiers
• Types of Variables
• Java Comments
KIIT 2014