0% found this document useful (0 votes)
55 views67 pages

Unit 2.1

This document discusses the basics of Java program structure and syntax. It covers the three types of Java programs, importing packages and classes, writing a simple Java program including the main method, compiling and running a Java program, and the basic tokens used in Java programs including identifiers, keywords, literals, separators, and operators. It also discusses declaring and naming variables, conventions for writing names, and the different types of variables in Java.

Uploaded by

James Cardon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views67 pages

Unit 2.1

This document discusses the basics of Java program structure and syntax. It covers the three types of Java programs, importing packages and classes, writing a simple Java program including the main method, compiling and running a Java program, and the basic tokens used in Java programs including identifiers, keywords, literals, separators, and operators. It also discusses declaring and naming variables, conventions for writing names, and the different types of variables in Java.

Uploaded by

James Cardon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Program

Structure
in Java
CONCEPT OF OBJECT
ORIENTATION
Three types of programs in Java:
Stand-alone application programs These are
made and run on users’ computers.

Applet programs These are meant to run in a


web page on the Internet.

Java servlets These run in computers that


provide web services. They are also often
called servlets.
IMPORTING PACKAGES AND
CLASSES
 Java standard library contains a
large number of packages.
 Any program in Java may need
one or more classes defined in the
Java standard library.
 Only one import statement is
written in one line.
IMPORTING PACKAGES AND
CLASSES
 import statements are written at
the top of the program.
 Only one import statement is
written in one line.
 E.g importing the complete
packages is as follows:
import java.util.*
IMPORTING PACKAGES AND
CLASSES
 java refers to the main java
package.
 It contains all the packages of
Java.
 In some case we may need to
import only one class of the
package,
IMPORTING PACKAGES AND
CLASSES
 For such case, the code is
import java.util.Random
 Here Random is the name of the class
that deals with generation of
pseudorandom numbers.
 Class Random is a part of package
util contained within the package
java.
IMPORTING PACKAGES AND
CLASSES
 There is one package- java.lang that
is imported by default by the
compiler.

 There is no need to write this import


statement.
WRITING SIMPLE JAVA
PROGRAM
 A class declaration starts with the
keyword class, followed by the
identifier or name of the class.
 E.g. class Start

 Giving the name of a package at


the top is optional.
WRITING SIMPLE JAVA
PROGRAM
 The first word public is the access
modifier.

 This class is accessible to any


outside code.
WRITING SIMPLE JAVA
PROGRAM
 If default modifier is used, the class
is accessible only to the classes in its
own package.

 The second word class is the


keyword of Java language.
WRITING SIMPLE JAVA
PROGRAM
 The class body starts with the left
brace ({) and ends with the right
closing brace (}).

 A class body may comprise


statements for declaration of
variables, constants, expressions,
and methods.
WRITING SIMPLE JAVA
PROGRAM
 The main() method is qualified by
three qualifiers:
- public
- static
- void

 main() must be declared as public


since it must be called by code
outside of its class .
WRITING SIMPLE JAVA
PROGRAM
 Keyword static allows main() to be
called without instantiating a
particular instance of a class.

 Keyword void indicates that main


method does not return a value.
WRITING SIMPLE JAVA
PROGRAM
 main() method is called when Java
application begins.
 Any information that is required to
pass to a method is done by variables
specified within the set of parenthesis
following the name of the method.

 These variables are called as


parameters.
WRITING SIMPLE JAVA
PROGRAM
 In main(), there is only one
parameter, i.e. class String args[].
 String args[] declares a parameter
named args which is an array of
instances of class String.
 Next line of the code outputs the
string “lets learn Java”.
WRITING SIMPLE JAVA
PROGRAM
 Output is accomplished by println()
method.
 In System.out.println, System is the
predefined class that provides
access to the system.
 out is the output stream that is
connected to the console.
WRITING SIMPLE JAVA
PROGRAM
Note:
 Java is case sensitive language.

 All statements in Java ends with a


semicolon.
INSTALLING AND
CONFIGURING JAVA
 For running Java programs, Java
development tools or the Java
platform is needed.

 The latest version of JDK may be


download from the Internet by
opening an Oracle site.
COMPILING AND RUNNING
JAVA PROGRAM
 In C or C++ , the program is compiled
for the machine you are working on.

 In Java, the compiler first converts the


source code into an intermediate
code.
 It is known as bytecode or virtual
machine code
COMPILING AND RUNNING
JAVA PROGRAM
 For compiling the program, the
Java compiler javac is run on the
command line as :
javac Start.java
 The javac compiler creates a file
called Start.class.

 It contains the bytecode version of


the program.
COMPILING AND RUNNING
JAVA PROGRAM
 Java interpreter in JVM executes
the instructions contained in this
intermediate Java bytecode.

 For this, the Java interpreter


called java is used.
c:>java Start
COMPILING AND RUNNING
JAVA PROGRAM
 Java interpreter looks for the main
method in the program and starts
executing it.

 If it is successfully executed, it would


display the following:
Let us learn JAVA.
TOKENS IN JAVA
PROGRAMS
The smallest program element
recognized by the compiler and which
treats them as defined for the compiler.

Identifiers or names This is meant for variables,


methods, classes, etc.

Keywords These are special words defined in


Java and represent a set of instructions.
TOKENS IN JAVA
PROGRAMS
Literals These are values represented by
a set of characters, digits, etc.

Separators These include comma,


semicolon, period (.), (), [], etc.

Operators These are mostly represented


by symbols such as + , −,*, etc.
IDENTIFIERS OR NAMES
Rules for framing names:
1. It may be a single word or a
combination of words with no
space between words.
2. It should not be a keyword of Java.
3. It should not be Boolean literal, that
is, true or false.
4. It should not be null literal.
IDENTIFIERS OR NAMES
Rules for framing names:
5. It should not start with a digit but
it can start with an underscore.

6. It can comprise one or more


unicode characters which are
characters as well as digits.
CONVENTIONS FOR
WRITING NAMES
 Names of packages are
completely in lower-case letters
such as mypackage, java.lang.

 Names of classes and interfaces


start with an upper-case letter.
CONVENTIONS FOR
WRITING NAMES
• Names of methods start with a
lower-case character.

• Names of variables should start


with a lower-case character.
KEYWORDS IN JAVA
 The keywords represent groups
of instructions for the compiler.

 These are special tokens that


have a predefined meaning
and their use is restricted.
KEYWORDS IN JAVA

 keywords cannot be used as


names of variables, methods,
classes, or packages.

 These are written in the lower


case.
KEYWORDS IN JAVA
LITERALS IN JAVA
• It represents a value which may be
of primitive, String, or null type.
• The value may be a number (either
whole or decimal point number)
• It may be sequence of characters
which is called String literal, Boolean
type, etc.
• A literal is a constant value.
LITERALS IN JAVA
Types of Literals:
1. Integer literals
2. Floating point literal
3. Boolean literal
4. Character literal
5. String literal
6. Null literal
LITERALS IN JAVA
The integral literals
• Sequences of digits.
• The whole numbers
• Different number systems such as
decimal numbers, hexadecimal
numbers, octal numbers, and
binary numbers.
• Each number has a different set of
digits
LITERALS IN JAVA
Decimal Integer Literals
• These are sequences of decimal
digits which are 0, 1, 2, 3, 4, 5, 6, 7,
8, and 9.
• Examples of such literals are 6, 453,
34789, etc.
LITERALS IN JAVA
Hex Integral Literals
• These are sequences of hexadecimal
digits which are 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, A, B, C, D, E, and F.
• The values 10 to 15 are represented
by A, B, C, D, E, and F or a, b, c, d, e,
and f.
• The numbers are preceded by 0x or
0X. Examples are 0x56ab o0X6AF2,
etc.
LITERALS IN JAVA
Octal Integer Literals
• These are sequences of octal digits which
are 0, 1, 2, 3, 4, 5, 6, and 7.
• These numbers are preceded by 0. Examples
of literals are 07122, 04, 043526,
Binary Literal
• These are sequences of binary digits.
• Binary numbers have only two digits—0 and
1 and a base 2.
• Examples of such literals are 0b0111001,
0b101, 0b1000, etc.
LITERALS IN JAVA
Floating Point Literal
• These are floating decimal point numbers
or fractional decimal numbers with base
10. Examples are 3.14159, 567.78, etc.

Boolean Literal
• These are Boolean values. There are only
two values—true or false.
LITERALS IN JAVA
Character Literal
• These are the values in characters.
• Characters are represented in single quotes
such as ‘A’, ‘H’, ‘k’, and so on.
String Literals
• These are strings of characters in double
quotes. Examples are “Delhi”, “John”, “AA”,
etc.
Null Literal
There is only one value of Null Literal, that is, null.
SEPERATOS IN JAVA
Separators
OPERATORS IN JAVA
 There are 37 operators in Java
OPERATORS IN JAVA..CONT

OPERATORS IN JAVA
 There are 37 operators in Java
DECLARATION OF
VARIABLES
• Variables are objects whose values may
change in the program.
• A variable is declared by first writing its
type, followed by its name or identifier.
TYPES OF VARIABLES
Local variables These are declared inside a
method or are arguments to a method.
Instance variables These variables may be
declared anywhere in a class but outside
a method.
- Associated with objects.
- Created when objects are instantiated.
- Take different values for each object.
TYPES OF VARIABLES
Static or class variables
- The declaration of these variables is
modified by the keyword static.

- The program keeps only one copy of


these variables which are shared by all the
objects of a class.
COMMENTS
• Comments are not a part of the compiled
program.
• They are added to the source code of the
program.
Java supports three types of comments as:

1. Single-line comment
2. Multi-line comment
3. Documentation comment
COMMENTS
Single-line comment It starts after a double slash
(//) up to the end of the line. E.g.
//This is single line comment.

Multi-line comment - comment is enclosed


between slash followed by star at the
beginning as (/*) and star followed by slash
(*/) at the end. E.g.
/* This is a multi-line comment. It starts with slash
and star and ends with star followed by
slash.*/
ESCAPE SEQUENCES

 These characters consist of a


backslash followed by a letter
such as “\t”, “\n”, and so on.

 These have special meaning for


the compiler.
ESCAPE SEQUENCES
PRINTING SIMPLE PATTERNS
PRINTING SIMPLE PATTERNS
PRINTING SIMPLE PATTERNS
PRINTING SIMPLE PATTERNS
PRINT() AND PRINTLN()
METHODS
PRINT() AND PRINTLN()
METHODS
PROGRAMMING STYLE
USER INPUT TO PROGRAMS
• Class Scanner of package java.util is
used to carry out input to the
program.
• Java Scanner class is a text scanner
that breaks the input into tokens
using a delimiter.
• Class is first Imported.
• The object of the class Scanner is
then declared.
USER INPUT TO PROGRAMS
Local variables These are declared inside a
method or are arguments to a method.
Instance variables These variables may be
declared anywhere in a class but outside
a method.
Static or class variables The declaration of
these variables is modified by the keyword
static.
- The program keeps only one copy of
values of these variables which are shared
by all the objects of a class.
USER INPUT TO PROGRAMS
USER INPUT TO PROGRAMS
USER INPUT TO PROGRAMS
MATHEMATICAL FUNCTIONS
• The class Math of the package
java.lang. defines several
mathematical methods.
• java.lang package is imported by
default in every Java program.
• All the methods of class Math are
declared static, that is, it can be
called even
MATHEMATICAL FUNCTIONS
USE OF CLASS MATH IN
MATHEMATICAL CALCULATIONS
PROGRAM EXAMPLE-1

You might also like