Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Concepts Of Programming

Soorya M
Project Engineer
What is Java?
▪ A high-level third generation language
▪ Designed by Sun Microsystems, as an alternative to C++
▪ Most-popular language for generating internet-based
applications.
▪ Derives the syntax from C language and much of the object
oriented concepts from C++

7 March 2022 Knowledge Resource Centre


Programming language
▪ It is a high-level language that is
– Simple
– Secure
– Portable
– Object-oriented
– Robust
– Multithreaded
– Architecture-Neutral
– Interpreted
– Distributed
7 March 2022 Knowledge Resource Centre
Java Programming Language
▪ Java is Simple
– Removal of complicated statements

▪ Java is object-oriented
– Java considers everything as an object

7 March 2022 Knowledge Resource Centre


Java Programming Language
▪ Java is a distributed language
– Designed for the distributed environment of
Internet
▪ An interpreted language
– Java compiles programs into an intermediate
representation called the bytecode
– This code can be executed on any machine that has
JVM
7 March 2022 Knowledge Resource Centre
Contd..
▪ Architecture-Neutral
– Independent of any processor type and machine
architecture
– Goal used is “write once, run anywhere, any time
forever”
▪ Robust
– In a well-written Java program, all run-time errors
can be handled by the system
• Exception handling, strong type checking
7 March 2022 Knowledge Resource Centre
Contd..
▪ Secure
– Enables construction of virus-free systems
– Java program always runs in Java Runtime Environment with
almost null interaction with system OS, hence it is more secure.
▪ Portable
– Java bytecode can be carried to any platform
▪ Multi-threaded
– Java programs that deal with many tasks at once by defining
multiple threads.
7 March 2022 Knowledge Resource Centre
Platform-Independence

7 March 2022 Knowledge Resource Centre


7 March 2022 Knowledge Resource Centre
Java Development Kit
▪ JDK contains tools needed to develop the Java
programs, and JRE to run the programs.
▪ The tools include Compiler (javac), Java application
launcher (java), Appletviewer, The Java
Debugger(jdb), Java documentation generator
(JavaDoc) etc...

7 March 2022 Knowledge Resource Centre


Java Runtime Environment
▪ JRE has two components:
– Java Virtual Machine (JVM)
• Virtual machine that executes byte code
• Available on many types of h/w and s/w platforms which
enables Java to function as a middleware and a platform on its
own
• JVM also performs garbage collection
– Java Application Programming Interface (Java API)

7 March 2022 Knowledge Resource Centre


Simple Java Application
Java programs are made up of classes
public class SayHello {

public static void main(String[] args) {


System.out.println("Hello world");
}
}
SayHello.java

7 March 2022 Knowledge Resource Centre


▪ A .java file can be compiled to a .class file using
javac compiler as follows:
prompt> javac SayHello.java
… compiler output …
▪ An application .class file is executed using Java
application launcher java as follows:
prompt> java SayHello
Hello world
prompt>
7 March 2022 Knowledge Resource Centre
7 March 2022 Knowledge Resource Centre
Comments
▪ Comments explain the program to yourself and others
▪ Block comments
– Can span several lines
– Begin with /*
– End with */
– Compiler ignores all text between /* and */
▪ Line comments
– Start with //
– Compiler ignores text from // to end of line

7 March 2022 Knowledge Resource Centre


Identifiers - symbolic names
▪ Identifiers are used to name classes, variables, and
methods.
– Must start with a "Java letter"
• A - Z, a - z, _, $
– Can contain essentially any number of Java letters and
digits, but no spaces
– Case sensitive!!
• Number1 and number1 are different!
– Cannot be keywords or reserved words

7 March 2022 Knowledge Resource Centre


Program Building Blocks
▪ Statement
– Performs some action
– Terminates with a semicolon (;)
– Can span multiple lines
▪ Block
– 1or more statements
– Begins and ends with curly braces { }
– Can be used anywhere a statement is allowed.

7 March 2022 Knowledge Resource Centre


White Space Characters
▪ Space, tab, newline are white space characters
▪ At least one white space character is required
between a keyword and identifier
▪ Any amount of white space characters are permitted
between identifiers, keywords, operators, and literals

7 March 2022 Knowledge Resource Centre


Data Types
▪ For all data, assign a name (identifier) and a data type
▪ Data type tells compiler:
– How much memory to allocate
– Format in which to store data
– Type of operations to perform on data
▪ Compiler monitors use of data
– Java is a "strongly typed" language
▪ Java "primitive data types"
byte, short, int, long, float, double, char, boolean
7 March 2022 Knowledge Resource Centre
Declaring Variables
▪ Variables hold one value at a time, but that value can
change
▪ Syntax:
dataType identifier;
or
dataType identifier1, identifier2, …;

7 March 2022 Knowledge Resource Centre


Integer Types
Type Size in Bytes Minimum Value Maximum Value

byte 1 -128 127


short 2 -32,768 32,767
int 4 -2, 147, 483, 6 2, 147, 483, 647
long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Example declarations:
int numPlayers, highScore, diceRoll;
short xCoordinate, yCoordinate;
byte ageInYears;
long cityPopulation;

7 March 2022 Knowledge Resource Centre


Floating-Point Data Types
▪ Numbers with fractional parts
Type Size in Bytes
float 4
double 8 (used in mathematical functions like sin, cos)
Example declarations:
float salesTax;
double interestRate;
double paycheck, sumSalaries;

7 March 2022 Knowledge Resource Centre


char Data Type

▪ One Unicode character (16 bits - 2 bytes)


Type Size in Bytes
char 2

7 March 2022 Knowledge Resource Centre


boolean Data Type
▪ Two values only:
true
false
▪ Used for decision making or as "flag" variables
▪ Example declarations:
boolean isEmpty;
boolean passed, failed;
7 March 2022 Knowledge Resource Centre
7 March 2022 Knowledge Resource Centre
Literals
▪ Literal in Java denotes a constant value
▪ Integer literals
Integer 0 1 42 -23795
02 077 0123
0x0 0x2a
0b1010
123_456_789
365L 077L

7 March 2022 Knowledge Resource Centre


▪ Floating point literals
Floating 1.0 4.2 .47
point 1.22e19 4.61E-9
6.2f 6.21F

▪ Char Character ‘a’ ‘\n’ ‘\t’

7 March 2022 Knowledge Resource Centre


▪ String
– text contained within double quotes.
– Must begin and end on the same line
– String concatenation operator is ‘+’

String “Hello, world\n”

7 March 2022 Knowledge Resource Centre

You might also like