Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 61

Introduction to

Java
Unit 1 / Chapter 1
Basics of java
programming
1.1 HISTORY
• Java is high-level object-oriented programming language developed by the
sun microsystems.
• It was developed for the electronics and communication equipment.
• Later, it came into existence as a part of web application, web services and
platform independent programming language in 1990s
• Sun released the first public implementation as java 1.0 in 1995 as “write
once, Run anywhere”.
1.2 JAVA FEATURES
Objected Distributed Complied and
Simple Portability
Oriented computing Interpreted

Architecture Dynamic and


Security Robust Multithreading
Neutral Extensible

High Multimedia
Performance Support
1.3 DIFFERENCE BETWEEN JAVA AND C
Sl. No C JAVA
Java is OOP language, everything in java is a
1 C is structured and POP language
part of the class
2 Pointers in C No pointers
Memory allocation and deallocation is Java Virtual Machine takes care of memory
3
handled by programmer management
4 Automatic type casting is available in C In some cases, implicit casting available in java
#define, typedef and header files are
5 No such files will be used.
used in C
6 Supports global variables No global variables in java
7 Supports structure and union No such methods
1.4 JAVA
ENVIRONMENT
• The java environment contains both
development tools and class libraries.
• The development tools are part of Java
Development Kit (JDK).
• Class libraries are part of java Application
Program Interface (API).
JAVA DEVELOPMENT KIT
• Appletviewer
• Javac (java compiler)
• Java (java interpreter)
• Javap (java disassembler)
• Javah (for c header files)
• Javadoc (for creating HTML documents)
• Jdb (java debugger)
SL.
TOOL DESCRIPTION
NO

1 Appletviewer Enables us to run java applets

Java interpreter, which runs applets and applications by reading the interpreting
2 Java
bytecode files

The java compiler, which translate java source code to bytecode files that the
3 Javac
interpreter can understand

4 Javadoc Creates HTML – format documentation from java source code files.

5 Javah Produces header files for use with native methods.

Java disassembler, which enables us to convert bytecode files into a program


6 Javap
description

7 Jdb Java debugger, which helps us to find errors in our programs.


JAVA RUNTIME ENVIRONMENT
• JVM
• Runtime Class Libraries
• User Interface toolkits
• Development Technologies
• Java plugin
• Java web start
SIMPLE JAVA
PROGRAM
class SampleOne
{
public static void main( string args[])
{
system.out.println(“Welcome to Java
Programming”);
}
}
UNDERSTANDING JAVA PROGRAM

1. CLASS DECLARATION: the first line class SampleOne declares as class,


which is an object-oriented construct.

• Since java is truly object-oriented language everything must be placed


inside the class.

• Here class is a keyword and declares that a new class definition, SampleOne
is a java identifier that specifies the name of the class
2. BRACES

3. THE MAIN LINE: public static void main (string args[])

• Defines the method named main, its similar to main() function in c.

• Every java application program must include main() method and it’s the
starting point for the interpreter to begin the execution of the program.

• Here keywords are public, static and void.


KEYWORD DESCRIPTION

The keyword public is an access specifier that declares the main


Public method as unprotected and therefore making it accessible to all other
classes.
Which declares this method as one which belongs to all class not
Static
belongs to any object
Void States that the main me method does not return any value.

OUTPUT LINE: It is the executable line system.out.println(“ Welcome to Java


Programming”).
This is similar to printf() function in c language.
JAVA PROGRAM STRUCTURE
DOCUMENTATION SECTION
The documentation section comprises a set of comment lines giving the name
of the program, the author and other details.

Comments must explain why and what of classes and how of algorithms, this
will help in maintaining the program.

/** ……….. */ is the comments style used in java for documentation.


PACKAGE STATEMENT

The first statement allowed in a java file is a package.

This statement declares a package name and informs the compiler that the classes
defined here belongs to this package.

Example: package student;

Package statement is optional. That is, our classes do not have to be part of
package.
This is similar to #include statement in c
language.

Import student.test;
IMPORT
STATEMENTS
This statement instructs the interpreter to
load the test class contained in the
package student.

Using import statements, we can have


access to classes that are part of other
named packages.
INTERFACE STATEMENTS

This is also an optional section and is


An interface is like a class but includes a
used only when we wish to implement
group of method declarations.
multiple inheritance feature.
A java program may contain multiple class definition,
classes are the primary and essential elements of a java
program.

CLASS These classes are used to map the objects of real world
DEFINITION problems.

The number of classes used depends on the complexity


of the program.
MAIN METHOD CLASS
1 2 3
Every java stand alone This main class is The main method
program requires a main essential part of java creates objects of
method as its starting program. various classes and
point. establishes
communications
between them.
JAVA TOKENS
A java program is basically a collection of classes.

A class is defined by a set of declaration statements and


methods containing executable statements.

Most statements contains expressions, which describe


the actions carried out on data.

Smallest individual unit in a program are known as


tokens.

In simple terms, a java program is a collection of tokens,


comments and white spaces.

Java language includes 5 types of tokens.


Reserved
Identifiers Literals Operators separators
keywords
JAVA CHARACTER SET

• The smallest units of java language are the characters used


to write java tokens.

• These characters are defined by the Unicode character set.

• The Unicode is a 16-bit character coding system and


currently supports more than 34,000 characters derived
from 24 languages from America, Europe, Middle East,
Africa and Asia
a. Keywords
Keywords are an essential part of a language definition.

They implement specific features of the language.

Java language has reserved 50 words as keywords.

These keywords will combine with operators and separators according to the syntax, from the
definition of java language.

All keywords should be written in lowercase letters.


abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return short static strictfp super
switch synchronised this throw throws
transient try void volatile while
IDENTIFIERS
• Identifiers are programmer designed tokens.

• They are used for naming classes, methods, variables, objects, labels, packages and
interfaces in a program.

• Rules to write identifiers,

They can have alphabets, digits and the underscore and dollar sign characters.

They must not begin with digit.

Uppercase and lowercase letters are distinct.

They can be of any length.


Java developers have followed some naming conventions,

1. Name of all public methods and instance variables start with a lowercase letter.

Example: average, sum, total

2. When more than one words are used in a name, the second and subsequent words are
marked with uppercase letters.

Example: dayTemperature, firstDayOfTheMonth, totalMarks.


LITERALS
• Literals in java are a sequence of characters [digits, letters
and other characters] that represent constant values to be
stored in variables.

• Java language specifies five types of literals,

1. Integer Literals

2. Floating_point Literal

3. Character Literal.

4. String Literal

5. Boolean Literal
Arithmetic Operators

Relational Operators

Logical Operators

Assignment Operators
operators Increment and decrement Operators

Conditional Operators

Bitwise Operators

Special Operators
SEPARATORS
• Separators are symbols used to indicate where groups of code are divided and
arranged.
• They basically define the shape and function of our code.
Name Used for
Parentheses() Used to enclose parameters
Braces {} Used to contain the values of automatically initialized arrays
Brackets [] Used to declare array types
Semicolon ; Used to separate statements
Comma , Used to separate consecutive identifiers
Period . Used to separate package names from sub-packages and classes
JAVA STATEMENTS

• Statements in java are like sentences in natural languages.

• A statement is an executable combination of token ending with semicolon mark.

• Statements are usually executed in sequence in the order in which they appear.

• However, it is possible to control the flow of execution, if necessary.


Java implements several types of
statements

Expression Labelled Control Synchronization Guarding


statements Statements Statements Statement Statement
Constants, Variables and
Data Types
1. CONSTANTS
• Constants in java referred to fixed values that do not change during the execution of a
program.
• Java supports several types of constants,
1. Integer constants
2. Real constants
3. Single character constants
4. String constants
5. Backslash character constants
1.1 Integer Constants

An integer constant refers to a sequence of digits.

There are3 types of integers; decimal integer, octal and hexadecimal integer.

Decimal integer consist of set of digits [0-9], preceded by an optional minus sign.
• Example: 123, -254, 25477

Embedded spaces, commas, and non-digit characters are not permitted between digits.
• Example: 12 750 30.000 $100 are illegal numbers.
An octal integer constant A sequence of digits
consist of any combination preceded by 0x or 0X is
of digits from the set 0 considered as hexadecimal
through 7, with a leading 0. integer.

Example: 0X2
Example: 037
ox9F
00 0437 0661
oxbd
Integer constants are inadequate to represent
quantities that vary continuously, such as
distance, heights, temperature and so on.

1.2 Real The quantities are represented by numbers


containing fractional parts like 17.56 are called
Constants real [floating point] constants.

Example: 0.0025 -0.254 -.57


1.3 Single Character Constants
• A single character constant contains a single character enclosed
within a pair of single quotes.
Example: ‘A’ ‘X’ ‘;’
1.4 String Constants

A string constant is a sequence of characters enclosed between double quotes.

The characters may be alphabets, digits, Example: “Hello Java”, “1997”, “!@...,”
special characters and blank spaces

Java supports some special backslash


character constants that are used in Example: ‘\b’, ‘\n’ , ‘\t’

output methods.
2.
VARIABLE • A variable is an identifier that denotes a
storage location used to store a data
S value.
• Unlike constants that remain unchanged
during the execution of a program, a
variable make take different values at
different times during the execution.
• Example :height, length, total
• Variable names may consist of alphabets,
digit, the underscore and dollar character,
subjected to the following condition,
• They must not begin with digit

• Uppercase and lower case are distinct

• It should not be a keyword

• White space is not allowed

• Variable names can be of any length


3. DATA TYPES
Every variable in java has a data type.

Data type specify the size and type of values


that can be stored.

Java language is rich in its data types.

Data types in java is broadly classifieds as


primitive and non-primitive data types.
4. DECLARATION OF VARIABLES

• In java, variables are names of storage locations.

• After designing suitable names, we must declare them to the compiler,


declaration does 3 things,
• It tells the compiler what the variable name is.

• It specifies what type of data the variable will hold.

• The place of declaration decides the scope of the variable.


• A variable must be declared before it is used in the program.
• Syntax:
type variable1, variable2…………variable;
• A declaration statement must end with a semicolon.
• Example: int count; float x, y;
5. GIVING VALUES TO
VARIABLES
 By using assignment statement
• variableName=value;
• A=b=c=d=10;
• int a=10;
 By using a read statement
• readLine()
• intNumber=Integer.parseInt(in.readLine())
6. SCOPE OF VARIABLES

INSTANCE CLASS LOCAL


VARIABLES VARIABLES VARIABLES
• Instance and class variables are declared inside a class.
• Instance variables are created when the objects are instantiated.
• Instance variables are associated with the objects, they take different values
for each object.
• Class variables are global to class and belong to the entire set of objects that
class creates.
• Variables declared and used inside methods are called local variables.
7. GETTING VALUES OF VARIABLES

• A computer program is written to manipulate a given set of data and to


display or print results.

• Java supports 2 output methods,


• print() //print and wait

• println() //print a line and move to next line


• The print() method sends information into a buffer, this buffer is not cleared until a
newline character is sent.

• As a result, the print() method prints output on one line until a new line character is
encountered.

System.out.println (“Hello”);

System.out.println (“Java”);
The println() method, by contrast, takes the information provided and displays it
on a line followed by a line feed

System.out.println(“Hello”); System.out.println(“JAVA”);
THANK YOU

You might also like