Java Programming
17UCTC31
K.A.Sheik Fareed
Assistant Professor of IT
Hajee Karutha Rowther Howdia College
Uthamapalayam-625533
Email Id: www.sfnotes2019@gmail.com
Blog Id : https//:sfnotesforit.blogspot.com
1-1
I. The Java Programming Language
• A programming language specifies the words and
symbols that we can use to write a program
• A programming language employs a set of rules that
dictate how the words and symbols can be put
together to form valid program statements
• The Java programming language was created by Sun
Microsystems, Inc.
• It was introduced in 1995 and it's popularity has grown
quickly since
1-2
Java Program Structure
• In the Java programming language:
– A program is made up of one or more classes
– A class contains one or more methods
– A method contains program statements
• These terms will be explored in detail
throughout the course
• A Java application always contains a method
called main
1-3
Java Program Structure
Import java.io.*; Package importing
(Optional)
public class MyProgram
{
class header
class body
1-4
Java Program Structure
public class MyProgram
{
public static void main(String[] args)
{ method header
method body
}
}
1-5
sample.java
Import java.io.*;
public class sample
{
public static void main (String[ ] args)
{
System.out.println (“Welcome to Java”);
System.out.println (“Its simpler”);
}
}
1-6
Command for Compile the
program
javac sample.java
If there is no error
Command for Run the program
java sample
1-7
Output
Welcome to Java
Its simpler
1-8
Comments
• Comments in a program are called inline
documentation
• They should be included to explain the purpose of
the program and describe processing steps
• They do not affect how a program works
• Java comments can take three forms:
// this comment runs to the end of the line
/* this comment runs to the terminating
symbol, even across line breaks */
/** this is a javadoc comment */
1-9
Identifiers
Identifiers are the words a programmer uses in a program
An identifier can be made up of letters, digits, the
underscore character ( _ ), and the dollar sign($)
Identifiers cannot begin with a digit (ex: int 20sum;)
Java is case sensitive - Total, total, and TOTAL
are different identifiers
By convention, programmers use different case styles for
different types of identifiers, such as
title case for class names - Raja
upper case for constants - MAXIMUM
1-10
Reserved Words
• The Java reserved words:
abstract else interface switch
assert enum long synchronized
boolean extends native this
break false new throw
byte final null throws
case finally package transient
catch float private true
char for protected try
class goto public void
const if return volatile
continue implements short while
default import static
do instanceof strictfp
double int super
1-11
White Space
• Spaces, blank lines, and tabs are called white
space
• White space is used to separate words and
symbols in a program
• Extra white space is ignored
1-12
sample1.java
public class sample1{public static void
main(String[ ]args){
System.out.println("A quote by Abraham
Lincoln:");
System.out.println("Whatever you are, be a
good one.");}}
1-13
II. Program Development
• The mechanics of developing a program
include several activities
– writing the program in a specific programming
language (such as Java)
– translating the program into a form that the
computer can execute
– investigating and fixing various types of errors that
can occur
• Software tools can be used to help with all 1-14
Programming Languages
• Each type of CPU executes only a particular
machine language
• A program must be translated into machine
language before it can be executed
• A compiler is a software tool which
translates source code into a specific target
language
• Often, that target language is the machine
language for a particular CPU type 1-15
Java Translation
The Java compiler translates Java source code into a
special representation called bytecode
Java bytecode is not the machine language for any
traditional CPU
Another software tool, called an interpreter, translates
bytecode into machine language and executes it
Therefore the Java compiler is not tied to any particular
machine
Java is considered to be architecture-neutral
1-16
Java Translation
Java source
code Java
bytecode
Java
compiler Bytecode
Interpreter
Machine
code
1-17
Development Environments
There are many programs that support the
development of Java software, including:
Sun Java Development Kit (JDK)
Sun NetBeans
IBM Eclipse
Borland JBuilder
MetroWerks CodeWarrior
BlueJ
jGRASP
Though the details of these environments differ, the
basic compilation and execution process is essentially
the same
1-18
Syntax and Semantics
• The syntax rules of a language define how we
can put together symbols, reserved words,
and identifiers to make a valid program
• The semantics of a program statement define
what that statement means (its purpose or
role in a program)
• A program that is syntactically correct is not
necessarily logically (semantically) correct
1-19
•
Errors
A program can have three types of errors
The compiler will find syntax errors and other basic problems
(compile-time errors)
If compile-time errors exist, an executable version of the program is
not created
A problem can occur during program execution, such as
trying to divide by zero, which causes a program to terminate
abnormally (run-time errors)
A program may run, but produce incorrect results, perhaps
using an incorrect formula (logical errors)
1-20
Basic Program Development
Edit and
save program
Errors
No errors
Errors
Compile program
No errors
Execute program and
evaluate results
1-21